 Problem Description
 Problem Description Input
 Input有多组测试数据。
每组测试数据第一行是一个整数T,代表接下去的例子数。(0<=T<=10)
接下来是T组例子。
每组例子第一行是两个整数N和M。代表迷宫的大小有N行M列(0<=N,M<=1000)。
接下来是一个N*M的迷宫描述。
S代表小明的所在地。
E代表出口,出口只有一个。
.代表可以行走的地方。
!代表岩浆的产生地。(这样的地方会有多个,其个数小于等于10000)
#代表迷宫中的墙,其不仅能阻挡小明前进也能阻挡岩浆的蔓延。
小明携带者宝藏每秒只能向周围移动一格,小明不能碰触到岩浆(小明不能和岩浆处在同一格)。
岩浆每秒会向四周不是墙的地方蔓延一格。
小明先移动完成后,岩浆才会蔓延到对应的格子里。
小明能移动到出口,则小明顺利逃脱。
 Output
 Output Sample Input
 Sample Input Sample Output
 Sample Output Source
 Source#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#include <queue>
#define MAXN 1e9+1
int dir[4][2]= {{0,1},{0,-1},{1,0},{-1,0}};
char mat[1015][1015];
int pre[1015][1015];
int flag[1015][1015];
int n,m;
bool check(int x,int y)
{
    if(x<0 ||y<0 ||x>=n ||y>=m ||mat[x][y]=='#')
        return 0;
    return 1;
}
struct node
{
    int x,y;
    int step;
};
queue<node>q;
queue<node>Q;
void bfs1()
{
    memset(pre,-1,sizeof(pre));
    node f,st;
    for(int i=0; i<n; i++)
        for(int j=0; j<m; j++)
        {
            if(mat[i][j]=='!')
            {
                pre[i][j]=0;
                f.x=i;
                f.y=j;
                f.step=0;
                q.push(f);
            }
        }
    int dx,dy;
    while(!q.empty())
    {
        f=q.front();
        q.pop();
        for(int i=0; i<4; i++)
        {
            dx=f.x+dir[i][0];
            dy=f.y+dir[i][1];
            if(check(dx,dy)&&pre[dx][dy]==-1)
            {
                pre[dx][dy]=f.step+1;
                st.x=dx,st.y=dy,st.step=pre[dx][dy];
                q.push(st);
            }
        }
    }
    return;
}
int bfs2()
{
    node st,f;
    memset(flag,0,sizeof(flag));
    for(int i=0; i<n; i++)
        for(int j=0; j<m; j++)
        {
            if(mat[i][j]=='S')
            {
                flag[i][j]=1;
                f.x=i;
                f.y=j;
                f.step=0;
            }
        }
    Q.push(f);
    int dx,dy;
    while(!Q.empty())
    {
        f=Q.front();
        Q.pop();
        for(int i=0; i<4; i++)
        {
            dx=f.x+dir[i][0];
            dy=f.y+dir[i][1];
            if(check(dx,dy)&&flag[dx][dy]==0 &&pre[dx][dy]>=f.step+1)
            {
                flag[dx][dy]=1;   //千万别写成==
                if(mat[dx][dy]=='E')
                    return 1;
                else if(pre[dx][dy]!=f.step+1)
                {
                    st.x=dx,st.y=dy,st.step=f.step+1;
                    Q.push(st);
                }
            }
        }
    }
    return 0;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        while(!q.empty())
            q.pop();
        while(!Q.empty())
            Q.pop();
        scanf("%d%d",&n,&m);
        for(int i=0; i<n; i++)
            scanf("%s",mat[i]);
        bfs1();
        if(bfs2())
            printf("Yes\n");
        else
            printf("No\n");
    }
    return 0;
}
原文:http://blog.csdn.net/sky_miange/article/details/45599349