POJ 2049 Finding Nemo
Time Limit: 2000MS | Memory Limit: 30000K | |
Total Submissions: 8631 | Accepted: 2019 |
Description
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
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
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 <stdlib.h>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
const int inf=0x3f3f3f3f;
struct node {
int x, y;
int ans;
} f1,f2;
int map[][][];
int vis[][];
int jx[]= {,,,-};
int jy[]= {,-,,};
int min1;
void bfs(int s, int e)
{
int i, j;
queue<node>q;
f1.x=s;
f1.y=e;
f1.ans=;
vis[s][e]=;
q.push(f1);
min1=inf;
while(!q.empty()) {
f1=q.front();
q.pop();
if(f1.x<=||f1.x>=||f1.y<=||f1.y>=) {//边界处理,但是不能跳出,只能跳过,因为存在多个门的情况
min1=min(min1,f1.ans); //走出去之后,保存最小通过门数
continue ;
}
for(i=; i<; i++) {
f2.x=f1.x+jx[i];
f2.y=f1.y+jy[i];
if(i==) {//向上走
if(!vis[f2.x][f2.y]&&map[f1.x][f1.y][]!=) {
if(map[f1.x][f1.y][]==)
f2.ans=f1.ans+;
else
f2.ans=f1.ans;
vis[f2.x][f2.y]=;
q.push(f2);
}
} else if(i==) {//向下走
if(!vis[f2.x][f2.y]&&map[f2.x][f2.y][]!=) {
if(map[f2.x][f2.y][]==)
f2.ans=f1.ans+;
else
f2.ans=f1.ans;
vis[f2.x][f2.y]=;
q.push(f2);
}
} else if(i==) {//向右走
if(!vis[f2.x][f2.y]&&map[f1.x][f1.y][]!=) {
if(map[f1.x][f1.y][]==)
f2.ans=f1.ans+;
else
f2.ans=f1.ans;
vis[f2.x][f2.y]=;
q.push(f2);
}
} else if(i==) {//向左走
if(!vis[f2.x][f2.y]&&map[f2.x][f2.y][]!=) {
if(map[f2.x][f2.y][]==)
f2.ans=f1.ans+;
else
f2.ans=f1.ans;
vis[f2.x][f2.y]=;
q.push(f2);
}
}
}
}
}
int main()
{
int n, m, i, j;
int x,y,d,t;
double a1,a2;
int int_x,int_y;
while(scanf("%d %d",&m,&n)!=EOF) {
if(n==-&&m==-) break;
memset(map,,sizeof(map));
memset(vis,,sizeof(vis));
for(i=; i<m; i++) {
scanf("%d %d %d %d",&x, &y, &d, &t);
if(d) {
for(j=; j<t; j++) {
map[x-][y+j][]=;
}
} else {
for(j=; j<t; j++) {
map[x+j][y-][]=;
}
}
}
for(i=; i<n; i++) {
scanf("%d %d %d",&x,&y,&d);
if(d) {
map[x-][y][]=;
} else {
map[x][y-][]=;
}
}
scanf("%lf %lf",&a1,&a2);
int_x=a1;
int_y=a2;
if(int_x<=||int_x>=||int_y<=||int_y>=) {//后台可能出现 0,0 200+,200+,虽然不符合题目描述
printf("0\n");
continue ;
}
bfs(int_x,int_y);
if(min1==inf)
printf("-1\n");
else
printf("%d\n",min1);
}
return ;
}
POJ 2049 Finding Nemo的更多相关文章
- POJ 2049— Finding Nemo(三维BFS)10/200
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u013497151/article/details/29562915 海底总动员.... 这个题開始 ...
- POJ 2049 Finding Nemo bfs 建图很难。。
Finding Nemo Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 6952 Accepted: 1584 Desc ...
- poj 2049 Finding Nemo(优先队列+bfs)
题目:http://poj.org/problem?id=2049 题意: 有一个迷宫,在迷宫中有墙与门 有m道墙,每一道墙表示为(x,y,d,t)x,y表示墙的起始坐标d为0即向右t个单位,都是墙d ...
- Finding Nemo 分类: POJ 2015-07-11 10:11 10人阅读 评论(0) 收藏
Finding Nemo Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 8117 Accepted: 1883 Desc ...
- poj 2049(二分+spfa判负环)
poj 2049(二分+spfa判负环) 给你一堆字符串,若字符串x的后两个字符和y的前两个字符相连,那么x可向y连边.问字符串环的平均最小值是多少.1 ≤ n ≤ 100000,有多组数据. 首先根 ...
- Finding Nemo(搜索)
http://poj.org/problem?id=2049 题意:有一个迷宫,迷宫中有墙.门和空地.有M道墙,每一道墙用(x,y,d,t)表示,(x,y)表示墙的起始坐标,(d=1,t)表示向上t个 ...
- poj 3376 Finding Palindromes
Finding Palindromes http://poj.org/problem?id=3376 Time Limit: 10000MS Memory Limit: 262144K ...
- Finding Nemo(bfs)
Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 6988 Accepted: 1600 Description Nemo ...
- POJ 3376 Finding Palindromes(扩展kmp+trie)
题目链接:http://poj.org/problem?id=3376 题意:给你n个字符串m1.m2.m3...mn 求S = mimj(1=<i,j<=n)是回文串的数量 思路:我们考 ...
随机推荐
- 分布式系统间通信之RPC简单Demo(七)
看似终点,回到起点.第一次接触C#,编写的第一个真正的Demo是基于Socket的简单通信,现在JAVA开始的第一个RPC的Demo也是基于Socket.. 下面通过java原生的序列化,Socket ...
- while死循环问题-输入字符就会死循环
问题: 是否会遇到这样的问题,在while循环中 sanf("%d",&a);如果输入的不是数字,是字符就会进入死循环. 解决方案:都是缓冲区惹的祸,输入字符后,字符会一直 ...
- J2EE开源项目
这篇文章写在我研究J2SE.J2EE近三年后.前3年我研究了J2SE的Swing.Applet.Net.RMI.Collections.IO.JNI……研究了J2EE的JDBC.Sevlet.JSP. ...
- (org.hibernate.LazyInitializationException:19) - could not initialize proxy错误
(org.hibernate.LazyInitializationException:19) - could not initialize proxy错误 在刚插入数据后,马上使用dao进行query ...
- cut 命令
今天看到cut拿来取参数也是很方便的. cut -d = -f 2 -d表示分隔符 -f参数是分隔符算第几个参数
- A Statistical View of Deep Learning (II): Auto-encoders and Free Energy
A Statistical View of Deep Learning (II): Auto-encoders and Free Energy With the success of discrimi ...
- C语言宏定义使用技巧
写好C语言,漂亮的宏定义很重要,使用宏定义可以防止出错,提高可移植性,可读性,方便性 等等.下面列举一些成熟软件中常用得宏定义...... 1.防止一个头文件被重复包含 #ifndef COMDEF_ ...
- 【HDOJ】1813 Escape from Tetris
bfs预处理一点到边界的最小距离,IDA*求出可行方案.注意按字典序初始化dir数组.并且存在中间点全为1,边界含0的可能性(wa了很多次).此时不输出任何命令. /* 1813 */ #includ ...
- WallsEveryDay 必应桌面壁纸
软件名:WallsEveryday 陈述: 无聊时写着玩的一个桌面壁纸的软件,壁纸是自动从必应下载,所以每天都会有新的. 在ubuntu上测试通过,windows上找了台win7测试通过,其他没测试. ...
- 搭建ftp环境
首先明确,ftp站点设置在服务器上,而在客户端上来使用ftp工具来进行上传文件 具体环境搭建如下两个链接,一个server2003,一个是win7 server2003:http://jingyan. ...