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. 缓存淘汰算法---LRU

    1. LRU1.1. 原理 LRU(Least recently used,最近最少使用)算法根据数据的历史访问记录来进行淘汰数据,其核心思想是“如果数据最近被访问过,那么将来被访问的几率也更高”. ...

  2. javascript深入理解js闭包[转]

    一.变量的作用域 要理解闭包,首先必须理解Javascript特殊的变量作用域. 变量的作用域无非就是两种:全局变量和局部变量. Javascript语言的特殊之处,就在于函数内部可以直接读取全局变量 ...

  3. C#【数据库】 Access类

    using System; using System.Data; using System.Data.OleDb; namespace AccessDb { /**//// <summary&g ...

  4. Flask_SqlAlchemy 1215, 'Cannot add f oreign key constraint'

    Flask_SqlAlchemy 1215, 'Cannot add f oreign key constraint'报错 sqlalchemy.exc.IntegrityError: (pymysq ...

  5. Find The Multiple(poj 1426)

    Description Given a positive integer n, write a program to find out a nonzero multiple m of n whose ...

  6. JavaScript encodeURI() 函数

    encodeURI() 函数可把字符串作为 URI 进行编码. -------------------------------------------------------------------- ...

  7. 6.ListView

    Repeater一般只用来展示数据,如果要增删改则用ListView更方便,使用向导(强类型数据)来使用ListView会自动生成很多模板,免去手写模板代码的麻烦,再进行手工调整即可. 首先设定数据源 ...

  8. Maven实战六

    转载:http://www.iteye.com/topic/1132509 一.简介 settings.xml对于maven来说相当于全局性的配置,用于所有的项目,当Maven运行过程中的各种配置,例 ...

  9. 几种任务调度的 Java 实现方法与比较Timer,ScheduledExecutor,Quartz,JCronTab

    几种任务调度的 Java 实现方法与比较 综观目前的 Web 应用,多数应用都具备任务调度的功能.本文由浅入深介绍了几种任务调度的 Java 实现方法,包括 Timer,Scheduler, Quar ...

  10. 图论(网络流):COGS 410. [NOI2009] 植物大战僵尸

    410. [NOI2009] 植物大战僵尸 ★★★   输入文件:pvz.in   输出文件:pvz.out   简单对比时间限制:2 s   内存限制:512 MB [问题描述] Plants vs ...