您的位置:首页 > 其它

poj2049 Finding Nemo BFS

2016-08-01 08:51 183 查看
Language:
Default

Finding Nemo

Time Limit: 2000MS Memory Limit: 30000K
Total Submissions: 8804 Accepted: 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 are assumed 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

Source

Beijing 2004
  被这个题卡了三四天,一开始有的思路,怎么敲都不过,几天里又都是各类比赛和测试,基本没有多少时间来做题,说起来三四天能空余出来做这个题的时间也就那么一点,最后还是在一场测试的时候碰到了这个题,在测试的时候敲了绝大部分,测试完之后用了十分钟左右就把这个题给过了,也是挺尴尬的。

  先说题意,其实我也是借助了许多工具,才看懂意思。这个题是说Nemo丢了,要从0,0出发去找他,然后因为门很危险,所以不要求路径最短,而是要求经过的门最少。题目给出墙和门的起点、方向和长度,可以自己处理成图。这个题关键是处理成图的过程,有多种方法可以使用。根据方法的不同,处理出来的图也是不一样的,但做法都大同小异。

  我的方法是用结构体存下每个位置四个方向分别是什么,然后再跑BFS。要注意,因为需要得到的是最少经过的门数,因此并不是最早跑到的是最小的。这里我在每一步之前进行判断,对每个位置,只有当前门数少于之前储存的门数,才会更新入栈,以此来保证找到重点时门数最少。这个题还有一点坑点,就是虽然迷宫范围只有1-199,但是Nemo的坐标可能不在这个范围内,要特别判断一下,如果不在范围内,直接输出0。下面上我的代码,处理的部分我写的麻烦了不少。

#include <stdio.h>
#include <string.h>

const int INF = 23333333;

struct node
{
int u, d, l, r;
}a[222][222];

struct Move
{
int x, y, door;
}s[222*222*22];

int Min[222][222], minl;

void pri()
{
for (int j = 6; j >= 0; j--)
{
for (int i = 0; i < 7; i++)
printf("%d ", Min[i][j]);
printf("\n");
}
}

void BFS(int x, int y)
{
struct Move now, next;
now.x = x;
now.y = y;
now.door = 0;
int base = 0, top = 0;
Min[now.x][now.y] = 0;
s[top++] = now;
while (base < top)
{
if (a[s[base].x][s[base].y].u != 1)
{
next.x = s[base].x;
next.y = s[base].y + 1;
next.door = s[base].door;
if (a[s[base].x][s[base].y].u == -1)
next.door = s[base].door + 1;
if (next.x>=0&&next.x<200&&next.y>=0&&next.y<200&&Min[next.x][next.y] > next.door)
{
s[top++] = next;
Min[next.x][next.y] = next.door;
}
}

if (a[s[base].x][s[base].y].d != 1)
{
next.x = s[base].x;
next.y = s[base].y - 1;
next.door = s[base].door;
if (a[s[base].x][s[base].y].d == -1)
next.door = s[base].door + 1;
if (next.x>=0&&next.x<200&&next.y>=0&&next.y<200&&Min[next.x][next.y] > next.door)
{
s[top++] = next;
Min[next.x][next.y] = next.door;
}
}

if (a[s[base].x][s[base].y].l != 1)
{
next.x = s[base].x - 1;
next.y = s[base].y;
next.door = s[base].door;
if (a[s[base].x][s[base].y].l == -1)
next.door = s[base].door + 1;
if (next.x>=0&&next.x<200&&next.y>=0&&next.y<200&&Min[next.x][next.y] > next.door)
{
s[top++] = next;
Min[next.x][next.y] = next.door;
}
}

if (a[s[base].x][s[base].y].r != 1)
{
next.x = s[base].x + 1;
next.y = s[base].y;
next.door = s[base].door;
if (a[s[base].x][s[base].y].r == -1)
next.door = s[base].door + 1;
if (next.x>=0&&next.x<200&&next.y>=0&&next.y<200&&Min[next.x][next.y] > next.door)
{
s[top++] = next;
Min[next.x][next.y] = next.door;
}
}
//pri();
base++;
}
}

int main()
{
int i, j, n, m, l;
while (scanf("%d%d", &n, &m), n!=-1||m!=-1)
{
int x, y, k, d;
memset(a, 0, sizeof a);
for (i = 0; i < 222; i++)
for (j = 0; j < 222; j++)
Min[i][j] = INF;
for (i = 0; i < n; i++)
{
scanf("%d%d%d%d", &x, &y, &k, &d);
if (k == 1)
for (l = 0; l < d; l++)
{
a[x-1][y].r = a[x][y].l = 1;
y++;
}
else if (k == 0)
for (l = 0; l < d; l++)
{
a[x][y-1].u = a[x][y].d = 1;
x++;
}
}
for (i = 0; i < m; i++)
{
scanf("%d%d%d", &x, &y, &k);
if (k == 1)
a[x-1][y].r = a[x][y].l = -1;
else if (k == 0)
a[x][y-1].u = a[x][y].d = -1;
}
double xb, yb;
scanf("%lf%lf", &xb, &yb);
if (xb < 0 || xb > 199 || yb < 0 || yb > 199)
printf("0\n");
else
{
BFS(int(xb), int(yb));
//pri();
if (Min[0][0] != INF)
printf("%d\n", Min[0][0]);
else
printf("-1\n");
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: