Finding Nemo(bfs)
Time Limit: 2000MS | Memory Limit: 30000K | |
Total Submissions: 6988 | Accepted: 1600 |
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
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
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)的更多相关文章
- 深搜(DFS)广搜(BFS)详解
图的深搜与广搜 一.介绍: p { margin-bottom: 0.25cm; direction: ltr; line-height: 120%; text-align: justify; orp ...
- 【算法导论】图的广度优先搜索遍历(BFS)
图的存储方法:邻接矩阵.邻接表 例如:有一个图如下所示(该图也作为程序的实例): 则上图用邻接矩阵可以表示为: 用邻接表可以表示如下: 邻接矩阵可以很容易的用二维数组表示,下面主要看看怎样构成邻接表: ...
- 深度优先搜索(DFS)与广度优先搜索(BFS)的Java实现
1.基础部分 在图中实现最基本的操作之一就是搜索从一个指定顶点可以到达哪些顶点,比如从武汉出发的高铁可以到达哪些城市,一些城市可以直达,一些城市不能直达.现在有一份全国高铁模拟图,要从某个城市(顶点) ...
- 【BZOJ5492】[HNOI2019]校园旅行(bfs)
[HNOI2019]校园旅行(bfs) 题面 洛谷 题解 首先考虑暴力做法怎么做. 把所有可行的二元组全部丢进队列里,每次两个点分别向两侧拓展一个同色点,然后更新可行的情况. 这样子的复杂度是\(O( ...
- 深度优先搜索(DFS)和广度优先搜索(BFS)
深度优先搜索(DFS) 广度优先搜索(BFS) 1.介绍 广度优先搜索(BFS)是图的另一种遍历方式,与DFS相对,是以广度优先进行搜索.简言之就是先访问图的顶点,然后广度优先访问其邻接点,然后再依次 ...
- 图的 储存 深度优先(DFS)广度优先(BFS)遍历
图遍历的概念: 从图中某顶点出发访遍图中每个顶点,且每个顶点仅访问一次,此过程称为图的遍历(Traversing Graph).图的遍历算法是求解图的连通性问题.拓扑排序和求关键路径等算法的基础.图的 ...
- 数据结构与算法之PHP用邻接表、邻接矩阵实现图的广度优先遍历(BFS)
一.基本思想 1)从图中的某个顶点V出发访问并记录: 2)依次访问V的所有邻接顶点: 3)分别从这些邻接点出发,依次访问它们的未被访问过的邻接点,直到图中所有已被访问过的顶点的邻接点都被访问到. 4) ...
- 层层递进——宽度优先搜索(BFS)
问题引入 我们接着上次“解救小哈”的问题继续探索,不过这次是用宽度优先搜索(BFS). 注:问题来源可以点击这里 http://www.cnblogs.com/OctoptusLian/p/74296 ...
- HDU.2612 Find a way (BFS)
HDU.2612 Find a way (BFS) 题意分析 圣诞节要到了,坤神和瑞瑞这对基佬想一起去召唤师大峡谷开开车.百度地图一下,发现周围的召唤师大峡谷还不少,这对基佬纠结着,该去哪一个...坤 ...
随机推荐
- asp.net 事件模型
asp.net的原始设计构想,就是要让开发人员能够像 VB 开发工具那样,可以使用事件驱动式程序开发模式 (Event-Driven Programming Model) 的方法来开发网页与应用程序, ...
- oracle中创建表时添加注释
CREATE TABLE t1(id varchar2(32) primary key,name VARCHAR2(32) ,age VARCHAR2(32) );comment on column ...
- wampserver 2.4 配置虚拟主机
最近用到了wamp环境,想创建一个虚拟主机,可是忘记了,于是百度了一下,把它写下来: 环境wampserver 2.4 找到安装目录,进入apache安装目录:找到conf 下的 httpd.conf ...
- jquery生成UUID的方法
来源: http://www.broofa.com/2008/09/javascript-uuid-function/ 1.代码: http://www.broofa.com/Tools/Math ...
- Bootstrap Table的例子(转载)
转载自:http://wenzhixin.net.cn/p/bootstrap-table/docs/examples.html#classes-table 使用的API: data1.json da ...
- Struts2 中拦截器和Action的调用关系(写的很好)
http://blog.csdn.net/hackerain/article/details/6991082
- 1.swt/rap学习源码网址
1.rap使用JS/ RAP加载JS http://download.eclipse.org/rt/rap/doc/2.3/guide/reference/jsdoc/symbols/rap.html ...
- WPF DataBinding之我见
原创,转载请注明出处:WPF DataBinding之我见 一.DataBinding介绍 数据绑定是在应用程序 UI 与业务逻辑之间建立连接的过程. 如果绑定具有正确设置并且数据提供正确通知,则 ...
- 【POJ3580】【块状链表】SuperMemo
Description Your friend, Jackson is invited to a TV show called SuperMemo in which the participant i ...
- Python自动化运维之24、JQuery
jQuery是一个兼容多浏览器的javascript库,核心理念是write less,do more(写得更少,做得更多).它是轻量级的js库 ,它兼容CSS3,还兼容各种浏览器(IE 6.0+, ...