POJ:2049Finding Nemo(bfs+优先队列)
http://poj.org/problem?id=2049
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 题目大意:有一个迷宫,在迷宫中有墙与门
有m道墙,每一道墙表示为(x,y,d,t)
x,y表示墙的起始坐标
d为0即向右t个单位,都是墙
d为1即向上t个单位,都是墙
有n道门,每一道门表示为(x,y,d)
x,y表示门的起始坐标
d为0即向右一个单位表示门
d为1即向上一个单位表示门
再给出你起点的位置(f1,f2),并保证这个点的位置不会再墙或者门中,为起点到(0,0)最少要穿过多少条门。
题目解析:
这个题忒坑,让它坑死了,首先这个地图可能完全由空格组成,所以要判断n==0&&m==0的状况,其次那个死孩子可能在地图外面,所以要判断孩子的位置。
解决方案:
如果坐标的位置不乘2的话,在给墙赋完值后,在给窗户赋值就把墙的值给覆盖了,所以坐标乘2进行处理。
刚开始对于孩子的位置坐标怎么处理没想明白,之后懂了,(int)x*2+1,(int)y*2+1是奇数,
而坐标乘2之后都是偶数,又因为孩子不在墙上与窗户上,所以(int)x*2+1,(int)y*2+1还是在原本孩子
呆的范围内。因为两墙之内最短的距离是2。
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <queue>
using namespace std;
int map[][],v[][];
int n,m,maxx,maxy;
float tx,ty;
struct node
{
int ans;
int x,y;
friend bool operator<(struct node a,struct node b)//按ans从小到大排序
{
return a.ans>b.ans;
}
};
struct node t,f;
int jx[]= {,-,,};
int jy[]= {,,,-};
void bfs(int ww,int ee)
{
priority_queue<node>q;
memset(v,,sizeof(v));
t.x=ww;
t.y=ee;
t.ans=;
q.push(t);
v[ww][ee]=;
while(!q.empty())
{
t=q.top();
q.pop();
if(t.x==&&t.y==)
{
printf("%d\n",t.ans);
return ;
}
for(int i=; i<; i++)
{
f.x=t.x+jx[i];
f.y=t.y+jy[i];
if(f.x>=&&f.x<=maxx&&f.y>=&&f.y<=maxy&&v[f.x][f.y]==&&map[f.x][f.y]!=)
{
if(map[f.x][f.y]==)
{
f.ans=t.ans;
}
else if(map[f.x][f.y]==)
{
f.ans=t.ans+;
}
q.push(f);
v[f.x][f.y]=;
}
}
}
printf("-1\n");
}
int main()
{
int xx,yy,zz,ww;
while(scanf("%d%d",&m,&n)!=EOF)
{
if(n==-&&m==-) break;
maxx=-;
maxy=-;
memset(map,,sizeof(map));
for(int i=; i<=m; i++)
{
scanf("%d%d%d%d",&xx,&yy,&zz,&ww);
if(zz==)
{
for(int j=xx*; j<=(xx+ww)*; j++)
{
map[j][yy*]=;//墙是2
}
if(maxx<(xx+ww)*)
maxx=(xx+ww)*;
if(maxy<yy*)
maxy=yy*;
}
else if(zz==)
{
for(int j=yy*; j<=(yy+ww)*; j++)
{
map[xx*][j]=;
}
if(maxy<(yy+ww)*)
maxy=(yy+ww)*;
if(maxx<xx*)
maxx=xx*;
}
}
for(int i=; i<=n; i++)
{
scanf("%d%d%d",&xx,&yy,&zz);
if(zz==)
{
map[xx*+][yy*]=;//路是1
}
else if(zz==)
{
map[xx*][yy*+]=;
}
}
scanf("%f%f",&tx,&ty);
if(!(tx>=&&tx<=&&ty>=&&ty<=))//他在迷宫之外
{
printf("0\n");
}
else if(!n&&!m)
printf("0\n");
else
{
int ww=(int)tx*+;
int ee=(int)ty*+;
maxx+=;
maxy+=;
bfs(ww,ee);
} }
return ;
}
POJ:2049Finding Nemo(bfs+优先队列)的更多相关文章
- POJ 1724 ROADS(BFS+优先队列)
题目链接 题意 : 求从1城市到n城市的最短路.但是每条路有两个属性,一个是路长,一个是花费.要求在花费为K内,找到最短路. 思路 :这个题好像有很多种做法,我用了BFS+优先队列.崔老师真是千年不变 ...
- Meteor Shower POJ - 3669 (bfs+优先队列)
Meteor Shower Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 26455 Accepted: 6856 De ...
- POJ - 2312 Battle City BFS+优先队列
Battle City Many of us had played the game "Battle city" in our childhood, and some people ...
- poj 3253 Fence Repair 优先队列
poj 3253 Fence Repair 优先队列 Description Farmer John wants to repair a small length of the fence aroun ...
- hdu 1242 找到朋友最短的时间 (BFS+优先队列)
找到朋友的最短时间 Sample Input7 8#.#####. //#不能走 a起点 x守卫 r朋友#.a#..r. //r可能不止一个#..#x.....#..#.##...##...#.... ...
- HDU 1428 漫步校园 (BFS+优先队列+记忆化搜索)
题目地址:HDU 1428 先用BFS+优先队列求出全部点到机房的最短距离.然后用记忆化搜索去搜. 代码例如以下: #include <iostream> #include <str ...
- hdu1839(二分+优先队列,bfs+优先队列与spfa的区别)
题意:有n个点,标号为点1到点n,每条路有两个属性,一个是经过经过这条路要的时间,一个是这条可以承受的容量.现在给出n个点,m条边,时间t:需要求在时间t的范围内,从点1到点n可以承受的最大容量... ...
- BFS+优先队列+状态压缩DP+TSP
http://acm.hdu.edu.cn/showproblem.php?pid=4568 Hunter Time Limit: 2000/1000 MS (Java/Others) Memo ...
- HDU 1242 -Rescue (双向BFS)&&( BFS+优先队列)
题目链接:Rescue 进度落下的太多了,哎╮(╯▽╰)╭,渣渣我总是埋怨进度比别人慢...为什么不试着改变一下捏.... 開始以为是水题,想敲一下练手的,后来发现并非一个简单的搜索题,BFS做肯定出 ...
随机推荐
- WebService连接postgresql( 失败尝试)
一.先进行postgresql的配置 1. 安装ODBC驱动 下载地址:http://www.postgresql.org/ftp/odbc/versions/msi/ 2. 打开 控制面板 -&g ...
- 如何在浏览器中简单模拟微信浏览器(仅限于通过User Agent进行判断的页面)
模拟微信浏览器: .打开360极速 .F12开发者工具 .开发者模式左上方有一个手机样子的图标 点击进入 设备模式‘ .将UA选项中的字符串替换成: Mozilla/ 备注: 替换的字符串是微信浏览器 ...
- web应用安全防范(1)—为什么要重视web应用安全漏洞
现在几乎所有的平台都是依赖于互联网构建核心业务的. 自从XP年代开始windows自带防火墙后,传统的缓冲器溢出等攻击失去了原有威力,黑客们也把更多的目光放在了WEB方面,直到进入WEB2.0后,WE ...
- liunx trac 插件使用之GanttCalendarPlugin
http://trac-hacks.org/wiki/GanttCalendarPlugin官网上的说明很清楚,处理做几点提示,以做记录. 1.我的Trac版本是1.0.1 我使用了'B' Metho ...
- Word 2010 制作文档结构之页码从正文开始设置
一般技术性文档结构划分: 第一页(首页) 第二页(修改记录页/版本记录页) 第三页(目录) 第四页(正文) 需求: 页脚编码 从正文(即第四页)开始,而不是从首页开始,那么该如何实现? 前提准备: 输 ...
- Elasticsearch学习之深入搜索二 --- 搜索底层原理剖析
1. 普通match如何转换为term+should { "match": { "title": "java elasticsearch"} ...
- html5里面的延迟加载属性
html5中给script标签引入了 async 和 defer 属性. 原理:带有async属性的script标签,会在浏览器解析时立即下载脚本同时不阻塞后续的document渲染和script加载 ...
- Linux下socket最大连接数 ulimit -n 最大值修改
请求多的Linux服务器,如不改最大打开文件数的话,那是一个悲剧~可以用命令 ulimit -n 看看当前最大可打开文件数 默认是1024如果加大呢?临时方法是ulimit -n 8192 这个方法是 ...
- os.walk的用法
import os path = 'C:\\aa' for root,dirs,files in os.walk(path): print("Root=",root,'dirs=' ...
- ansible的优化
Ansible企业实战环境中,如果管理的服务器越来越多,Ansibe执行效率会变得比较慢,可以通过优化Ansible提供工作效率,由于Ansible基于SSH协议通信,SSH连接慢会导致整个基于Ans ...