首页 > 代码库 > 笨笨熊搬家交通篇
笨笨熊搬家交通篇
笨笨熊要搬家,它现在的家在B点,新的豪宅在H点,现在要从B点到H点
地图R表示行,C表示列,其中:
-表示能通过
#表示不能通过
B表示笨笨熊原来的家
H表示笨笨熊的新豪宅
输入
R
C
R×C矩阵
输出
Y//表示可以通过
N//表示不可以通过
#include <iostream> #include <queue> using namespace std; //Point in the grid typedef struct { int x; int y; }Pos; //record the BSF progress , then go back to find the right road typedef struct { Pos curpoint; int prepoint_pos;//previous in records (parent node) }Record; //记录遍历节点 int record_len = 0; int dequeue_count = -1;//出队列的节点在记录数组中的位置 // const int M = 5; const int N = 4; char grid[M][N] = { '-','-','-','-', 'B','-','#','H', '-','-','#','-', '-','-','#','-', '#','-','-','-', }; Record BFS_Record[M*N];// //其目的是为了在找到出口后沿这个下标返回到入口处,方便最后显示这条最短路径。 queue<Pos> roadqueue; bool map[M][N] = {false}; Pos GetDataPos(char c); bool IsHome(Pos pos,char c); int EnqueueAdjacentPos(Pos pos); int VisitAndPush(int i,int j); int main() { Pos beg; bool flag = false; cout << "***当前地图***" << endl; for(int m = 0;m<M;m++) { for(int n = 0;n<N;n++) cout << grid[m][n]; cout<< endl; } beg = GetDataPos('B'); roadqueue.push(beg); //有元素入队列 就记录 遍历节点与父节点的信息 BFS_Record[record_len].curpoint = beg; BFS_Record[record_len].prepoint_pos = dequeue_count; //cout << "record_len:" << record_len << endl; record_len ++; // int i = beg.x, j = beg.y;//may existed non-conncted graph cout << "*****能否从B到达H*****" << endl; if(!map[i][j]) { //cout << "(" << i << "," << j << "): " << grid[i][j] << endl;//visit map[i][j] = true; while(!roadqueue.empty()) { Pos tmp = roadqueue.front(); dequeue_count++;//又一个元素出队列 roadqueue.pop(); if(EnqueueAdjacentPos(tmp))// right down left up { cout << "YES" << endl; flag = true; break; } } } if(!flag) cout << "NO" << endl;//roadqueue is empty , but still not reach the pos with 'H' //end = GetDataPos('H'); cout << endl << "****记录遍历的坐标点*******************************" << endl; cout << grid[ BFS_Record[0].curpoint.x ][ BFS_Record[0].curpoint.y ] << endl; for(i = 0;i<record_len;i++) { cout << "[" << i << "]:" << "(" << BFS_Record[i].curpoint.x << "," << BFS_Record[i].curpoint.y << ") " << BFS_Record[i].prepoint_pos << endl; } cout << grid[ BFS_Record[record_len - 1].curpoint.x ][ BFS_Record[record_len - 1].curpoint.y ] << endl; cout << endl << "******H<--B的一条最短路径******" << endl << endl; for(i = record_len - 1;i>0;) { cout << "(" << BFS_Record[i].curpoint.x << "," << BFS_Record[i].curpoint.y << ")"; cout << "<--"; i = BFS_Record[i].prepoint_pos; } cout << "(" << BFS_Record[i].curpoint.x << "," << BFS_Record[i].curpoint.y << ")"; cout << endl << endl; return 0; } Pos GetDataPos(char c) { Pos tmp; tmp.x = tmp.y = -1; for(int i = 0;i<M;i++) for(int j = 0;j<N;j++) if(grid[i][j] == c) { tmp.x = i; tmp.y = j; return tmp; } return tmp; } bool IsHome(Pos pos,char c) { if(grid[pos.x][pos.y] == c) return true; else return false; } int EnqueueAdjacentPos(Pos cur_pos) {/* right down left up */ int cur_x = cur_pos.x,cur_y = cur_pos.y; int i,j; //has right pos if(cur_y >=0 && cur_y < N-1) { i = cur_x; j = cur_y + 1; if(VisitAndPush(i,j)) return 1; } //has down pos if(cur_x >=0 &&cur_x < M-1) { i = cur_x +1; j = cur_y; if(VisitAndPush(i,j)) return 1; } //has left pos if(cur_y > 0 && cur_y < M) { i = cur_x; j = cur_y -1; if(VisitAndPush(i,j)) return 1; } //has up pos if(cur_x > 0 && cur_x < M) { i = cur_x - 1; j = cur_y; if(VisitAndPush(i,j)) return 1; } return 0; } int VisitAndPush(int i,int j) { Pos tmp; if(!map[i][j] && grid[i][j] != '#')//*****unvisited and available pos ( - , H) { map[i][j] = true; tmp.x = i; tmp.y = j; //有元素入队列 就记录 遍历节点与父节点的信息 BFS_Record[record_len].curpoint = tmp; BFS_Record[record_len].prepoint_pos =dequeue_count; //cout << "record_len:" << record_len << endl; record_len ++; // roadqueue.push(tmp); if(grid[i][j] == 'H')//arrive the end pos [最后一个结点H进入队列 算法终止] return 1; } return 0; }
笨笨熊搬家交通篇
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。