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) 题意分析 圣诞节要到了,坤神和瑞瑞这对基佬想一起去召唤师大峡谷开开车.百度地图一下,发现周围的召唤师大峡谷还不少,这对基佬纠结着,该去哪一个...坤 ...
随机推荐
- 后端接收不到AngularJs中$http.post发送的数据的问题
1.问题: 后端接收不到AngularJs中$http.post发送的数据,总是显示为null 示例代码: $http.post(/admin/KeyValue/GetListByPage, { pa ...
- 移动前端之 zepto
移动前端之 zepto http://qtown.corp.qunar.com/media/video/detail?id=1084&type=1&title=%E5%86%AF%E5 ...
- tj
--统计set @collSql='select sum(case Ca_IssueType when 0 then 1 else 0 end) as IssueCount,sum(case when ...
- Unity3D GUI学习之GUI窗口的使用
GUI还可以定义一个窗口,在窗口里面进行控件的添加: using UnityEngine; using System.Collections; public class getbutton : Mon ...
- .net 学习路线感想
从上到大学到现在工作,已经有六年多了,发现学习编程到以开发为工作也是一个挺长的过程的. 大学中,从c语言到java.C#到其他各种语言的学习,还有其他知识的学习如:数据库(oracle.sql Ser ...
- ASP.net MVC 多语言处理
MVC多语言处理主要分两部分,一部分是Razor视图中的文字标签内容切换, 另一部分是javascript文件中的文标签内容切换. 这里分这两部分来说. View视图中的比较好做, 思路是使用资源文 ...
- padding and margin.
padding is the space between the content and the border, whereas margin is the space outside the bor ...
- iOS打包ipa安装包的流程
应用的发布也分两种 一种是.打包成ipa上传到国内第3方软件市场,当用户的手机已经JailBreak时,双击下载的ipa文件就可以安装软件 (ipa同android的apk包一样,实质是一个压缩包) ...
- vs2010安装路径解决不能修改的方法
环境:win7 64位 解决:网上说需要卸载以下4项 Microsoft Visual Studio Tools for Applications 2.0 - ENU Microsoft Visual ...
- 网络编程(学习整理)---2--(Udp)实现简单的控制台聊天室
1.UDP协议: 总结一下,今天学习的一点知识点! UDP也是一种通信协议,常被用来与TCP协议作比较!我们知道,在发送数据包的时候使用TCP协议比UDP协议安全,那么到底安全在哪里呢?怎么理解呢! ...