Uva 11624-Fire!
2015-07-25 16:26
405 查看
题目描述:
Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the owner of
the maze neglected to create a fire escape plan. Help Joe escape the maze.
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.
输入格式:
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.
输出格式:
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.
样例出入:
样例输出:
思路分析:
一开始想到先扩展一层所有的F,然后再扩展J能走的格子,F的扩展条件漏泄了F经过J的情况,WA一次,改掉这个BUG之后,还是WA,经过查找UVA的数据,知道了开始的程序对J和K的扩展速度没控制好,导致错误。正确的情况应该是扩展一层F,再扩展一层J,结果我写成了扩展几层F才扩展一层C,导致F的扩展速度比J快了不少。解决方法是设置一个J的扩展层节点计数变量num,当num为0时,表示当前层的J扩展完毕。另外对判定边界的方法还可以改进,比如可以先初始化MAPT为-1然后,从(1,1)开始才是真正的MAZE,当走到值为-1的点的时候,即已经走出了边界,另外Vis数组可以完全代替dist数组的功能。
源代码如下:
Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the owner of
the maze neglected to create a fire escape plan. Help Joe escape the maze.
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.
输入格式:
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.
输出格式:
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.
样例出入:
2 4 4 #### #JF# #..# #..# 3 3 ### #J. #.F
样例输出:
3 IMPOSSIBLE
思路分析:
一开始想到先扩展一层所有的F,然后再扩展J能走的格子,F的扩展条件漏泄了F经过J的情况,WA一次,改掉这个BUG之后,还是WA,经过查找UVA的数据,知道了开始的程序对J和K的扩展速度没控制好,导致错误。正确的情况应该是扩展一层F,再扩展一层J,结果我写成了扩展几层F才扩展一层C,导致F的扩展速度比J快了不少。解决方法是设置一个J的扩展层节点计数变量num,当num为0时,表示当前层的J扩展完毕。另外对判定边界的方法还可以改进,比如可以先初始化MAPT为-1然后,从(1,1)开始才是真正的MAZE,当走到值为-1的点的时候,即已经走出了边界,另外Vis数组可以完全代替dist数组的功能。
源代码如下:
//注意控制F和J的扩展速度 #include<cstdio> #include<cstring> #include<queue> #include<algorithm> using namespace std; #define N 1000 + 10 struct Node { int x, y; }; char MAPT ; int R, C, vis , dist ; int dir[4][2] = {-1, 0, 0, -1, 1, 0, 0, 1}; queue<Node> f; void Fire() { int i, num = f.size(); Node now, pre; while (num--) { pre = f.front(); f.pop(); for (i = 0; i < 4; i++) { now.x = pre.x + dir[i][0]; now.y = pre.y + dir[i][1]; if (now.x >= 0 && now.x < R && now.y >= 0 && now.y < C && (MAPT[now.x][now.y] == '.' || MAPT[now.x][now.y] == 'J')) { f.push(now); MAPT[now.x][now.y] = 'F'; } } } } int BFS(int xx, int yy) { queue<Node> q; Node now, pre; int i, j; now.x = xx; now.y = yy; q.push(now); vis[now.x][now.y] = 1; while (!q.empty()) { int num = q.size(); Fire(); while (num--) { pre = q.front(); q.pop(); for (i = 0; i < 4; i++) { now.x = pre.x + dir[i][0]; now.y = pre.y + dir[i][1]; if (now.x < 0 || now.x >= R || now.y < 0 || now.y >= C) { return dist[pre.x][pre.y] + 1; } if (!vis[now.x][now.y] && MAPT[now.x][now.y] == '.') { q.push(now); vis[now.x][now.y] = 1; dist[now.x][now.y] = dist[pre.x][pre.y] + 1; } } } } return -1; } int main() { //FILE *p = freopen("test.txt", "r", stdin); int T, i, j; while (scanf("%d", &T) != EOF) { while (T--) { scanf("%d%d", &R, &C); for (i = 0; i < R; i++) { scanf("%s", MAPT[i]); } memset(vis, 0, sizeof(vis)); memset(dist, 0, sizeof(dist)); int sx = 0, sy = 0; for (i = 0; i < R; i++) { for (j = 0; j < C; j++) { if (MAPT[i][j] == 'J') { sx = i; sy = j; vis[i][j] = 1; } if (MAPT[i][j] == 'F') { Node temp = {i, j}; f.push(temp); } } } int ans = BFS(sx, sy); if (ans >= 0) printf("%d\n", ans); else printf("IMPOSSIBLE\n"); while (!f.empty()) f.pop(); } } return 0; }
相关文章推荐
- struts2之拦截器
- 在不用strstr()函数的情况下在字符串中查找某一子字符串
- 在一组数字中是否有n个数,使得这n个数的和等于一给定的定值m
- 移动端前端开发调试
- .Net验证码实现基础--Draw
- 关于jquery同步和异步请求问题总结
- poj 1164 DFS
- Attribute
- (转载)何时用ExecuteDataSet / ExecuteReader() / ExecuteScalar
- Android 自定义View(自定义控件)
- [LeetCode] Kth Largest Element in an Array
- 噩梦5 敌人血量
- [网络流24题] 03 最小路径覆盖问题(有向无环图最小路径覆盖,网络最大流)
- 简单好用的Adapter---ArrayAdapter
- POJ 1026-Cipher(置换群)
- 希腊字母发音对照表
- 【编程之美】java二进制实现重建
- 关于c,c++,oc,swift的混合编程
- HDU 5301 Friends(DFS + 枚举)
- 【转载】计算1至n中数字X出现的次数