FZU_2196_Escape(BFS)

来源:互联网 发布:黑客帝国3矩阵革命 编辑:程序博客网 时间:2024/05/18 05:22

传送门:http://acm.fzu.edu.cn/problem.php?pid=2196


题型:搜索


题意:中文题,不解释


分析:

       先将所有的岩浆产生点压入队列,然后进行BFS,不断更新岩浆到达每个点的最早时间,#区域默认岩浆到达时间为0,获得一个新的地图。

       然后从S搜到E,只有在图中为走过且到达时间小于等于岩浆到达该点最早时间的结点为合法扩展结点。

       特殊情况需要注意:当到达E点时,即使岩浆也同时到达,也记为Yes

                                        当N或者M为0时,记为No


代码:

#include<iostream>#include<cmath>#include<cstring>#include<cstdio>#include<queue>#define mt(a,b) memset(a,b,sizeof(a))using namespace std;const int inf = 0x3f3f3f3f;const int M = 1234;struct Node{    int x,y;    int step;}s,e;int n,m;char Map[M][M];int dist[M][M];bool vis[M][M];queue<Node> q;int dir1[] = {0,0,1,-1};int dir2[] = {1,-1,0,0};bool inside(Node ts){    if(ts.x>=0&&ts.x<n && ts.y>=0&&ts.y<m) return true;    return false;}void bfs_get(){    mt(dist,inf);    while(!q.empty()) q.pop();    Node magma;    for(int i=0;i<n;i++){        for(int j=0;j<m;j++){            if(Map[i][j] == 'S'){                s.x = i;                s.y = j;                s.step = 0;                continue;            }            if(Map[i][j] == 'E'){                e.x = i;                e.y = j;                continue;            }            if(Map[i][j] == '!'){                magma.x = i;                magma.y = j;                magma.step = 0;                q.push(magma);                dist[i][j] = 0;                continue;            }            if(Map[i][j] == '#'){                dist[i][j] = 0;            }        }    }    Node now,pre;    while(!q.empty()){        pre = q.front();        q.pop();        for(int i=0;i<4;i++){            now.x = pre.x + dir1[i];            now.y = pre.y + dir2[i];            now.step = pre.step + 1;            if(inside(now) && now.step<dist[now.x][now.y]){                dist[now.x][now.y] = now.step;                q.push(now);            }        }    }}bool bfs(){    while(!q.empty()) q.pop();    mt(vis,false);    Node pre,now;    vis[s.x][s.y] = true;    q.push(s);    while(!q.empty()){        pre = q.front();        q.pop();        if(pre.x==e.x && pre.y==e.y) return true;        for(int i=0;i<4;i++){            now.x = pre.x + dir1[i];            now.y = pre.y + dir2[i];            now.step = pre.step + 1;            if(now.x==e.x && now.y==e.y && now.step<=dist[now.x][now.y]) return true;            if(inside(now)&&!vis[now.x][now.y]&&now.step<dist[now.x][now.y]){                q.push(now);                vis[now.x][now.y] = true;            }        }    }    return false;}int main(){    int _;    while(~scanf("%d",&_)){        while(_--){            scanf("%d%d",&n,&m);            if(n==0 || m==0){                puts("No");                continue;            }            for(int i=0;i<n;i++){                scanf("%s",Map[i]);            }            bfs_get();//            for(int i=0;i<n;i++){//                for(int j=0;j<m;j++){//                    printf("%d ",dist[i][j]);//                }//                puts("");//            }            puts(bfs()?"Yes":"No");        }    }    return 0;}/**35 5....!S....#....!#...#E...2 2S.!E2 2SE!.*/


0 0
原创粉丝点击