广度优先搜索 BFS I && 回顾--深度优先搜索

· · 个人记录

我们先来回顾一下深度优先搜索,先来看一张图。

               11 | . . . . . . . . . .
             10 | . . . . * . . . . . 
              9 | . . . . . . . . . . 
              8 | . . . * . * . . . . 
              7 | . . . . . . . * . . 
              6 | . . * . . * . . . H 
              5 | * . . . . . . . . . 
              4 | . . . * . . . * . . 
              3 | . K . . . . . . . . 
              2 | . . . * . . . . . * 
              1 | . . * . . . . * . . 
              0 ----------------------
                                    1 
                0 1 2 3 4 5 6 7 8 9 0 

假设我们要从 K 的位置走到 H 的位置,*为障碍物,.为空地。
首先我们先看能不能往上走,如果可以就走。若不能,则依次向左,右,下这几个方向看,如果还是不能,则退回去,否则继续向前走。

反正就是一句话: 不撞南墙不回头。

注意,在走的时候,要注意几个地方:

那我们先来看一道题:

D1203 迷宫

题目大意: 一天Extense在森林里探险的时候不小心走入了一个迷宫,迷宫可以看成是由 n \times n的格点组成,每个格点只有2种状态,.和#,前者表示可以通行后者表示不能通行。同时当Extense处在某个格点时,他只能移动到东南西北(或者说上下左右)四个方向之一的相邻格点上,Extense想要从点 A 走到点 B ,问在不走出迷宫的情况下能不能办到。如果起点或者终点有一个不能通行(为 # ),则看成无法办到。

解题思路:

深度优先搜索解法:

1.在走的时候,要注意几个地方:

2.走动的时候,要根据偏移数组来求位置,数组求法如下:

||-1,0|| |:-:|:-:|:-:| |0,-1|0,0|0,1| ||1,0||

在定义偏移数组时,dx 和 dy 要一一对应,不能拆开,因此数组定义为如下:

int dx[]={0,-1,1,0,0};
int dy[]={0,0,0,-1,1};

3.我们要注意到题目中的一句话:
-->注意到ha, la, hb, lb全部是从0开始计数的。
而我们输入时是按从下标 1-n 输入的,所以需要偏移位置,操作为:

ha++,la++,hb++,lb++;

4.起始位置和终止位置考虑
如果起始位置或终止位置有一个点有障碍物,那么就永远走不到,输出无解。

#include<bits/stdc++.h>
using namespace std;
const int maxn=1005;
char a[maxn][maxn];
int vis[maxn][maxn];
int dx[]={0,-1,1,0,0};//偏移数组一定不能乱配对
int dy[]={0,0,0,-1,1};
int n;
bool flag=false;//废物
void dfs(int posx,int posy,int yyy1,int yyy2)
{
    for(int i=1;i<=4;i++)
    {
        int nx=dx[i]+posx;
        int ny=dy[i]+posy;
        if(nx>=1&&nx<=n&&ny>=1&&ny<=n&&vis[nx][ny]==false&&a[nx][ny]!='#')//
        {
            vis[nx][ny]=true;//这个格子被走过了!
            dfs(nx,ny,yyy2,yyy2);//往下一个格子走
        }
    }
}
void work()
{
    flag=false;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            cin>>a[i][j];
            vis[i][j]=false;//多测必清空
        }
    }
    int ha,la,hb,lb;
    cin>>ha>>la>>hb>>lb;
    ha++,la++,hb++,lb++;//位置偏移,同下标
    if(a[ha][la]=='#'||a[hb][lb]=='#')//但凡起始或终止位置有障碍物一概输出无解
    {
        cout<<"NO\n";
        return ; 
    }
    vis[ha][la]=true;//起始位置要标记,否则会再次走到起点
    dfs(ha,la,hb,lb);
    if(vis[hb][lb]==true) cout<<"YES\n";
    else cout<<"NO\n"; 
    return ;
}
signed main()
{
    int k;
    cin>>k;
    while(k--)
    {
        work();
    }
    return 0;
} 

广度优先搜索解法:

假设起始位置是 (2,3) ,终止位置是 (4,3) ,我们就可以先找到一步能走到的地方,然后做好标记。接着考虑走两步能走到的地方。注意:在这里不需要从起点开始搜索,只要在一步能走到的地方进行扩散操作即可,也就是把一步能走到的地方看成起点,求走一步能走到的地方。以此类推.....

不过还是要遵循之前的原则:

这样只要在扩散的时候找到了终点,就输出有解。 如图:

广搜时的要点

这里的 t 是扩散点的位置

