#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
#define inf 0x3f3f3f3f
int n, m;
int vis[1100][1100];
int vis1[1100][1100];
char mmap[1100][1100];
int x1, x2, y2, y11;
int ans = 0;
struct node
{
int x, y, step;
} w[10000];
int Max = 0;
int c[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};
bool judge(int x, int y)
{
if(x >= 0 && x <= n - 1 && y >= 0 && y <= m - 1)return true;
else return false;
}
bool judge1(int x, int y)
{
if(x == 0 || x == n - 1 || y == 0 || y == m - 1)return true;
else return false;
}
void bfs1(int x, int y)
{
queue<node>q;
node t;
t.step = 0;
t.x = x;
t.y = y;
q.push(t);
ans = 0;
memset(vis, 0, sizeof(vis));
vis[x][y] = 1;
while(!q.empty())
{
node tmp;
tmp = q.front();
q.pop();
if(tmp.step < vis1[tmp.x][tmp.y] && judge1(tmp.x, tmp.y))
{
ans = tmp.step + 1;
break;
}
for(int i = 0; i <= 3; i++)
{
int xx = tmp.x + c[i][0];
int yy = tmp.y + c[i][1];
if(judge(xx, yy) && !vis[xx][yy] && tmp.step + 1 < vis1[xx][yy] && mmap[xx][yy] == '.')
{
node tt;
tt.step = tmp.step + 1;
vis[xx][yy] = 1;
tt.x = xx;
tt.y = yy;
q.push(tt);
}
}
}
}
void bfs()
{
queue<node> q;
memset(vis, 0, sizeof(vis));
for(int i = 0; i <= Max - 1; i++)
{
q.push(w[i]);
vis[w[i].x][w[i].y] = 1;
}
while(!q.empty())
{
node tmp;
tmp = q.front();
vis1[tmp.x][tmp.y] = tmp.step;
q.pop();
for(int i = 0; i <= 3; i++)
{
int xx = tmp.x + c[i][0];
int yy = tmp.y + c[i][1];
if(judge(xx, yy) && !vis[xx][yy] && mmap[xx][yy] != '#')
{
node tt;
tt.step = tmp.step + 1;
vis[xx][yy] = 1;
tt.x = xx;
tt.y = yy;
q.push(tt);
}
}
}
}
int main()
{
int t;
cin >> t;
while(t--)
{
int sum = 0;
cin >> n >> m;
Max = 0;
for(int i = 0; i <= n - 1; i++)
{
for(int j = 0; j <= m - 1; j++)
{
vis1[i][j] = inf;
}
}
for(int i = 0; i <= n - 1; i++)
{
for(int j = 0; j <= m - 1; j++)
{
cin >> mmap[i][j];
if(mmap[i][j] == 'J')
{
x1 = i;
y11 = j;
}
else if(mmap[i][j] == 'F')
{
w[Max].step = 0;
w[Max].x = i;
w[Max].y = j;
vis1[i][j] = 0;
Max++;
sum++;
}
}
}
// for(int i = 0; i <= n - 1; i++)
// {
// for(int j = 0; j <= m - 1; j++)
// {
// vis1[i][j] = inf;
// }
// }
bfs();
// {
// for(int i = 0; i <= n - 1; i++){
// for(int j = 0; j <= m - 1; j++){
// vis1[i][j] = inf;
// }
// }
// }
bfs1(x1, y11);
if(ans)cout << ans <<endl;
else cout << "IMPOSSIBLE" << endl;
}
return 0;
}
人一我百,人十我万!追逐青春的梦想,怀着自信的心,永不放弃! by kuangbin
UVA 11624 - Fire! (简单图论基础)
Problem B: 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 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
2 4 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
3 IMPOSSIBLE
简单图论基础题:
bfs