Finding Nemo
Time Limit: 2000MS   Memory Limit: 30000K
Total Submissions: 8631   Accepted: 2019

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

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的更多相关文章

  1. POJ 2049— Finding Nemo(三维BFS)10/200

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u013497151/article/details/29562915 海底总动员.... 这个题開始 ...

  2. POJ 2049 Finding Nemo bfs 建图很难。。

    Finding Nemo Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 6952   Accepted: 1584 Desc ...

  3. poj 2049 Finding Nemo(优先队列+bfs)

    题目:http://poj.org/problem?id=2049 题意: 有一个迷宫,在迷宫中有墙与门 有m道墙,每一道墙表示为(x,y,d,t)x,y表示墙的起始坐标d为0即向右t个单位,都是墙d ...

  4. Finding Nemo 分类: POJ 2015-07-11 10:11 10人阅读 评论(0) 收藏

    Finding Nemo Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 8117   Accepted: 1883 Desc ...

  5. poj 2049(二分+spfa判负环)

    poj 2049(二分+spfa判负环) 给你一堆字符串,若字符串x的后两个字符和y的前两个字符相连,那么x可向y连边.问字符串环的平均最小值是多少.1 ≤ n ≤ 100000,有多组数据. 首先根 ...

  6. Finding Nemo(搜索)

    http://poj.org/problem?id=2049 题意:有一个迷宫,迷宫中有墙.门和空地.有M道墙,每一道墙用(x,y,d,t)表示,(x,y)表示墙的起始坐标,(d=1,t)表示向上t个 ...

  7. poj 3376 Finding Palindromes

    Finding Palindromes http://poj.org/problem?id=3376 Time Limit: 10000MS   Memory Limit: 262144K       ...

  8. Finding Nemo(bfs)

    Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 6988   Accepted: 1600 Description Nemo ...

  9. POJ 3376 Finding Palindromes(扩展kmp+trie)

    题目链接:http://poj.org/problem?id=3376 题意:给你n个字符串m1.m2.m3...mn 求S = mimj(1=<i,j<=n)是回文串的数量 思路:我们考 ...

随机推荐

  1. ecshop分页问题1

    点解下一页时弹出 查找原因: json返回 分页查询之后返回的 filter 数据为空 问题在这: $deliveryInfo['fliter']  $deliveryInfo['page_count ...

  2. Android 常用系统控件

    1. 日期选择器 DatePickerDialog 2. 时间选择器 TimePickerDialog 3. 单选按钮 RadioButton 4. 多选按钮 CheckBox 5. 下拉列表 Spi ...

  3. while if 循环判断

    temp=input("猜一下我想的那个数字吧:") guess=int(temp) while guess!=8: temp=input("诶呀错误了在输入一次吧:&q ...

  4. 发送邮件(E-mail)方法整理合集

    在IOS开发中,有时候我们会需要用到邮件发送的功能.比如,接收用户反馈和程序崩溃通知等等.其实这个功能是很常用的,因为我目前就有发送邮件的开发需求,所以顺便整理下IOS发送邮件的方法. IOS原生自带 ...

  5. 转:GraphicsMagick介绍及安装

    原文来自于:http://www.cnblogs.com/cocowool/archive/2010/08/16/1800954.html GraphicsMagick 当前稳定版本:1.3.12(发 ...

  6. Hotel

    poj3667:http://poj.org/problem?id=3667 题目大意:Hotel有N(1 ≤ N ≤ 50,000)间rooms,并且所有的rooms都是连续排列在同一边,group ...

  7. 硬盘IO,SAS,SATA,和HD TUNE

    SAS的接口技术可以向下兼容SATA.具体来说,二者的兼容性主要体现在物理层和协议层的兼容. SAS系统的背板(Backplane)既可以连接具有双端口.高性能的SAS驱动器,也可以连接高容量.低成本 ...

  8. qt实现头像上传功能(朝十晚八的博客,一堆帖子)

    http://www.cnblogs.com/swarmbees/p/5688885.html

  9. 使用 Spring Data JPA 简化 JPA 开发

    从一个简单的 JPA 示例开始 本文主要讲述 Spring Data JPA,但是为了不至于给 JPA 和 Spring 的初学者造成较大的学习曲线,我们首先从 JPA 开始,简单介绍一个 JPA 示 ...

  10. 给java中的System.getProperty添加新的key value对

    由于系统被格了,所以,现在的java项目配置不对,代码里面的配置类调用了一个System.getProperty("env")发现找不到该变量的值,以前一直能找到的. 其实就是以前 ...