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) 题意分析 圣诞节要到了,坤神和瑞瑞这对基佬想一起去召唤师大峡谷开开车.百度地图一下,发现周围的召唤师大峡谷还不少,这对基佬纠结着,该去哪一个...坤 ...
随机推荐
- 前端高性能滚动 scroll 及页面渲染优化
前言 最近在研究页面渲染及web动画的性能问题,以及拜读<CSS SECRET>(CSS揭秘)这本大作.本文主要想谈谈页面优化之滚动优化. 主要内容包括了为何需要优化滚动事件,滚动与页面渲 ...
- oracle中创建表时添加注释
CREATE TABLE t1(id varchar2(32) primary key,name VARCHAR2(32) ,age VARCHAR2(32) );comment on column ...
- jQuery循环给某个ID赋值
1.id名为sl的input框循环赋值 $("input[id=sl]").each(function(){alert(this.value) })
- alpha属性设置
alpha是来设置透明度的,它的基本属性是filter:alpha(opacity,finishopacity,style,startX,startY,finishX,finishY).opacity ...
- c读mysql产生乱码问题
在编写接口API时,发现中文字utf8输入的在linux下采用c读取显示为”??”问号,这是由于编码造成的. 很简单的两个地方做修改就搞定. 1.先找到mysql的my.cnf配置文件/etc/my. ...
- 记一次网站服务器迁移(my)
遇到的难题: 基本没有遇到太大的问题,因为服务器环境是配好的阿里云环境,最后再nginx的rewrite配置遇到了一点问题,最后也算解决. 收获小点: 1) vim替换命令: 利用 :s 命令可以实现 ...
- 与数据库打交道的Adapter----SimpleCursorAdapter
http://www.cnblogs.com/wenjiang/p/3196486.html 程序员是这个世界上最神奇的职业,因为几乎所有其他职业的人都能转到该行来,只要他智力正常,有接受过正规的编程 ...
- IOS开发网络篇之──ASIHTTPRequest详解
目录 目录 发起一个同步请求 创建一个异步请求 队列请求 请求队列上下文 ASINetworkQueues, 它的delegate提供更为丰富的功能 取消异步请求 安全的内存回收建议 向服务器端上传数 ...
- C# div布局
本文讲解使用DIV+CSS布局最基本的内容,读完本文你讲会使用DIV+CSS进行简单的页面布局. 转载请标明:http://www.kwstu.com/ArticleView/divcss_20139 ...
- RESTful互联网框架
在我们日常接触的网络中,对于非程序员来说主要关注的就是在网上找到自己需要的资料,但是对于开发者来说,主要关注的就是将结构和页面,以及功能的分离,但是如何划分这个结构呢,或许我们知道的有MVC框架,甚至 ...