迷宫最短路径的C++实现(队列:广度优先)

#include<iostream> #include<queue> #include<string> using namespace std; struct point{ int x; int y; point *last;//上一步的坐标 }; int main(){ while(1){ int row, col, i, j; cout<<"请输入迷宫图的行数和列数:"; cin>>row>>col; int **a = new int* [row+2]; for(i = 0; i < row+2; ++i){ a[i] = new int[col+2]; } cout<<"请输入迷宫图(1代表墙壁,0代表通路):"<<endl; for(i = 1; i < row+1; ++i){ for(j = 1; j < col+1; ++j){ cin>>a[i][j]; } } for(i = 0; i < col+2; ++i){//加墙 a[0][i] = 1; a[row+1][i] = 1; } for(i = 1; i < row+1; ++i){//加墙 a[i][0] = 1; a[i][col+1] = 1; } queue<point*> q; point *start = (point*)malloc(sizeof(point));//起点 cout<<"请输入起点坐标(范围1,1到"<<row<<","<<col<<"):"; cin>>start->x>>start->y; start->last = start; q.push(start); a[start->x][start->y] = 2; point end;//终点 cout<<"请输入终点坐标(范围1,1到"<<row<<","<<col<<"):"; cin>>end.x>>end.y; int aspect[4][2] = {{0, -1},{0, 1},{-1, 0},{1, 0}};//转向:上下左右 int flag = 0;//是否有路可走的标志 while(!q.empty()){ point *front = q.front(); q.pop();//弹出队头 if(front->x == end.x && front->y == end.y){ flag = 1; cout<<"成功找到出路."<<"最少需要"<<a[front->x][front->y]-2<<"步。如下所示:"<<endl; a[front->x][front->y] = -6; //cout<<"倒退回去:"<<(front)->x<<","<<(front)->y; point *lastPoint = front; front = front->last; while((front->x != start->x) || (front->y != start->y)){ //cout<<"->"<<front->x<<","<<front->y; if(lastPoint->x - front->x == 1){ a[front->x][front->y] = -1; }else if(lastPoint->x - front->x == -1){ a[front->x][front->y] = -2; }else if(lastPoint->y - front->y == 1){ a[front->x][front->y] = -3; }else{ a[front->x][front->y] = -4; } lastPoint = front; front = front->last; } //cout<<"->"<<start->x<<","<<start->y<<endl; a[start->x][start->y] = -5; break; }else{ for(int i = 0; i < 4 ; ++i){ point *temp = new point; temp->x = front->x + aspect[i][0]; temp->y = front->y + aspect[i][1]; if(a[temp->x][temp->y] == 0){ temp->last = front; q.push(temp); a[temp->x][temp->y] = a[front->x ][front->y] + 1; } } } } if(!flag) cout<<"无路可走!"<<endl; else{ string s[6] = {"★", "☆", "←","→","↑","↓" }; for(int i = 0; i < 11; ++i){ for(int j = 0; j < 10; ++j){ if(a[i][j]==1){ cout<<"■"; }else if(a[i][j]<0){ int temp = a[i][j]+6; cout<<s[temp]; }else{ cout<<" "; } } cout<<endl; } } system("pause"); } }

测试数据

9 8 0 0 1 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 1 1 1 0 0 1 1 1 0 0 1 0 0 0 0 1 0 0 0 0 0 1 0 0 0 1 0 1 0 1 1 1 1 0 0 1 1 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 运行结果:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要解决迷宫最短路径问题,可以使用广度优先搜索算法(BFS)来实现。BFS算法广泛应用于图形和树中的搜索问题,其中从起点开始扩展每个节点,并在扩展的所有节点上重复此过程,直到找到目标节点。 以下是一个使用C++语言实现5*5规格迷宫最短路径问题的示例代码: ```c++ #include <iostream> #include <queue> using namespace std; const int MAXN = 5; // 迷宫规格 int maze[MAXN][MAXN] = { // 迷宫地图 {0, 1, 0, 0, 0}, {0, 1, 0, 1, 0}, {0, 0, 0, 0, 0}, {0, 1, 1, 1, 0}, {0, 0, 0, 1, 0} }; int d[MAXN][MAXN]; // 到达每个点的最短距离 int dx[4] = {-1, 0, 1, 0}; // 方向数组 int dy[4] = {0, 1, 0, -1}; struct Node { int x, y; }; int bfs() { queue<Node> q; Node s = {0, 0}; // 起始节点 q.push(s); memset(d, -1, sizeof(d)); d[0][0] = 0; while (!q.empty()) { Node cur = q.front(); q.pop(); if (cur.x == MAXN-1 && cur.y == MAXN-1) break; // 到达终点 for (int i = 0; i < 4; ++i) { int nx = cur.x + dx[i]; int ny = cur.y + dy[i]; if (nx >= 0 && nx < MAXN && ny >= 0 && ny < MAXN && maze[nx][ny] == 0 && d[nx][ny] == -1) { // 可行的下一步 Node next = {nx, ny}; q.push(next); d[nx][ny] = d[cur.x][cur.y] + 1; } } } return d[MAXN-1][MAXN-1]; } int main() { cout << bfs() << endl; return 0; } ``` 在上面的代码中,我们使用了一个结构体`Node`来表示节点的坐标。首先将起始节点入队并将到达每个点的最短距离初始化为-1。在每次循环中,我们从队列中取出一个节点,并在其周围四个方向扩展节点。如果下一步是可行的,则将其入队,并将到达该节点的最短距离更新为当前节点的最短距离+1。 最后,我们返回到达终点的最短距离。对于上述的迷宫,该代码将输出9,即到达右下角的最短距离为9。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值