二维迷宫bfs+回溯路径

问题描述

东东有一张地图,想通过地图找到妹纸。地图显示,0表示可以走,1表示不可以走,左上角是入口,右下角是妹纸,这两个位置保证为0。既然已经知道了地图,那么东东找到妹纸就不难了,请你编一个程序,写出东东找到妹纸的最短路线。

Input

输入是一个5 × 5的二维数组,仅由0、1两数字组成,表示法阵地图。

Output

输出若干行,表示从左上角到右下角的最短路径依次经过的坐标,格式如样例所示。数据保证有唯一解。

Sample Input

0 1 0 0 0
0 1 0 1 0
0 1 0 1 0
0 0 0 1 0
0 1 0 1 0

Sample Output

(0, 0)
(1, 0)
(2, 0)
(3, 0)
(3, 1)
(3, 2)
(2, 2)
(1, 2)
(0, 2)
(0, 3)
(0, 4)
(1, 4)
(2, 4)
(3, 4)
(4, 4)

Hint

坐标(x, y)表示第x行第y列,行、列的编号从0开始,且以左上角为原点。
另外注意,输出中分隔坐标的逗号后面应当有一个空格。

解题思路

这是一个很基本的搜索问题,直接在原点进行bfs暴力搜索即可。但是这道题多了一个输出路径的步骤,我们可以使用path[i][j]数组来记录当前 ( i , j ) (i,j) 节点的上一个节点。
下面这行代码是一个简单的哈希方法,由于我们已经知道地图是 5 × 5 5\times 5 的矩阵,则可以通过这行代码,将坐标转换成一个二位数存到数组中。

path[nx][ny]=point.x*10+point.y;

输出路径的代码如下所示,就是一个递归程序,在回溯的时候输出路径即可。需要注意的一点是回溯到原点时,需要特殊处理一下。

void findthepath(int x,int y)
{
    int px=path[x][y]/10,py=path[x][y]%10;//拆出坐标
    if(px==0 && py==0)
    {
        cout<<"("<<px<<", "<<py<<")"<<endl;//最开始两层在回溯的时候并没有输出,需要在此处手动输出
        cout<<"("<<x<<", "<<y<<")"<<endl;
        return;
    }
    findthepath(px,py);
    cout<<"("<<x<<", "<<y<<")"<<endl;
}

完整代码

//#pragma GCC optimize(2)//比赛禁止使用!
//#pragma G++ optimize(2)
//#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <climits>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;

const int maxn=10;
int a[maxn][maxn],path[maxn][maxn];
int vis[maxn][maxn];//-1表示没有到过,其他的值是到达此处需要的距离
int dx[]={-1,1,0,0};
int dy[]={0,0,-1,1};
int sx,sy,tx,ty;
struct node
{
    int x,y;
};
queue<node> q;
void bfs()
{
    q.push({sx,sy});
    memset(vis,-1,sizeof(vis));
    vis[sx][sy]=0;//到达过
    while(!q.empty())
    {
        node point=q.front(); q.pop();
        if(point.x==tx && point.y==ty) break;//找到终点
        for (int i=0; i<4; i++)
        {
            int nx=point.x+dx[i],ny=point.y+dy[i];
            if(nx>=0 && nx<=4 && ny>=0 && ny<=4 && a[nx][ny]==0 && vis[nx][ny]==-1)//新的点没有访问过
            {
                path[nx][ny]=point.x*10+point.y;//将到达path[nx][ny]的地方存储,一种简单的哈希方法
                vis[nx][ny]=vis[point.x][point.y]+1;
                q.push({nx,ny});
            }
        }
    }
}
void findthepath(int x,int y)
{
    int px=path[x][y]/10,py=path[x][y]%10;
    if(px==0 && py==0)
    {
        cout<<"("<<px<<", "<<py<<")"<<endl;//最开始两层在回溯的时候并没有输出,需要在此处手动输出
        cout<<"("<<x<<", "<<y<<")"<<endl;
        return;
    }
    findthepath(px,py);
    cout<<"("<<x<<", "<<y<<")"<<endl;
}
void output()//用于观察两个矩阵,调试用
{
    for (int i=0; i<=4; i++)
    {
        for (int j=0; j<=4; j++)
            cout<<path[i][j]<<' ';
        cout<<endl;
    }
    cout<<endl;
    for (int i=0; i<=4; i++)
    {
        for (int j=0; j<=4; j++)
            cout<<vis[i][j]<<' ';
        cout<<endl;
    }
}
int getint()//快读,切记不可与cin关同步使用
{
    int x=0,s=1;
    char ch=' ';
    while(ch<'0' || ch>'9')
    {
        ch=getchar();
        if(ch=='-') s=-1;
    }
    while(ch>='0' && ch<='9')
    {
        x=x*10+ch-'0';
        ch=getchar();
    }
    return x*s;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    sx=0; sy=0; tx=4; ty=4;
    for (int i=0; i<=4; i++)
        for (int j=0; j<=4; j++)
            cin>>a[i][j];
    bfs();
    //output();
    findthepath(tx,ty);
    return 0;
}
发布了32 篇原创文章 · 获赞 24 · 访问量 2245

猜你喜欢

转载自blog.csdn.net/weixin_43347376/article/details/104655650