#include<bits/stdc++.h>
using namespace std;
#define int long long
const int maxn=1005;
int dx[]={1,0,-1,0},dy[]={0,1,0,-1};
char a[maxn][maxn];
int n;
int vis[maxn][maxn];
struct node
{
    int x,y;
};
int bfs(int posx,int posy,int yyy1,int yyy2)
{
    if(a[posx][posy]=='#'||a[yyy1][yyy2]=='#')//起始,终止位置考虑
    {
        return -1;//-1就代表无解
    }
    queue<node>q;
    vis[posx][posy]=true;//起点一开始就走过
    q.push({posx,posy});//起始位置
    while(q.empty()==false)
    {
        node t=q.front();
        q.pop();
        if(t.x==yyy1&&t.y==yyy2)//是终点就不用扩散
        {
            return 1;
        }

        for(int i=0;i<4;i++)
        {
            int nx=t.x+dx[i];//偏移位置
            int ny=t.y+dy[i];//偏移位置
            if(nx>=1&&nx<=n&&ny>=1&&ny<=n&&vis[nx][ny]==false&&a[nx][ny]!='#')//判断格子是否能走
            {
                vis[nx][ny]=true;
                q.push({nx,ny});//装进队列,因为发现的这个点即将会扩散
            }
        }
    }
    return -1;
}
void work()
{
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            cin>>a[i][j];
            vis[i][j]=false;//清空
        } 
    } 
    int ha,la,hb,lb;
    cin>>ha>>la>>hb>>lb;
    ha++,la++;
    hb++,lb++;
    int ans=bfs(ha,la,hb,lb);//传参数至广搜函数BFS
    if(ans==-1) cout<<"NO\n";
    else cout<<"YES\n";
    return ;
}
signed main()
{
    int k;
    cin>>k;
    while(k--)
    {
        work();
    }
    return 0;
}

D1227 仙岛求药

题目大意:少年李逍遥的婶婶病了,王小虎介绍他去一趟仙灵岛,向仙女姐姐要仙丹救婶婶。叛逆但孝顺的李逍遥闯进了仙灵岛,克服了千险万难来到岛的中心,发现仙药摆在了迷阵的深处。迷阵由 M×N 个方格组成,有的方格内有可以瞬秒李逍遥的怪物,而有的方格内则是安全。现在李逍遥想尽快找到仙药,显然他应避开有怪物的方格,并经过最少的方格,而且那里会有神秘人物等待着他。现在要求你来帮助他实现这个目标。

下图 显示了一个迷阵的样例及李逍遥找到仙药的路线。*其中 M 和 N 均小于 20 。

BFS解法: 跟上一题基本一样,这里就只讲一下不同点。在这题需要求步数,所以结构体里只需多放一个步数变量 step ,每次加一即可。然后就是输入完之后需要自己找起点和终点,其它的跟上一题一样。

#include<bits/stdc++.h>

using namespace std;
#define int long long

const int maxn=1005;

struct node
{
    int x,y;

    int step;
};

char a[maxn][maxn];
int vis[maxn][maxn];

int dx[]={1,0,-1,0};
int dy[]={0,1,0,-1};
int n,m;

bool check(int ax,int ay)
{
    if(ax<1)
    {
        return true;
    }

    if(ay<1)
    {
        return true;
    }

    if(ax>n)
    {
        return true;
    }

    if(ay>m)
    {
        return true;
    }

    if(vis[ax][ay]==true)
    {
        return true;
    }

    if(a[ax][ay]=='#')
    {
        return true;
    }

    return false;

}

int bfs(int posx,int posy,int yyy1,int yyy2)
{

    if(a[posx][posy]=='#')
    {
        return -1;
    }
    if(a[yyy1][yyy2]=='#')
    {
        return -1;
    }

    queue<node>q;

    vis[posx][posy]=false;

    q.push({posx,posy,0});

    while(q.empty()==false)
    {

        node t=q.front();

        if(t.x==yyy1)
        {

            if(t.y==yyy2)
            {
                return t.step;
            }

        }

        q.pop();

        for(int i=0;i<4;i++)
        {
            int nx=dx[i]+t.x;

            int ny=dy[i]+t.y;

            if(check(nx,ny))
            {
                continue;
            }

            vis[nx][ny]=true;

            q.push({nx,ny,t.step+1});

        }
    }

    return -1;

}

void work(int r,int c)
{

    for(int i=1;i<=r;i++)
    {
        for(int j=1;j<=c;j++)
        {

            cin>>a[i][j];

            vis[i][j]=false;

        } 
    }
    //起始 
    int qx;
    int qy;
    //终止 
    int zx;
    int zy;

    for(int i=1;i<=r;i++)
    {
        for(int j=1;j<=c;j++)
        {
            if(a[i][j]=='@')
            {
                qx=i;
                qy=j;
                break;
            }
        }
    }

    for(int i=1;i<=r;i++)
    {
        for(int j=1;j<=c;j++)
        {
            if(a[i][j]=='*')
            {
                zx=i;
                zy=j;
                break;
            }
        }
    }

    cout<<bfs(qx,qy,zx,zy)<<"\n";

    return ;

}
signed main()
{

    while(cin>>n>>m)
    {

        if(n==0)
        {
            if(m==0)
            {
                break;
            }
        }

        work(n,m);
    }

    return 0;
}