SLIKAR (BFS)
来源:互联网 发布:js 调用随机数 编辑:程序博客网 时间:2024/05/29 14:17
Problem Description
在一块 R×C 的矩形土地上,某天发起了洪水。洪水即将把整片土地淹没,于是 Beaver 不得不用最快的速度赶回老家,因为那里地势高,可以安全地避难。 在地图上,用“.”表示空地,用“D”表示老家的位置,用“S”表示Beaver 当前的位置,用“X”表示巨大的岩石,用“*”表示被洪水淹没的地区。每一个单位时间,Beaver 可以向上下左右四个方向移动一格,同样在已经有洪水的地区,洪水也能沿四个方向往外扩展一格。所有的岩石以及 Beaver 的老家都不会被水淹没,Beaver 也无法通过岩石阻碍的地方。任何一个时刻,都要保证Beaver不在洪水覆盖的区域。
现在请写一个程序,告诉Beaver最快要多少时间可以安全到达老家。
现在请写一个程序,告诉Beaver最快要多少时间可以安全到达老家。
Input
第一行给出两个正整数R和 C,均不超过50。
接下来R行,每行包含C个字符,描述了整个地图。其中“D”和“S”有且仅有一个。
接下来R行,每行包含C个字符,描述了整个地图。其中“D”和“S”有且仅有一个。
Output
输出所需要的最短时间,使 Beaver 可以安全到达老家。如果不存在这样的路,就输出“KAKTUS”。
Sample Input
3 3 D.* ... .S.
Sample Output
3
BFS,先预处理出,每个点被洪水淹没的时刻最小值,然后就直接bfs
#include<stdio.h>#include<string.h>#include<queue>#include<algorithm>using namespace std;struct node{ int x,y,t;};int dx[]={1,-1,0,0};int dy[]={0,0,-1,1};bool vis[55][55];char s[55][55];int r,c;int tl[55][55];int sx,sy,ex,ey;//预处理inline void bfs1(int x,int y){ queue<node>q; node vn,vw; vn.x=x; vn.y=y; vn.t=0; vis[x][y]=1; q.push(vn); while(!q.empty()) { vn=q.front(),q.pop(); tl[vn.x][vn.y]=min(tl[vn.x][vn.y],vn.t); for(int i=0;i<4;i++) { int a=vn.x+dx[i]; int b=vn.y+dy[i]; if(a>=0&&a<r&&b>=0&&b<c&&s[a][b]!='D'&&s[a][b]!='X'&&!vis[a][b]) { vis[a][b]=1; vw.x=a,vw.y=b,vw.t=vn.t+1; q.push(vw); } } }}inline void bfs2(int sx,int sy,int ex,int ey){ node vn,vw; queue<node>q; vn.x=sx,vn.y=sy,vn.t=0; q.push(vn); while(!q.empty()) { vn=q.front();q.pop(); if(vn.x==ex&&vn.y==ey) { printf("%d\n",vn.t); return; } else { for(int i=0;i<4;i++) { int a=vn.x+dx[i]; int b=vn.y+dy[i]; int t=vn.t+1; if(a>=0&&a<r&&b>=0&&b<c&&!vis[a][b]&&s[a][b]!='X') { if(t<=tl[a][b]) { vis[a][b]=1; vw.x=a,vw.y=b,vw.t=t; q.push(vw); } } } } } puts("KAKTUS");}int main(){ while(~scanf("%d%d",&r,&c)) { for(int i=0;i<r;i++) { scanf("%s",s[i]); } for(int i=0;i<r;i++) for(int j=0;j<c;j++) tl[i][j]=100; // printf("hh\n"); for(int i=0;i<r;i++) for(int j=0;j<c;j++) { if(s[i][j]=='*') { memset(vis,0,sizeof(vis)); vis[i][j]=1; bfs1(i,j); } else if(s[i][j]=='S') { sx=i,sy=j; } else if(s[i][j]=='D') { ex=i,ey=j; } } // printf("zz\n"); memset(vis,0,sizeof(vis)); vis[sx][sy]=1; bfs2(sx,sy,ex,ey); } return 0;}
- SLIKAR (BFS)
- F-SLIKAR 多校联合第七场 搜索问题
- BFS
- bfs
- BFS
- BFS
- bfs
- BFS
- bfs
- BFS
- BFS
- bfs
- BFS
- BFS
- BFS
- BFS
- BFS
- bfs
- SGU 223 Little Kings
- java中关于goto
- eclipse下测试JNI
- shell 脚本的基础知识
- Java多线程应用-condition的使用
- SLIKAR (BFS)
- 换行的问题
- shell之if简化语句
- spingMVC上传文件
- 链表的简单操作
- USACO1.3.4 Prime Cryptarithm(牛式)
- SOAP
- 为myeclipse 8.5 安装properties Editor插件
- CodeForces 29D - Ant on the Tree 暴力LCA