您的位置:首页 > 其它

图论

2016-03-16 11:04 232 查看
D - 图论
Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu
Submit Status

Description

Jane is one of the most talented young programmers as well as an astrophysicist. Recently she discovered a planet and named it Jotunheim - the world of giants. As you already guessed that the inhabitants are all giants. Among them the Frost Giants are the
most evil ones. Before Jane could publicly announce her great discovery, the Frost Giants came and captured her in a maze. Since the Giants would be discovered to the universe because of her, that's why they lit fires on some positions in the maze to kill
her.

You are given Jane's location in the maze and the positions of the fires lit by the Frost Giants whom are always keeping an eye on her; you must find out whether Jane can escape from the maze before fire catches her, and how fast she can do it.

The Maze is defined as a 2D grid and the locations are defined as squares. The cost of each move is one square per minute. In each move, Jane can move vertically or horizontally but not diagonally. She cannot move to a square which is blocked by an obstacle,
or which is already burning. If a square has fire in it, in the next minute, fires spread to its adjacent non-obstacle squares (vertically or horizontally). Jane can escape from the maze from any squares that borders the edge of the maze.

Input

Input starts with an integer T (≤ 50), denoting the number of test cases.

The first line of each test case contains the two integers R and C,
separated by spaces, with 1 ≤ R, C ≤ 200. 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:

1. # an obstacle

2. . a free location

3. J Jane's initial position
in the maze (there will be exactly one 'J' in the maze)

4. F Position of a Fire

Output

For each case, print the case number and 'IMPOSSIBLE' if Jane cannot escape from the maze before
fire reaches her, or the earliest time for Jane to safely escape from the maze, in minutes.

Sample Input

2

4 5

##.##

#JF.#

#...#

#...#

3 3

###

#J.

#.F

Sample Output

Case 1: 3

Case 2: IMPOSSIBLE

PS:先算出F走到每一格需要的步数,存起来
再算J走到每一格的步数,如果走到边缘且步数比F小,则说明可以走出

代码:
#include<iostream>
#include<queue>
using namespace std;

struct P
{
int x;
int y;
int count;
}p;

char map[210][210];
int firecount[210][210];
queue<P> psj, psf;
int dis[4][2] = { 0, 1, 1, 0, 0, -1, -1, 0 };
int r, c;

void find()
{
while (!psf.empty())
{
P f = psf.front();
psf.pop();
for (int i = 0; i < 4; ++i)
{
p.x = f.x + dis[i][0];
p.y = f.y + dis[i][1];
p.count = f.count + 1;
if (p.x >= 0 && p.x < r && p.y >= 0 && p.y < c && (map[p.x][p.y] == '.' || map[p.x][p.y] == 'J'))
{
psf.push(p);
map[p.x][p.y] = 'F';
firecount[p.x][p.y] = p.count;
}
}
}
while (!psj.empty())
{
P f = psj.front();
psj.pop();
if (firecount[f.x][f.y] > f.count && (f.x == 0 || f.x == r - 1 || f.y == 0 || f.y == c - 1))
{
cout << f.count << endl;
while (!psj.empty())
{
psj.pop();
}
return;
}
for (int i = 0; i < 4; ++i)
{
p.x = f.x + dis[i][0];
p.y = f.y + dis[i][1];
p.count = f.count + 1;
if (p.x >= 0 && p.x < r && p.y >= 0 && p.y < c && map[p.x][p.y] == 'F' && p.count < firecount[p.x][p.y])
{
psj.push(p);
map[p.x][p.y] = 'J';
}
}
}
cout << "IMPOSSIBLE" << endl;
}

int main()
{
int t;
cin >> t;
for (int i = 1; i <= t; ++i)
{
cin >> r >> c;
for (int m = 0; m < r; ++m)
{
for (int n = 0; n < c; ++n)
{
cin >> map[m]
;
if (map[m]
== 'J')
{
p.x = m;
p.y = n;
p.count = 1;
psj.push(p);
}
if (map[m]
== 'F')
{
p.x = m;
p.y = n;
p.count = 1;
psf.push(p);
firecount[m]
= 1;
}
}
}
cout << "Case " << i << ": ";
find();
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: