UVA 11624 J - Fire!

原题:
Joe works in a maze. Unfortunately, portions of the maze have caught on re, and the owner of the maze neglected to create a re escape plan. Help Joe escape the maze.Given Joe’s location in the maze and which squares of the maze are on re, you must determine whether Joe can exit the maze before the re reaches him, and how fast he can do it.Joe and the re each move one square per minute, vertically or horizontally (not diagonally). The re spreads all four directions from each square that is on re. Joe may exit the maze from any square that borders the edge of the maze. Neither Joe nor the re may enter a square that is occupied by a wall.
Input
The r st line of input contains a single integer, the number of testcases to follow. The r st line of each test case contains the two integers Rand C, separated by spaces, with 1R;
C1000.
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 re 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 re reaches him, or an integer giving the earliest time Joe can safely exit the maze, in minutes.

Output
For each test case, output a single line containing `IMPOSSIBLE’ if Joe cannot exit the maze before the re 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

中文:
给你一个迷宫,这个迷宫里面有多个起火点,然后给你一个起始位置,每秒种火焰向上下左右扩散,人在每秒可以向上下左右行走,问你能否跑出迷宫,如果能给出最少时间,否则输出IMPOSSIBLE

代码:
#include<bits/stdc++.h>
using namespace std;
const int maxn = 200000 + 1;
typedef long long ll;
string s;
int n, m;
struct node
{
int x, y, step;
};

queue Q;

bool vis[1001][1001];
int maze[1001][1001];
int Time[1001][1001];
int dir[4][2] = {{1,0},{0,1},{-1,0},{0,-1}};

bool judge(int x, int y)
{
if (maze[x][y]!= 1 && x <= n && x >= 1 && y <= m && y >= 1)
return true;
return false;
}

void f_bfs()
{
while(!Q.empty())
{
node tmp = Q.front();
Q.pop();
for (int i=0;i<4;i++)
{
int x = tmp.x + dir[i][0];
int y = tmp.y + dir[i][1];
int t = tmp.step + 1;
if (judge(x,y) && !vis[x][y])
{
vis[x][y] = 1;
Time[x][y] = t;
Q.push(node{x,y,t});
}
}
}

}

int bfs(node st)
{
Q.push(st);
vis[st.x][st.y] = 1;
while (!Q.empty())
{
node tmp = Q.front();
Q.pop();
if (tmp.x == 1 || tmp.x == n || tmp.y == 1 || tmp.y == m)
{
return tmp.step + 1;
}
for (int i = 0; i < 4; i++)
{
int x = tmp.x + dir[i][0];
int y = tmp.y + dir[i][1];
int t = tmp.step + 1;
if (judge(x, y) && t < Time[x][y] && !vis[x][y])
{
vis[x][y] = 1;
Q.push(node{ x,y,t });
}
}
}
return -1;
}

int main()
{
ios::sync_with_stdio(false);
int t;
node st,f;
cin >> t;
while (t–)
{
f.x = f.y = -1;
f.step = 0;
cin >> n >> m;
memset(maze, 0, sizeof(maze));
memset(Time, 0, sizeof(Time));
memset(vis,0,sizeof(vis));

	for (int i = 0; i < n; i++)
	{
		cin >> s;
		for (int j = 0; j < m; j++)
		{
            Time[i+1][j+1] = INT_MAX;
			if (s[j] == 'J')
			{
				st.x = i + 1;
				st.y = j + 1;
				st.step = 0;
			}
			if (s[j] == 'F')
			{
				f.x = i + 1;
				f.y = j + 1;
                f.step = 0;
                vis[f.x][f.y] = 1;
                Time[f.x][f.y] = 0;
                Q.push(f);
			}
			if (s[j] == '#')
			{
				maze[i + 1][j + 1] = 1;
			}
			
			
		}
	}
    f_bfs();
    memset(vis,0,sizeof(vis));
    while(!Q.empty())
    {
        Q.pop();
    }
	int ans = bfs(st);

    while(!Q.empty())
    {
        Q.pop();
    }

	if (ans == -1)
	{
		cout << "IMPOSSIBLE" << endl;
	}
	else 
	{
		cout << ans << endl;
	}
}
return 0;

}

/*
2
4 4

#JF#
#…#
#…#
3 3

#J.
#.F
*/

思路:

首先注意此题有多个起火点。
先bfs遍历一遍火焰,记录迷宫中火焰到达该位置的时间(不能到达记正无穷)
然后人再bfs一次,保证人走到位置x,y的时间比火焰到达的时间小即可。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值