BFS【模板】-解救小哈

标题:解救小哈
标签:搜索
详情:有一天,小哈一个去玩迷宫。但是方向感很不好的小哈很快就迷路了。小哼得知后便立即去解救无助的小哈。小哼当然是有备而来,已经弄清楚了迷宫地图,现在小哼要以最快速度去解救小哈。问题就此开始了……
  迷宫由n行m列的单元格组成,每个单元格要么是空地,要么是障碍物。你的任务是帮助小哼找到一条从迷宫的起点到小哈所在位置的最短路径,注意障碍物是不能走的,当然也不能走到迷宫之外。n和m都小于等于100。
输入格式:
第一行有两个数N M。N表示迷宫的行,M表示迷宫的列。接来下来N行M列为迷宫,0表示空地,1表示障碍物。最后一行4个数,前两个数为迷宫入口的x和y坐标。后两个为小哈的x和y坐标。
输出格式:
一个整数表示小哼到小哈的最短步数。如果不能解救小哈则输出No Way!
提示:RQNOJ 34紧急援救
195校园迷宫
样例:

输入

5 4
0 0 1 0
0 0 0 0
0 0 1 0
0 1 0 0
0 0 0 1
1 1 4 3

输出

7

输入

3 3
1 1 1
0 1 0
0 1 0
2 1 3 3

输出

No Way!

题解:用DFS会超时,不知道为什么。。。BFS稳过

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
#define INF 99999999
using namespace std;
typedef struct Node{
    int x,y;//当前坐标
    int step;//步数
}node;
const int maxn=105;
int minx=INF;
int n,m;
int sx,sy,hx,hy;
int a[105][105];
int vis[maxn][maxn];
int dir[4][2]={0,1,-1,0,0,-1,1,0};//四个方向
void bfs(int x,int y){
    node p,q;
    p.x=x;
    p.y=y;
    p.step=0;
    queue<node> pq;
    pq.push(p);
    while(!pq.empty()){
        p=pq.front();
        pq.pop();
        if(p.x==hx&&p.y==hy){
            if(p.step<minx){//可能存在多条路径,要找其中最短的那条路。
                minx=p.step;
            }
        }
        for(int i=0;i<4;i++){
            q.x=p.x+dir[i][0];
            q.y=p.y+dir[i][1];
            q.step=p.step+1;
            if(q.x<1||q.x>n||q.y<1||q.y>m)//判断是否越界
                continue;
            if(!vis[q.x][q.y]&&a[q.x][q.y]!=1){//如果这个点没有走过或者这个点不是障碍物,则可以走
                vis[q.x][q.y]=1;
                pq.push(q);
            }
        }
    }
}
int main()
{
    scanf("%d %d",&n,&m);
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            scanf("%d",&a[i][j]);
        }
    }
    scanf("%d %d %d %d",&sx,&sy,&hx,&hy);
    vis[sx][sy]=1;
    bfs(sx,sy);
    if(minx==INF) printf("No Way!
");
    else printf("%d
",minx);
    return 0;
}

以下为用DFS做的,40分,还有几个测试点超时。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define INF 9999999
using namespace std;
const int maxn=105;
int n,m;
int dir[4][2]={0,1,-1,0,0,-1,1,0};
int a[maxn][maxn];
int vis[maxn][maxn];
int ans=0,minx=INF;
int hx,hy;
void dfs(int x,int y,int step){
    if(x==hx&&y==hy){
        if(step<minx)
            minx=step;
        return ;
    }
    for(int i=0;i<4;i++){
        int tx=x+dir[i][0];
        int ty=y+dir[i][1];
        if(tx>=1&&tx<=n&&ty>=1&&ty<=m&&!vis[tx][ty]&&a[tx][ty]!=1){
            ans++;
            vis[tx][ty]=1;
            dfs(tx,ty,step+1);
            vis[tx][ty]=0;
        }
    }
    return ;
}
int main()
{
    int sx,sy;
    scanf("%d %d",&n,&m);
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            scanf("%d",&a[i][j]);
        }
    }
    scanf("%d %d %d %d",&sx,&sy,&hx,&hy);
    memset(vis,0,sizeof vis);
    vis[sx][sy]=1;
    dfs(sx,sy,0);
    if(minx!=INF) printf("%d
",minx);
    else printf("No Way!
");
    return 0;
}

原文地址:https://www.cnblogs.com/kzbin/p/9205239.html