Rescue (BFS)

Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison.

Angel's friends want to save Angel. Their task is: approach Angel. We assume that "approach Angel" is to get to the position where Angel stays. When there's a guard in the grid, we must kill him (or her?) to move into the grid. We assume that we moving up, down, right, left takes us 1 unit time, and killing a guard takes 1 unit time, too. And we are strong enough to kill all the guards.

You have to calculate the minimal time to approach Angel. (We can move only UP, DOWN, LEFT and RIGHT, to the neighbor grid within bound, of course.)


Input
First line contains two integers stand for N and M.

Then N lines follows, every line has M characters. "." stands for road, "a" stands for Angel, and "r" stands for each of Angel's friend.

Process to the end of the file.

Output
For each test case, your program should output a single integer, standing for the minimal time needed. If such a number does no exist, you should output a line containing "Poor ANGEL has to stay in the prison all his life."

Sample Input
7 8
#.#####.
#.a#..r.
#..#x...
..#..#.#
#...##..
.#......
........

Sample Output
13

这个题并不是一个完全的BFS模板题,到达目的地的布数最少并不代表时间最短,网上大部分题解都使用了

BFS+优先队列的做法这里主要给大家说另一种做法:记忆化搜索,这里不再用一个vit数组标记该点是否被搜索过

而是记录一下搜到的点所用最短时间,如果再次搜到该点且比之前到该点时间长就不再入队。

#include <iostream>
#include<algorithm>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<deque>
#include<queue>
#include<vector>
#include<stack>
#include<ctime>
using namespace std;
struct A{
    int a,b,c;
}start,end1;
int main()
{
    int n,m;
    while(cin>>n>>m!=NULL)
    {
        deque<A>p;
        char a[250][250];
        int f[10000],l=0,b[250][250];
        memset(b,0x3f3f3f3f,sizeof(b));
        memset(a,0,sizeof(a));
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                cin>>a[i][j];
                if(a[i][j]=='r')//开始的位置
                {
                    start.a=i;
                    start.b=j;
                    start.c=0;
                    b[i][j]=0;
                    p.push_front(start);


                }
                if(a[i][j]=='a')//要到的位置
                {
                    end1.a=i;
                    end1.b=j;

                }

            }
        }
        while(!p.empty())
        {
            A d;
            d=p.front();
            p.pop_front();
            if(d.b+1<m&&a[d.a][d.b+1]!='#')
            {
                A c;
                c.a=d.a;
                c.b=d.b+1;
                c.c=d.c+1;
                if(a[d.a][d.b+1]=='x')
                    c.c++;
                    if(c.c<b[d.a][d.b+1])//此次到达该点所用时间比之前短,入队,否则不再入队;
                p.push_back(c),
                b[d.a][d.b+1]=c.c;
            }/*下面三个if与上面相同只是搜索的不同方向*/
            if(d.b-1>=0&&a[d.a][d.b-1]!='#')
            {
                A c;
                c.a=d.a;
                c.b=d.b-1;
                c.c=d.c+1;
                if(a[d.a][d.b-1]=='x')
                    c.c++;
                if(c.c<b[d.a][d.b-1])
                p.push_back(c),
                b[d.a][d.b-1]=c.c;
            }
            if(d.a+1<n&&a[d.a+1][d.b]!='#')
            {
                A c;
                c.a=d.a+1;
                c.b=d.b;
                c.c=d.c+1;
                if(a[d.a+1][d.b]=='x')
                    c.c++;
                if(c.c<b[d.a+1][d.b])
                p.push_back(c),
                b[d.a+1][d.b]=c.c;
            }
            if(d.a-1>=0&&a[d.a-1][d.b]!='#')
            {
                A c;
                c.a=d.a-1;
                c.b=d.b;
                c.c=d.c+1;
                if(a[d.a-1][d.b]=='x')
                    c.c++;
               if(c.c<b[d.a-1][d.b])
                p.push_back(c),
                b[d.a-1][d.b]=c.c;
            }
        }
        if(b[end1.a][end1.b]<0x3f3f3f3f)
            cout<<b[end1.a][end1.b]<<endl;
        else
            cout<<"Poor ANGEL has to stay in the prison all his life."<<endl;
    }

}
/*7 8
#.#####.
#.a#..r.
#..#x...
..#..#.#
#...##..
.#......
........*/

猜你喜欢

转载自blog.csdn.net/qq_41232172/article/details/81091610
BFS