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
题目类型:BFS
算法分析:同时扩展Joe可以走的位置和着火的区域(这里分别是将两个扩展的时间同步来实现的),注意图中可以存在多个Fire的地方!!!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
#pragma comment(linker, "/STACK:102400000,102400000") #include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; #define CFF freopen ("aaa.txt", "r", stdin) #define CPPFF ifstream cin ("aaa.txt") #define DB(ccc) cout << #ccc << " = " << ccc << endl #define PB push_back #define MP(A, B) make_pair(A, B) typedef long long LL; typedef unsigned long long ULL; typedef double DB; typedef pair <int, int> PII; typedef pair <int, bool> PIB; const int INF = 0x7FFFFFFF; const int MOD = 1e9 + 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 1e5 + 6666; const int dx[] = {-1, 1, 0, 0}; const int dy[] = {0, 0, -1, 1}; char g[1666][1666]; int row, col, sx, sy, len; bool vis[1666][1666], vvis[1666][1666]; struct node { int a, b, cnt; node (int aa, int bb, int ccnt) : a (aa), b (bb), cnt (ccnt) {} node () {} }; node fire[1666*1666]; int bfs () { memset (vis, false, sizeof(vis)); memset (vvis, false, sizeof (vvis)); vis[sx+6][sy+6] = true; for (int i = 0; i < len; i++) vvis[fire[i].a+6][fire[i].b+6] = true; queue <node> qua, qub; qua.push (node (sx, sy, 0)); for (int i = 0; i < len; i++) qub.push (node (fire[i].a, fire[i].b, 0)); int pp = 0; while (1) { if (qua.empty ()) return -1; while (!qua.empty ()) { node tt = qua.front (); if (tt.cnt > pp) break; qua.pop (); if (tt.a == -1 || tt.b == -1 || tt.a == row || tt.b == col) return tt.cnt; if (vvis[tt.a+6][tt.b+6]) continue; for (int i = 0; i < 4; i++) { int tx = tt.a + dx[i], ty = tt.b + dy[i]; if (tx >= 0 && tx <= row - 1 && ty >= 0 && ty <= col - 1 && g[tx][ty] == '.' && !vis[tx+6][ty+6] && !vvis[tx+6][ty+6]) { vis[tx+6][ty+6] = true; qua.push (node (tx, ty, tt.cnt + 1)); } if (tx == -1 || tx == row || ty == -1 || ty == col) qua.push (node (tx, ty, tt.cnt + 1)); } } if (qua.empty ()) return -1; while (!qub.empty ()) { node ttt = qub.front (); if (ttt.cnt > pp) break; qub.pop (); for (int i = 0; i < 4; i++) { int tx = ttt.a + dx[i], ty = ttt.b + dy[i]; if (tx >= 0 && tx <= row - 1 && ty >= 0 && ty <= col - 1 && !vvis[tx+6][ty+6] && g[tx][ty] != '#') { vvis[tx+6][ty+6] = true; qub.push (node (tx, ty, ttt.cnt + 1)); } } } pp++; } return -1; } int main() { // CFF; int t; scanf ("%d", &t); while(t--) { scanf ("%d %d", &row, &col); for (int i = 0; i < row; i++) scanf (" %s", g[i]); len = 0; for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { if (g[i][j] == 'J') { sx = i, sy = j; } if (g[i][j] == 'F') { fire[len].a = i; fire[len].b = j; fire[len++].cnt = 0; } } } int tt = bfs (); if (tt == -1) puts ("IMPOSSIBLE"); else printf ("%d\n", tt); } return 0; } |
- « 上一篇:fzu2150
- hdu2612:下一篇 »