POJ 2049 Finding Nemo
这道题各种忧伤啊,一开始做的时候想用三维数组来标记图中的元素的,但是实现起来很麻烦又放弃了啊、后来用了二维数组存,将边用点来表示、、、但是由于心情不佳,建图就建了一下午啊,好不容易建出图来,在BFS的时候又出现了问题啊、、、没有设定边界导致不好跳出啊、、而且是用的简单的队列所以时间也很慢啊、、后来看了同学写的,用了BFS+优先队列就过了啊、、中间还手残了一次以为别人的出现问题了啊、、、哎,反正各种忧伤啊!!!菜鸟苦逼啊、、、、
Finding Nemo
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.
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.
Time Limit: 2000MS | Memory Limit: 30000K | |
Total Submissions: 6964 | Accepted: 1586 |
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
#include <stdio.h> #include <string.h> #include <queue> #define MAX 450 using namespace std; struct block//使用优先队列 { int x, y, door; bool operator < (struct block b)const { return door > b.door; } }; priority_queue<struct block>q; int map[MAX][MAX]; bool vis[MAX][MAX]; int xx, yy; struct block tt; int bfs() { while(!q.empty())//保证q是空的 q.pop(); tt.x = xx; tt.y = yy; tt.door = 0; q.push(tt); vis[xx][yy] = 1; while(!q.empty()) { struct block u = q.top(); q.pop(); if(u.x == 1 && u.y == 1) return u.door; if(map[u.x-1][u.y] != 1 && !vis[u.x-1][u.y])//上 { vis[u.x-1][u.y] = 1; if(map[u.x-1][u.y] == 2) { tt.x = u.x-1; tt.y = u.y; tt.door = u.door+1; } else { tt.x = u.x-1; tt.y = u.y; tt.door = u.door; } q.push(tt); } if(map[u.x+1][u.y] != 1 && !vis[u.x+1][u.y])//下 { vis[u.x+1][u.y] = 1; if(map[u.x+1][u.y] == 2) { tt.x = u.x+1; tt.y = u.y; tt.door = u.door+1; } else { tt.x = u.x+1; tt.y = u.y; tt.door = u.door; } q.push(tt); } if(map[u.x][u.y-1] != 1 && !vis[u.x][u.y-1])//左 { vis[u.x][u.y-1] = 1; if(map[u.x][u.y-1] == 2) { tt.x = u.x; tt.y = u.y-1; tt.door = u.door+1; } else { tt.x = u.x; tt.y = u.y-1; tt.door = u.door; } q.push(tt); } if(map[u.x][u.y+1] != 1 && !vis[u.x][u.y+1])//右 { vis[u.x][u.y+1] = 1; if(map[u.x][u.y+1] == 2) { tt.x = u.x; tt.y = u.y+1; tt.door = u.door+1; } else { tt.x = u.x; tt.y = u.y+1; tt.door = u.door; } q.push(tt); } } return -1; } int main() { int n, m, x1, y1, d, t; int i, j; while(~scanf("%d %d",&n, &m)) { if(n == m && n == -1) break; memset(map , 0 , sizeof(map)); memset(vis , 0 , sizeof(vis)); for(i = 0; i < n; i++) { scanf("%d %d %d %d",&x1, &y1, &d, &t); if(d == 1) for(j = y1*2; j <= (y1+t)*2; j++) map[x1*2][j] = 1; else for(j = x1*2; j <= (x1+t)*2; j++) map[j][y1*2] = 1; } for(i = 0; i < m; i++) { scanf("%d %d %d",&x1, &y1, &d); if(d == 1) map[x1*2][y1*2+1] = 2; else map[x1*2+1][y1*2] = 2; } double f1, f2; scanf("%lf %lf",&f1, &f2); if(f1 < 0 || f1 > 199 || f2 < 0 || f2 > 199) printf("0 "); else { xx = (int)f1*2+1; yy = (int)f2*2+1; for(i = 0; i <= 400; i++) map[0][i] = map[i][0] = map[i][400] = map[400][i] = 1;//制定边界 printf("%d ",bfs()); } } return 0; }
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。
- 上一篇: 【POJ 2049】Finding Nemo
- 下一篇: php 去掉二维数组中的空数组