您的位置:首页 > 其它

Finding Nemo POJ 2049(bfs+dp思想)

2016-08-01 18:44 597 查看
Finding Nemo

Time Limit: 2000MSMemory Limit: 30000K
Total Submissions: 8804Accepted: 2069
Description

Nemo(现场转播节目) is a naughty(顽皮的) boy.
One day he went into the deep sea all by himself. Unfortunately, he became lost and couldn't find his way home. Therefore, he sent a signal to his father, Marlin, to ask for help.

After checking the map, Marlin found that the sea is like a labyrinth(迷宫) with walls and doors. All the walls are parallel(平行的) to
the X-axis or to the Y-axis. The thickness(厚度) of the walls areassumed(假定的) to
be zero.

All the doors are opened on the walls and have a length of 1. Marlin cannot go through a wall unless there is a door on the wall. Because going through a door is dangerous (there may be some virulent(剧毒的)medusas
near the doors), Marlin wants to go through as few doors as he could to find Nemo.

Figure-1 shows an example of the labyrinth and the path Marlin went through to find Nemo.



We assume Marlin's initial position is at (0, 0). Given the position of Nemo and the configuration(配置) of walls and doors, please write
a program to calculate(计算) the minimum(最小的) number
of doors Marlin has to go through in order to reach Nemo.
Input

The input(投入) consists of several test cases. Each test case is started by two non-negative(非负的) integers(整数) M
and N. M represents the number of walls in the labyrinth and N represents the number of doors.

Then follow M lines, each containing four integers that describe a wall in the following format:

x y d t

(x, y) indicates(表明) the lower-left point of the wall, d is the direction of the wall -- 0 means it's parallel(平行线) to
the X-axis and 1 means that it's parallel to the Y-axis, and t gives the length of the wall.

The coordinates(坐标) of two ends of any wall will be in the range of [1,199].

Then there are N lines that give the description of the doors:

x y d

x, y, d have the same meaning as the walls. As the doors have fixed length of 1, t is omitted(省略).

The last line of each case contains two positive(积极的) float numbers:

f1 f2

(f1, f2) gives the position of Nemo. And it will not lie within any wall or door.

A test case of M = -1 and N = -1 indicates the end of input(投入), and should not be processed.
Output

For each test case, in a separate line, please output(输出) the minimum(最小的) number
of doors Marlin has to go through in order to rescue his son. If he can't reach Nemo, output -1.
Sample Input
8 9
1 1 1 3
2 1 1 3
3 1 1 3
4 1 1 3
1 1 0 3
1 2 0 3
1 3 0 3
1 4 0 3
2 1 1
2 2 1
2 3 1
3 1 1
3 2 1
3 3 1
1 2 0
3 3 0
4 3 1
1.5 1.5
4 0
1 1 0 1
1 1 1 1
2 1 1 1
1 2 0 1
1.5 1.7
-1 -1

Sample Output
5
-1

这个题目做的有点久,比较麻烦的是构图,然后如何寻找经过最少门的路径也和普通的bfs题目不一样,跟大豪交流过后才想到这题的bfs该怎么写。
核心是开一个二维数组,记录到达每个点时所经过最少的门的数量,初始化为inf,起点初始化为0,每次在bfs中加入新节点时,需要注意这条路径到这个点时所经过的门数是否小于当前这个点所记录的经过这个点时的最小门数,如果是就加入队列,如果不是,就不入队。

需要注意:

1.当起点超出迷宫范围199时的情况。

2.当没有墙或没有门的情况。

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <vector>
#include <cmath>
using namespace std;
int m, n;
int next[4][2] = {{1, 0}, {0, 1}, {0, -1}, { -1, 0} };
int  sn;
struct p
{
int x;
int y;
int s;
} b[1000005];
int a[1005][1005];
int book[1005][1005];
int bfs(int x, int y)
{
int h, t, i;
h = t = 0;
b[t].x = x;
b[t].y = y;
b[t++].s = 0;
book[y][x] = 0;
while (h < t )
{
for (i = 0; i < 4; i++)
{
int dx, dy;
dx = b[h].x + next[i][1];
dy = b[h].y + next[i][0];
if ( dx >= 0 && dy >= 0  &&  dx <= 999 + 1 &&  dy <= 999 + 1  &&  a[dy][dx] != -1   )
{
b[t].x = dx;
b[t].y = dy;
if (a[dy][dx] == 1)
{
b[t].s = b[h].s + 1;
}
else
{
b[t].s = b[h].s;
}
if (b[t].s < book[dy][dx])
{
book[dy][dx] = b[t].s;
t++;
}
}
}
h++;
}
return book[0][0];
}
int main()
{

while (~scanf("%d%d", &n, &m))
{
if (n == m && n == -1)
{
break;
}
int i, j;
int x, y, d, t;
memset(a, 0, sizeof(a));
for (i = 0; i < 500; i++)for (j = 0; j < 500; j++)
{
book[i][j] = 99999999;
}
for (i = 0; i < n; i++)
{
scanf("%d%d%d%d", &x, &y, &d, &t);
if (d)
{
x = x * 2 - 1;
for (j = y; j <= y + t; j++)
{
int dy;
dy = j * 2 - 1;
a[j * 2 - 1][x] = -1;
if (j != y + t)
{
a[j * 2][x] = -1;
dy = j * 2;
}
}
}
else
{
y = y * 2 - 1;
for (j = x; j <= x + t; j++)
{
int dx;
dx = j * 2 - 1;
a[y][j * 2 - 1] = -1;
if (j != x + t)
{
a[y][j * 2] = -1;
dx = j * 2;
}
}
}
}
for (i = 0; i < m; i++)
{
scanf("%d%d%d", &x, &y, &d);
t = 1;
if (d)
{
x = x * 2 - 1;
for (j = y; j <= y; j++)
{
a[j * 2][x] = 1;
}
}
else
{
y = y * 2 - 1;
for (j = x; j <= x; j++)
{
a[y][j * 2] = 1;
}
}
}
double f1, f2;
scanf("%lf%lf", &f1, &f2);
x = f1;
y = f2;
x = x * 2;
y = y * 2;
sn = 99999999;
if (f1 > 199.0 || f2 > 199.0)
{
sn = 0;
}
else
{
sn = bfs(x, y);
}
if (sn != 99999999)
{
printf("%d\n", sn);
}
else
{
printf("-1\n");
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: