poj 2049 Finding Nemo(bfs+dij 建图难)
来源:互联网 发布:阿里云空间 编辑:程序博客网 时间:2024/05/25 08:12
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.
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.
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.
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
5-1
#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>#include <string>#include <queue>#include <vector>using namespace std;typedef long long LL;const int N = 1010;int dis[N][N], xa[N][N], ya[N][N];const LL mod = 1e9+7;const int inf = 0x3f3f3f3f;int xn, ym;struct node{ int x, y, step;;};int dir[4][2]= {{-1,0},{1,0},{0,-1},{0,1}};int bfs(int sx,int sy){ queue<node>q; node e; e.x=1, e.y=1; q.push(e); for(int i=1; i<=ym; i++) { for(int j=1; j<=xn; j++) dis[i][j]=inf; } dis[1][1]=0; while(!q.empty()) { node u=q.front();q.pop(); for(int i=0; i<4; i++) { int a1=u.x+dir[i][0], b1=u.y+dir[i][1]; int h; if(i==0) h=ya[u.x-1][u.y]; else if(i==1) h=ya[u.x][u.y]; else if(i==2) h=xa[u.x][u.y-1]; else h=xa[u.x][u.y]; if(a1<0||a1>xn+1||b1<0||b1>ym+1) continue; if(dis[a1][b1]>dis[u.x][u.y]+h) { dis[a1][b1]=dis[u.x][u.y]+h; node tmp=u; tmp.x=a1, tmp.y=b1; q.push(tmp); } } } return dis[sx][sy]>=inf?-1:dis[sx][sy];}int main(){ int n, m; while(scanf("%d %d", &n, &m),m!=1&&n!=-1) { memset(xa,0,sizeof(xa)); memset(ya,0,sizeof(ya)); xn=ym=0; for(int i=0; i<n; i++) { int x, y, d, t; scanf("%d %d %d %d", &x, &y, &d, &t); if(d) { for(int j=y+1; j<=y+t; j++) ya[x][j]=inf; xn=max(xn,x+1); ym=max(ym,y+t+1); } else { for(int j=x+1; j<=x+t; j++) xa[j][y]=inf; xn=max(xn,x+t+1); ym=max(ym,y+1); } } for(int i=0; i<m; i++) { int x, y, d; scanf("%d %d %d", &x, &y, &d); if(d) ya[x][y+1]=1; else xa[x+1][y]=1; } double sx, sy; scanf("%lf %lf", &sx, &sy); if(!(sx>=1&&sx<=199&&sy>=1&&sy<=199)) printf("0\n"); else printf("%d\n",bfs((int)sx+1, (int)sy+1)); } return 0;}
0 0
- poj 2049 Finding Nemo(bfs+dij 建图难)
- POJ 2049 Finding Nemo(三维BFS)
- Finding Nemo POJ 2049(三维BFS)
- 【POJ】 2049 Finding Nemo BFS
- POJ 2049 Finding Nemo ( BFS)
- poj-2049 Finding Nemo-BFS
- poj-2049-Finding Nemo-BFS
- POJ Finding Nemo 2049 (bfs)
- POJ 2049 Finding Nemo (网格中的BFS)
- POJ 2049— Finding Nemo(三维BFS)10/200
- Finding Nemo (poj 2049 超级蛋疼的bfs)
- poj 2049 Finding Nemo(bfs+优先队列)
- Finding Nemo POJ 2049(bfs+dp思想)
- POJ 2049 Finding Nemo (bfs+优先队列)
- Poj 2049 Finding Nemo(建图+BFS)
- POJ 2049 Finding Nemo 建图/BFS
- poj 2049 Finding Nemo (反过来使用bfs)
- POJ 2049 Finding Nemo
- 安卓国际化
- 九度OJ 1080 进制转换(又是大数计算)
- Linux基础——yum工具详解
- .realines()应用,将中文文章和对应英文文章,按顺序的合并在一起
- LeetCode OJ|Array| Find All Numbers Disappeared in an Array
- poj 2049 Finding Nemo(bfs+dij 建图难)
- Android四大组件及其生命周期
- Nginx+IIS+Redis 处理Session共享问题 1
- 如何日吸500精准粉,转化350精准粉?
- 蓝桥杯 牌型种数 DFS
- Linux基础——编译源码安装
- 218. The Skyline Problem
- C#——基础
- sapjco3.jar 使用maven私服的版本问题