搜索进阶 Fire!
来源:互联网 发布:网络犯罪罪名 编辑:程序博客网 时间:2024/05/01 17:13
题目链接
11624 Fire!
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.
Input
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.
Output
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.
Sample Input
2
4 4
####
#JF#
#..#
#..#
3 3
###
#J.
#.F
Sample Output
3
IMPOSSIBLE
#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>#include <cmath>#include <cstdlib>#include <limits>#include <queue>#include <stack>using namespace std;#define N 1100#define INF 0xfffffff#define PI acos (-1.0)#define EPS 1e-8const int dir[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};struct node{ int x, y, t;}J, F[N];int n, m, k, vis1[N][N], vis[N][N];char g[N][N];void BFS1 ();void BFS2 ();int main (){ int t; cin >> t; while (t--) { cin >> n >> m; k = 0; for (int i=0; i<n; i++) { for (int j=0; j<m; j++) { cin >> g[i][j]; if (g[i][j] == 'J') J = (node) {i, j, 0}; else if (g[i][j] == 'F') F[k++] = (node) {i, j, 0}; } } BFS1 (); BFS2 (); } return 0;}void BFS1 (){ memset (vis, 0, sizeof (vis)); queue <node> que; for (int i=0; i<k; i++) que.push (F[i]); while (que.size()) { node qq = que.front(); que.pop(); if (qq.x<0 || qq.x>=n || qq.y<0 || qq.y>=m) return; for (int i=0; i<4; i++) { node q = qq; q.x += dir[i][0]; q.y += dir[i][1]; if (q.x>=0 && q.x<n && q.y>=0 && q.y<m && g[q.x][q.y]!='#' && g[q.x][q.y]!='F' && !vis[q.x][q.y]) { vis[q.x][q.y] = vis[qq.x][qq.y] + 1; que.push(q); } } }}void BFS2 (){ memset (vis1, 0, sizeof (vis1)); queue <node> que; que.push (J); while (que.size()) { J = que.front(); que.pop(); if (J.x==0 || J.x==n-1 || J.y==0 || J.y==m-1) { cout << J.t+1 << endl; return; } for (int i=0; i<4; i++) { node q = J; q.x += dir[i][0]; q.y += dir[i][1]; q.t++; if (q.x>=0 && q.x<n && q.y>=0 && q.y<m && (vis[q.x][q.y]>q.t || (!vis[q.x][q.y] && g[q.x][q.y]!='F'))&& g[q.x][q.y]!='#' && !vis1[q.x][q.y]) { vis1[q.x][q.y] = 1; que.push (q); } } } puts ("IMPOSSIBLE");}
- 搜索进阶 Fire!
- Fire Net 搜索 贪心
- ZOJ 1002 Fire Net(搜索)
- hdu-1045-Fire Net(搜索)
- uva 11624Fire!(搜索:BFS)
- Uva11624 Fire(bfs 搜索)
- FZU 2150 Fire Game 搜索
- 【搜索】FZU 2150 Fire Game
- HDU1045 Fire Net 暴力搜索DFS
- ZOJ 1002 Fire Net (搜索 || 二分图)
- hdu 1045 Fire Net(回溯搜索)
- FZU2150 Fire Game(搜索题:bfs)
- zoj1002 FIRE NET(搜索算法DFS)
- HDU ACM 1045 Fire Net 暴力搜索
- HDU 1045 Fire Net (搜索+回溯)
- HDU 1045 Fire Net 深度搜索
- HDU 1045 Fire Net(搜索剪枝)
- FZU 2150 Fire Game(双端bfs搜索)
- Facebook测试在品牌专页内开展电商销售
- POJ1338 & POJ2545 & POJ2591 & POJ2247 找给定规律的数
- codeforces #312C 558C C. Amr and Chemistry(位运算)
- 通过CookieManager给WebView设置Cookie
- SpringMvc+Spring同时扫描出现问题。
- 搜索进阶 Fire!
- 字符串的获取
- 一键解决ScrollView嵌套ListView只显示一行的问题
- [MFC] 获得客户区在对话框上的位置
- 抓包使用说明
- 写一些去第一家公司面试的一些题目及答案
- offsetLeft,Left,clientLeft,clientX,pageX,screenX的区别
- EasyUI combogrid 用法(带搜索)
- QQTEA加密解密实现