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

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<queue>
using namespace std; int n,m,ex,ey;
int map[][],vis[][];
struct node
{
int x,y;
int step;
bool operator<(struct node b)const
{
return step > b.step;//按步数从小到大排序,每次取队列中步数较小的;
}
};
priority_queue <struct node> que; int bfs()
{
while(!que.empty())
que.pop();
que.push((struct node){,,});//从终点到起点
vis[][] = ;
while(!que.empty())
{
struct node u = que.top();
que.pop();
if(u.x == ex && u.y == ey)
return u.step;
if(map[u.x-][u.y] != && !vis[u.x-][u.y])
{
vis[u.x-][u.y] = ;
if(map[u.x-][u.y] == )
que.push((struct node){u.x-,u.y,u.step});
else que.push((struct node){u.x-,u.y,u.step+});
}
if(map[u.x+][u.y] != && !vis[u.x+][u.y])
{
vis[u.x+][u.y] = ;
if(map[u.x+][u.y] == )
que.push((struct node){u.x+,u.y,u.step});
else que.push((struct node){u.x+,u.y,u.step+});
}
if(map[u.x][u.y-] != && !vis[u.x][u.y-])
{
vis[u.x][u.y-] = ;
if(map[u.x][u.y-] == )
que.push((struct node){u.x,u.y-,u.step});
else que.push((struct node){u.x,u.y-,u.step+});
}
if(map[u.x][u.y+] != && !vis[u.x][u.y+])
{
vis[u.x][u.y+] = ;
if(map[u.x][u.y+] == )
que.push((struct node){u.x,u.y+,u.step});
else que.push((struct node){u.x,u.y+,u.step+});
}
}
return -;
}
int main()
{
int x,y,d,t;
while(~scanf("%d %d",&n,&m))
{
int i;
if(n == - && m == -)
break;
//1表示墙,2表示门,3表示空气
memset(map,,sizeof(map));
memset(vis,,sizeof(vis));
while(n--)
{
scanf("%d %d %d %d",&x,&y,&d,&t);
if(d == )
{
for(i = y*; i <= (y+t)*; i++)
map[x*][i] = ;
}
else
{
for(i = x*; i <= (x+t)*; i++)
map[i][y*] = ;
}
}
while(m--)
{
scanf("%d %d %d",&x,&y,&d);
if(d == )
map[x*][y*+] = ;
else
map[x*+][y*] = ;
}
double e_x,e_y;
scanf("%lf %lf",&e_x,&e_y);
if(e_x < || e_y < || e_x > || e_y > )
{
printf("0\n");
continue;
}
ex = (int)e_x*+;
ey = (int)e_y*+;
for(i = ; i <= ; i++)
map[i][] = map[][i] = map[][i] = map[i][] = ;
printf("%d\n",bfs());
}
return ;
}

