https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2671

因为是pdf形式我就不打了,各位上去看吧。

       大体意思就是,有一个人,要赶在火焰烧到他前炮出这个迷宫,他自己只能基础四方向的一个方向移动,火焰是每次四方向扩散,其中' . '是过道人可以走,火焰可以扩散,但是‘#’这个是墙壁,人和火都不能到达,但其中最最重要的事情就是,火焰不止一个,它包含着多个!!;

       所以我做了两个vis表,一个是人到达各个位置的最短时间是多少,一个是火焰扩散到某个位置的最短时间是多少,这样的话,只要最后看看四个边上有没有存在man_vis[a][b]<fire_vis[a][b]的就好了;

#include<iostream>
#include<cstdio>
#include<utility>
#include<cstring>
#include<queue>
using namespace std;
int m, n;
char map[1005][1005];
int man_vis[1005][1005];
int fire_vis[1005][1005];
#define defeat 1000005
int fx[4][2] = { 0,1,1,0,-1,0,0,-1 };
void BFS(int x, int y, int vis[][1005])
{
	queue<pair<int, int>>q;
	q.push(make_pair(x, y));
	vis[x][y] = 1;
	while (!q.empty())
	{
		pair<int, int>first = q.front();
		q.pop();
		pair<int, int>second;
		for (int s = 0; s < 4; s++)
		{
			second.first = first.first + fx[s][0];
			second.second = first.second + fx[s][1];
			if (second.first >= 0 && second.first < m&&second.second >= 0 && second.second < n&&map[second.first][second.second] != '#'&&vis[second.first][second.second] > vis[first.first][first.second] + 1)
			{
				vis[second.first][second.second] = vis[first.first][first.second] + 1;
				q.push(second);
			}
		}
	}
}
int check()
{
	int ans = defeat;
	for (int s = 0; s < m; s++)
	{
		if (map[s][0] == '#' || map[s][0] == 'F' || man_vis[s][0] >= fire_vis[s][0])
		{
			continue;
		}
		ans = min(ans, man_vis[s][0]);
	}
	for (int s = 0; s < m; s++)
	{
		if (map[s][n - 1] == '#' || map[s][n - 1] == 'F' || man_vis[s][n - 1] >= fire_vis[s][n - 1] )
		{
			continue;
		}
		ans = min(ans, man_vis[s][n - 1]);
	}
	for (int s = 0; s < n; s++)
	{
		if (map[0][s] == '#' || map[0][s] == 'F' || man_vis[0][s] >= fire_vis[0][s] )
		{
			continue;
		}
		ans = min(ans, man_vis[0][s]);
	}
	for (int s = 0; s < n; s++)
	{
		if (map[m - 1][s] == '#' || map[m - 1][s] == 'F' || man_vis[m - 1][s] >= fire_vis[m - 1][s])
		{
			continue;
		}
		ans = min(ans, man_vis[m - 1][s]);
	}
	return ans;
}
int main()
{
	int te;
	cin >> te;
	while (te--)
	{
		cin >> m >> n;
		for (int s = 0; s < m; s++)
		{
			for (int w = 0; w < n; w++)
			{
				man_vis[s][w] = defeat;
			}
		}
		for (int s = 0; s < m; s++)
		{
			for (int w = 0; w < n; w++)
			{
				fire_vis[s][w] = defeat;
			}
		}
		for (int s = 0; s < m; s++)
		{
			for (int w = 0; w < n; w++)
			{
				cin >> map[s][w];
			}
		}
		for (int s = 0; s < m; s++)
		{
			for (int w = 0; w < n; w++)
			{
				if (map[s][w] == 'J')
				{
					BFS(s, w, man_vis);
				}
				if (map[s][w] == 'F')
				{
					BFS(s, w, fire_vis);
				}
			}
		}
		int k = defeat;
		k = check();
		if (k == defeat)
		{
			cout << "IMPOSSIBLE" << endl;
		}
		else
		{
			cout << k << endl;
		}
	}
	return 0;
}