poj 2049 Finding Nemo(3D搜索)
来源:互联网 发布:阿里云ntp服务器 编辑:程序博客网 时间:2024/06/11 01:48
题目链接:http://poj.org/problem?id=2049
Finding Nemo
Time Limit: 2000MS Memory Limit: 30000KTotal Submissions: 9641 Accepted: 2288
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.
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.
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 91 1 1 32 1 1 33 1 1 34 1 1 31 1 0 31 2 0 31 3 0 31 4 0 32 1 12 2 12 3 13 1 13 2 13 3 11 2 03 3 04 3 11.5 1.54 01 1 0 11 1 1 12 1 1 11 2 0 11.5 1.7-1 -1
Sample Output
5-1
Source
Beijing 2004
[Submit] [Go Back] [Status] [Discuss]
Home Page Go Back To top
题意:给你一个迷宫,Nemo画出他走过的路径上的墙和门,通过迷宫问你最少穿过几个门找到Nemo
解析:把位置映射到网格里,如(x,y) --> (x+1,y+1),想x[N][N]存所有右侧的图,y[N][N]存所有上方的图,
1表示门,INF表示墙,0表示空地(比较坑,还有就是Nemo可能不在迷宫内部)
代码:
#include<iostream>#include<algorithm>#include<cstdio>#include<cstring>#include<cmath>#include<vector>#include<stack>#include<queue>#define N 209using namespace std;const int INF = 0x3f3f3f3f;int xa[N][N], ya[N][N], d[N][N];;int nx[4] = {-1, 0, 0, 1};int ny[4] = {0, -1, 1, 0};int xmax, ymax;int get_w(int x, int y, int i){ if(i == 0) return ya[x-1][y]; if(i == 1) return xa[x][y-1]; if(i == 2) return xa[x][y]; return ya[x][y];}int bfs(int s, int e){ if(xmax < 0 || ymax < 0) return 0; queue<int> q; q.push(1); q.push(1); memset(d, 0x3f, sizeof(d)); d[1][1] = 0; while(!q.empty()) { int i = q.front(); q.pop(); int j = q.front(); q.pop(); for(int k = 0; k < 4; k++) { int x = i + nx[k]; int y = j + ny[k]; if(x < 1 || x > xmax || y < 1 || y > ymax) continue; int tm = get_w(i, j, k); if(d[x][y] > d[i][j] + tm) { d[x][y] = d[i][j] + tm; q.push(x); q.push(y); } } } return d[s][e] == INF ? -1 : d[s][e];}int main(){ int n, m; int x, y, d, t; double s, e; while(scanf("%d%d", &n, &m)) { if(m == -1 && n == -1) break; memset(xa, 0, sizeof(xa)); memset(ya, 0, sizeof(ya)); xmax = ymax = -1; for(int i = 1; i <= n; i++) { scanf("%d%d%d%d", &x, &y, &d, &t); if(d) { for(int j = 0; j < t; j++) ya[x][y + j + 1] = INF; xmax = max(xmax, x + 1); ymax = max(ymax, y + t + 1); } else { for(int j = 0; j < t; j++) xa[x + j + 1][y] = INF; xmax = max(xmax, x + t + 1); ymax = max(ymax, y + 1); } } for(int i = 1; i <= m; i++) { scanf("%d%d%d", &x, &y, &d); if(d) ya[x][y + 1] = 1; else xa[x + 1][y] = 1; } scanf("%lf%lf", &s, &e); if(s >= 1 && s <= 199 && e >= 1 && e <= 199) printf("%d\n", bfs((int)s + 1, (int)e + 1)); else printf("0\n"); } return 0;}
0 0
- poj 2049 Finding Nemo(3D搜索)
- poj 2049 Finding Nemo (坑爹的搜索题)
- POJ 2049 Finding Nemo
- POJ 2049 Finding Nemo
- POJ 2049 Finding Nemo
- POJ 2049 Finding Nemo
- POJ 2049 Finding Nemo
- POJ 2049 Finding Nemo
- POJ 2049 Finding Nemo
- POJ 2049 Finding Nemo
- 【POJ 2049】Finding Nemo
- poj 2049 Finding Nemo
- Finding Nemo(POJ--2049
- POJ 2049 Finding Nemo
- poj 2049 Finding Nemo
- POJ-2049-Finding Nemo
- POJ-2049 Finding Nemo
- poj 2049 Finding Nemo
- windows安装lxml
- LCS(Longest Common Subsequence 最长公共子序列)
- 两种筛法比较
- linux 安装tomcat
- 最全面的Socket使用解析
- poj 2049 Finding Nemo(3D搜索)
- 中缀表达式与后缀表达式之间的转换
- iOS实现向多个社交网站(facebook,微信,微博,twitter等)分享的代码,支持iPhone和iPAD(分享链接编码)
- RPC架构简单理解
- matlab中的图像坐标系统
- 深入理解java虚拟机学习笔记(一、走近java)
- 剑指offer-把数组排成最小的数(Java)
- 如何在Xcode8上安装插件
- 机器学习过拟合