POJ训练计划2049_Finding Nemo(建图/BFS)
2014-05-22 11:30
363 查看
Finding Nemo
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
Sample Output
Source
解题报告
卡了很久很久的题,POJ一直刷不下去,很次卡了就得看题解,看了题解还不会,是不是很渣呀。。。
这次决定把这题给A了,看了老长的题解,貌似懂了。之前卡在建图上面。。。
题意:给一个map,它包含墙,门及空地。输入n,m分别代表墙的个数及门的个数。
对于墙,输入x,y,d,t,(x,y)是墙的左下角的坐标;d = 1平行y轴,d=0平行x轴;t代表墙延伸的长度。
对于门,输入x,y,d,(x,y)是门的左下角坐标,d同上,因为门的长度始终为1,t即忽略.
建图是麻烦事,一条边用三个点代替(左,右,中),也就是说把图扩大2倍,这样就可以用来搜索。1表示墙或是墙与墙的连接点,2表示门的开口,0表示空白地区。
这题让求从(0,0)出发到(x,y)过的门最少的个数
最好是在0-400围上墙,这样只要在这个范围搜索就行了。
Time Limit: 2000MS | Memory Limit: 30000K | |
Total Submissions: 7317 | Accepted: 1698 |
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
解题报告
卡了很久很久的题,POJ一直刷不下去,很次卡了就得看题解,看了题解还不会,是不是很渣呀。。。
这次决定把这题给A了,看了老长的题解,貌似懂了。之前卡在建图上面。。。
题意:给一个map,它包含墙,门及空地。输入n,m分别代表墙的个数及门的个数。
对于墙,输入x,y,d,t,(x,y)是墙的左下角的坐标;d = 1平行y轴,d=0平行x轴;t代表墙延伸的长度。
对于门,输入x,y,d,(x,y)是门的左下角坐标,d同上,因为门的长度始终为1,t即忽略.
建图是麻烦事,一条边用三个点代替(左,右,中),也就是说把图扩大2倍,这样就可以用来搜索。1表示墙或是墙与墙的连接点,2表示门的开口,0表示空白地区。
这题让求从(0,0)出发到(x,y)过的门最少的个数
最好是在0-400围上墙,这样只要在这个范围搜索就行了。
#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #include <queue> using namespace std; struct node { int x,y,step; bool operator<(const node &tmp)const { return step >tmp.step; } }; int n,m; int mmap[1000][1000],vis[1000][1000]; int dx[]={-1,0,1,0}; int dy[]={0,1,0,-1}; void bfs(int s,int e) { memset(vis,0,sizeof(vis)); priority_queue<node>Q; node next,tail; tail.x=s; tail.y=e; tail.step=0; Q.push(tail); vis[s][e]=1; while(!Q.empty()) { tail=Q.top(); Q.pop(); if(tail.x==1&&tail.y==1) { cout<<tail.step<<endl; return ; } for(int i=0;i<4;i++) { next.x=tail.x+dx[i]; next.y=tail.y+dy[i]; if(mmap[next.x][next.y]!=1&&!vis[next.x][next.y]) { if(mmap[next.x][next.y]==2) next.step=tail.step+1; else next.step=tail.step; Q.push(next); vis[next.x][next.y]=1; } } } cout<<"-1"<<endl; } int main() { int x,y,d,t; double s,e; while(cin>>n>>m) { memset(mmap,0,sizeof(mmap)); if(n==-1&&m==-1)break; for(int i=1; i<=n; i++) { cin>>x>>y>>d>>t; if(d) { for(int j=y*2; j<=(y+t)*2; j++) mmap[j][x*2]=1; } else { for(int j=x*2; j<=(x+t)*2; j++) mmap[y*2][j]=1; } } for(int i=1; i<=m; i++) { cin>>x>>y>>d; if(d) mmap[y*2+1][x*2]=2; else mmap[y*2][x*2+1]=2; } cin>>s>>e; int ss=(int )s*2+1; int ee=(int )e*2+1; if(!n&&!m) cout<<"0"<<endl; else if(s<0||s>199||ee<0||ee>199) cout<<"0"<<endl; else { for(int i = 0; i <= 400; i++) mmap[i][0] = mmap[i][400] = mmap[0][i] = mmap[400][i] = 1; bfs(ee,ss); } } return 0; }
相关文章推荐
- poj-2049 Finding Nemo-BFS
- 【POJ】 2049 Finding Nemo BFS
- poj 2049 Finding Nemo(bfs+dij 建图难)
- Finding Nemo (poj 2049 超级蛋疼的bfs)
- POJ-2049-Finding Nemo(BFS)
- POJ训练计划3414_Pots(BFS)
- poj-2049-Finding Nemo-BFS
- Finding Nemo POJ 2049(三维BFS)
- POJ 2049 Finding Nemo(三维BFS)
- POJ训练计划1753_Flip Game(枚举+BFS)
- poj2049——Finding Nemo(bfs)
- POJ训练计划3026_Borg Maze(最小生成树+BFS)
- POJ训练计划2251_Dungeon Master(三维BFS)
- POJ 2049-Finding Nemo(三维bfs解决类迷宫问题)
- POJ训练计划3126_Prime Path(素数+BFS)
- poj 2049 Finding Nemo(bfs)
- POJ Finding Nemo 2049 (bfs)
- POJ 2049 Finding Nemo ( BFS)
- POJ 2049 Finding Nemo 网格bfs
- POJ训练计划3278_Catch That Cow/SDUT2782_我跳我跳我跳跳跳(一维BFS)