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)是回文串的数量 思路:我们考 ...
随机推荐
- ubuntu install opengrok
总结: 1. 安装jdk和tomcat 2. 安装ctags 3. 解压opengrok.tar.gz包, 然后将source.war复制到tomcat/webapp下面 sudo cp -R ope ...
- ObjectiveC1基础代码——类和对象
// // main.m // ObjectiveC1 // // Created by scjy on 15/10/30. // Copyright © 2015年 lizhipeng. A ...
- 转:.NET中使用Redis (二)
原文来自于:http://blog.jobbole.com/83824/ 原文出处: 寒江独钓 欢迎分享原创到伯乐头条 很久以前写了一篇文章 .NET中使用Redis 介绍了如何安装Redis服务 ...
- pycharm去掉拼写检查
http://zhidao.baidu.com/question/523436629.html
- Ruby on Rails创始人DHH谈如何进行混合移动APP开发
混合型APP兼具原生型APP软件良好用户交互体验的优势和网页型APP软件跨平台开发的优势,并且其开发成本和网页型APP软件接近,其开发效率也远高于原生型APP软件.混合型APP已经被众多企业所认可.最 ...
- Unity3d场景合并
Unity3d场景合并 一.Unity3d场景合并,有一次的情况是这样的,就是我们是每个人都在开发,每个人有不同的场景,那么合并的时候,有些会出问题,那么我有一个好的方案,就是首先弄一个公共的资源库, ...
- WordPress Comment Extra Fields插件‘swfupload.swf’跨站脚本漏洞
漏洞名称: WordPress Comment Extra Fields插件‘swfupload.swf’跨站脚本漏洞 CNNVD编号: CNNVD-201308-027 发布时间: 2013-08- ...
- 字符串(后缀自动机):Codeforces Round #129 (Div. 1) E.Little Elephant and Strings
E. Little Elephant and Strings time limit per test 3 seconds memory limit per test 256 megabytes inp ...
- 数据结构(虚树,动态规划):HNOI 2014 世界树
Hnoi2014 世界树 Description 世界树是一棵无比巨大的树,它伸出的枝干构成了整个世界.在这里,生存着各种各样的种族和生灵,他们共同信奉着绝对公正公平的女神艾莉森,在他们的信条里,公平 ...
- oracle 表查询(1)
oracle 表基本查询 介绍在我们讲解的过程中我们利用scott 用户存在的几张表(emp,dept)为大家演示如何使用select语句,select 语句在软件编程中非常有用,希望大家好好的掌握. ...