Description
Input
有多组测试数据。
每组测试数据第一行是一个整数T,代表接下去的例子数。(0<=T<=10)
接下来是T组例子。
每组例子第一行是两个整数N和M。代表迷宫的大小有N行M列(0<=N,M<=1000)。
接下来是一个N*M的迷宫描述。
S代表小明的所在地。
E代表出口,出口只有一个。
.代表可以行走的地方。
!代表岩浆的产生地。(这样的地方会有多个,其个数小于等于10000)
#代表迷宫中的墙,其不仅能阻挡小明前进也能阻挡岩浆的蔓延。
小明携带者宝藏每秒只能向周围移动一格,小明不能碰触到岩浆(小明不能和岩浆处在同一格)。
岩浆每秒会向四周不是墙的地方蔓延一格。
小明先移动完成后,岩浆才会蔓延到对应的格子里。
小明能移动到出口,则小明顺利逃脱。
Output
Sample Input
Sample Output
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<string>
#include<cmath>
#include<queue>
#include<vector>
#include<map>
#include<set>
#define INF 0x3f3f3f3f
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
const int maxd=1000+5;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1 | 1
typedef long long ll;
typedef pair<int,int> pii;
//---------------------------
int dx[]= {0,0,1,-1};
int dy[]= {1,-1,0,0};
typedef struct node
{
    int x,y,t;
     node(int x_ = 0, int y_ = 0, int t_ = 0){
        x = x_;
        y = y_;
        t = t_;
    }
};
int n,m,stx,sty,enx,eny;
int vis[maxd][maxd],mz[maxd][maxd];
queue<node> f;
bool ok(int x,int y)
{
    if(x<0 || y<0 || x>=n ||y>=m) return false;
    return true;
}
void init()
{
    cin>>n>>m;
    for(int i=0; i<=n; ++i)
        for(int j=0; j<=m; ++j)
            mz[i][j]=-1;
    mem(vis,0);
    for(int i=0; i<n; ++i)
        for(int j=0; j<m; ++j)
        {
            char ch;
            cin>>ch;
            if(ch=='.')
                mz[i][j]=0;
            else if(ch=='!')
            {
                 mz[i][j]=0;
                 node tmp(i,j,0);
                 f.push(tmp),vis[i][j]=1;
            }
            else if(ch=='S')
                stx=i,sty=j,mz[i][j]=0;
            else if(ch=='E')
                enx=i,eny=j,mz[i][j]=0;
        }
    while(!f.empty())
    {
        node now=f.front();
        f.pop();
        for(int i=0; i<4; ++i)
        {
            int xx=now.x+dx[i];
            int yy=now.y+dy[i];
            int tt=now.t+1;
            if(ok(xx,yy) && mz[xx][yy]!=-1 && vis[xx][yy]==0)
            {
                if(mz[xx][yy]==0 || mz[xx][yy]> tt)
                {
                    mz[xx][yy]=tt;
                    vis[xx][yy]=1;
                    node tmp(xx,yy,tt);
                    f.push(tmp);
                }
            }
        }
    }
  mem(vis,0);
    for(int i=0; i<n; ++i)
    {
        for(int j=0; j<m; ++j)
            cout<<mz[i][j]<<' ';
        cout<<endl;
    }
}
void bfs(int x,int y,int t)
{
    queue<node> q;
    node tmp(x,y,t);
    q.push(tmp);
    vis[x][y]=1;
    while(!q.empty())
    {
        node now=q.front();
        q.pop();
        if(now.x==enx && now.y==eny)
        {
            cout<<"Yes"<<endl;
            return;
        }
        for(int i=0; i<4; ++i)
        {
            int xx=now.x+dx[i];
            int yy=now.y+dy[i];
            int tt=now.t+1;
            if(ok(xx,yy) && vis[xx][yy]==0 && mz[xx][yy]!=-1)
            if(tt<mz[xx][yy])
            {
                node tmp(xx,yy,tt);
                q.push(tmp);
                vis[xx][yy]=1;
            }
            else if(tt==mz[xx][yy] && xx==enx && yy==eny)
            {
                cout<<"Yes"<<endl;
                return;
            }
        }
    }
    cout<<"No"<<endl;
    return;
}
int main()
{
    int kase;
    freopen("1.txt","r",stdin);
    cin>>kase;
    while(kase--)
    {
        init();
        bfs(stx,sty,0);
    }
    return 0;
}
原文:http://blog.csdn.net/whoisvip/article/details/45564871