UVA 11624 Fire!
来源:互联网 发布:python 验证码识别库 编辑:程序博客网 时间:2024/06/08 05:27
Given Joe's location in the maze and which squares of the maze are on fire, you must determine whether Joe can exit the maze before the fire reaches him, and how fast he can do it.
Joe and the fire each move one square per minute, vertically or horizontally (not diagonally). The fire spreads all four directions from each square that is on fire. Joe may exit the maze from any square that borders the edge of the maze. Neither Joe nor the fire may enter a square that is occupied by a wall.
Input Specification
The first line of input contains a single integer, the number of test cases to follow. The first line of each test case contains the two integers R and C, separated by spaces, with 1 <= R,C <= 1000. The following R lines of the test case each contain one row of the maze. Each of these lines contains exactly C characters, and each of these characters is one of:
- #, a wall
- ., a passable square
- J, Joe's initial position in the maze, which is a passable square
- F, a square that is on fire
There will be exactly one J in each test case.
Sample Input
24 4#####JF##..##..#3 3####J.#.F
Output Specification
For each test case, output a single line containing IMPOSSIBLE if Joe cannot exit the maze before the fire reaches him, or an integer giving the earliest time Joe can safely exit the maze, in minutes.
Output for Sample Input
3IMPOSSIBLE
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<queue>using namespace std;const int MAXN=1005;const int INF=0x3f3f3f3f;int n,m;int sx,sy;int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};char map[MAXN][MAXN];int vis[MAXN][MAXN];int visp[MAXN][MAXN];int T[MAXN][MAXN];struct node{ int x,y,s;};queue<struct node>q;void BFS_Fire(){ int i; while(!q.empty()) { struct node t; t=q.front(); q.pop(); for(i=0;i<=3;i++) { int xx=t.x+dir[i][0]; int yy=t.y+dir[i][1]; if(xx>=0&&xx<=n-1&&yy>=0&&yy<=m-1&&!vis[xx][yy]&&map[xx][yy]!='#') { vis[xx][yy]=1; struct node f; f.x=xx; f.y=yy; f.s=t.s+1; T[xx][yy]=f.s; q.push(f); } } }}int BFS_Man(){ queue<struct node>qq; while(!q.empty()) q.pop(); struct node s; s.x=sx; s.y=sy; s.s=0; qq.push(s); visp[sx][sy]=1; while(!qq.empty()) { int i; struct node t; t=qq.front(); qq.pop(); if(t.x==0||t.x==n-1||t.y==0||t.y==m-1) return t.s+1; for(i=0;i<=3;i++) { int xx=t.x+dir[i][0]; int yy=t.y+dir[i][1]; //if(xx==0||xx==n-1||yy==0||yy==m-1) return t.s+1; if(t.s+1>=T[xx][yy]) continue; if(xx>=0&&xx<=n-1&&yy>=0&&yy<=m-1&&!visp[xx][yy]&&map[xx][yy]!='#') { visp[xx][yy]=1; struct node f; f.x=xx; f.y=yy; f.s=t.s+1; qq.push(f); } } } return -1;}int main(){ int t; scanf("%d",&t); while(t--) { while(!q.empty()) q.pop(); memset(vis,0,sizeof(vis)); memset(visp,0,sizeof(visp)); memset(T,INF,sizeof(T)); int i,j; scanf("%d%d",&n,&m); for(i=0;i<=n-1;i++) { scanf("%s",map[i]); } for(i=0;i<=n-1;i++) { for(j=0;j<=m-1;j++) { if(map[i][j]=='J') { sx=i; sy=j; } if(map[i][j]=='F') { struct node t; t.x=i; t.y=j; t.s=0; vis[i][j]=1; T[i][j]=0; q.push(t); } } } BFS_Fire(); int ans=BFS_Man(); if(ans==-1) printf("IMPOSSIBLE\n"); else printf("%d\n",ans); } return 0;}
- UVa 11624 - Fire!
- UVA 11624 - Fire!
- Uva-11624-Fire!
- uva 11624 Fire!
- uva 11624 - Fire!
- UVa 11624 - Fire!
- UVa 11624 Fire!
- UVA 11624 BFS Fire
- UVa Fire! 11624 (BFS)
- UVA - 11624 Fire!
- uva 11624 - Fire! bfs
- UVA 11624 Fire!
- UVa 11624 Fire! / BFS
- UVa:11624 Fire!
- UVA - 11624 Fire!
- UVa 11624 - Fire!
- uva 11624 Fire
- Uva 11624 Fire!
- 样式表
- 模拟退火算法
- 聚类算法——python实现层次聚类(AGNES)
- HUffmanTree
- Java数据类型
- UVA 11624 Fire!
- Proxy SwitchySharp使用
- 文本三剑客之sed
- 格式与布局
- Harris 角点检测原理详解
- Linux驱动之Kobject、Kset (二)uevent mdev ktype type
- 【LeetCode】数学题
- tf.train.exponential_decay的用法
- OpenSSL命令详解