Finding Nemo(bfs)的更多相关文章

  1. 深搜(DFS)广搜(BFS)详解

    图的深搜与广搜 一.介绍: p { margin-bottom: 0.25cm; direction: ltr; line-height: 120%; text-align: justify; orp ...

  2. 【算法导论】图的广度优先搜索遍历(BFS)

    图的存储方法:邻接矩阵.邻接表 例如:有一个图如下所示(该图也作为程序的实例): 则上图用邻接矩阵可以表示为: 用邻接表可以表示如下: 邻接矩阵可以很容易的用二维数组表示,下面主要看看怎样构成邻接表: ...

  3. 深度优先搜索(DFS)与广度优先搜索(BFS)的Java实现

    1.基础部分 在图中实现最基本的操作之一就是搜索从一个指定顶点可以到达哪些顶点,比如从武汉出发的高铁可以到达哪些城市,一些城市可以直达,一些城市不能直达.现在有一份全国高铁模拟图,要从某个城市(顶点) ...

  4. 【BZOJ5492】[HNOI2019]校园旅行(bfs)

    [HNOI2019]校园旅行(bfs) 题面 洛谷 题解 首先考虑暴力做法怎么做. 把所有可行的二元组全部丢进队列里,每次两个点分别向两侧拓展一个同色点,然后更新可行的情况. 这样子的复杂度是\(O( ...

  5. 深度优先搜索(DFS)和广度优先搜索(BFS)

    深度优先搜索(DFS) 广度优先搜索(BFS) 1.介绍 广度优先搜索(BFS)是图的另一种遍历方式,与DFS相对,是以广度优先进行搜索.简言之就是先访问图的顶点,然后广度优先访问其邻接点,然后再依次 ...

  6. 图的 储存 深度优先(DFS)广度优先(BFS)遍历

    图遍历的概念: 从图中某顶点出发访遍图中每个顶点,且每个顶点仅访问一次,此过程称为图的遍历(Traversing Graph).图的遍历算法是求解图的连通性问题.拓扑排序和求关键路径等算法的基础.图的 ...

  7. 数据结构与算法之PHP用邻接表、邻接矩阵实现图的广度优先遍历(BFS)

    一.基本思想 1)从图中的某个顶点V出发访问并记录: 2)依次访问V的所有邻接顶点: 3)分别从这些邻接点出发,依次访问它们的未被访问过的邻接点,直到图中所有已被访问过的顶点的邻接点都被访问到. 4) ...

  8. 层层递进——宽度优先搜索(BFS)

    问题引入 我们接着上次“解救小哈”的问题继续探索,不过这次是用宽度优先搜索(BFS). 注:问题来源可以点击这里 http://www.cnblogs.com/OctoptusLian/p/74296 ...

  9. HDU.2612 Find a way (BFS)

    HDU.2612 Find a way (BFS) 题意分析 圣诞节要到了,坤神和瑞瑞这对基佬想一起去召唤师大峡谷开开车.百度地图一下,发现周围的召唤师大峡谷还不少,这对基佬纠结着,该去哪一个...坤 ...

随机推荐

  1. CentOS+Nginx+PHP+MySQL详细配置(图解)

    原文地址: http://www.jb51.net/article/26597.htm CentOS+Nginx+PHP+MySQL详细配置(带有图解),需要的朋友可以参考下.   一.安装MySQL ...

  2. Linux 用户与用户组

    1  Linux 用户与组 Linux是一个多用户多任务操作系统.多用户可以在同一时间内登录同一个系统执行各自不同的任务,而互不影响. 1.1  Linux下用户的角色分类 Linux用户的角色分为3 ...

  3. 【bzoj2938】[Poi2000]病毒

    题目描述 二进制病毒审查委员会最近发现了如下的规律:某些确定的二进制串是病毒的代码.如果某段代码中不存在任何一段病毒代码,那么我们就称这段代码是安全的.现在委员会已经找出了所有的病毒代码段,试问,是否 ...

  4. 10.3 noip模拟试题

    希望[题目描述]网页浏览器者有后退与前进按钮,一种实现这两个功能的方式是用两个栈,“前进栈”.“后退栈”.这里你需要实现以下几个功能:BACK: 如果“后退栈”为空则忽略此命令. 否则将当前两面压入“ ...

  5. C#--Session用完如何清除

    Session.Abandon();//清除全部Session//清除某个SessionSession["UserName"] = null;Session.Remove(&quo ...

  6. HTML5 FileReader读取Blob对象API详解

    使用FileReader对象,web应用程序可以异步的读取存储在用户计算机上的文件(或者原始数据缓冲)内容,可以使用File对象或者Blob对象来指定所要读取的文件或数据.其中File对象可以是来自用 ...

  7. OpenGL ES 3.0 点,线,三角形绘制形式总结

    OpenGL ES 3.0 顶点     -1,  1, 0, -0.5f,  0, 0,     0, -1, 0,    -1,  0, 0, 0.5f,   0, 0,     1, -1,   ...

  8. Opencart 之 Registry 类详解

    Registry 中文意思是记录,登记,记录本的意思, 在opencart中他的用途就是 登记公共类.类的原型放在 system\engine文件夹下 代码很简单: <?php final cl ...

  9. while read line无法循环read文件

    while read line 与for循环的区别 例子:要从一个ip列表中获取ip.port,然后ssh ip 到目标机器进行特定的command操作ssh -o StrictHostKeyChec ...

  10. iOS打包ipa安装包的流程

    应用的发布也分两种 一种是.打包成ipa上传到国内第3方软件市场,当用户的手机已经JailBreak时,双击下载的ipa文件就可以安装软件 (ipa同android的apk包一样,实质是一个压缩包) ...