http://poj.org/problem?id=2049

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 题目大意:有一个迷宫,在迷宫中有墙与门
有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+优先队列)的更多相关文章

  1. POJ 1724 ROADS(BFS+优先队列)

    题目链接 题意 : 求从1城市到n城市的最短路.但是每条路有两个属性,一个是路长,一个是花费.要求在花费为K内,找到最短路. 思路 :这个题好像有很多种做法,我用了BFS+优先队列.崔老师真是千年不变 ...

  2. Meteor Shower POJ - 3669 (bfs+优先队列)

    Meteor Shower Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 26455   Accepted: 6856 De ...

  3. POJ - 2312 Battle City BFS+优先队列

    Battle City Many of us had played the game "Battle city" in our childhood, and some people ...

  4. poj 3253 Fence Repair 优先队列

    poj 3253 Fence Repair 优先队列 Description Farmer John wants to repair a small length of the fence aroun ...

  5. hdu 1242 找到朋友最短的时间 (BFS+优先队列)

    找到朋友的最短时间 Sample Input7 8#.#####. //#不能走 a起点 x守卫 r朋友#.a#..r. //r可能不止一个#..#x.....#..#.##...##...#.... ...

  6. HDU 1428 漫步校园 (BFS+优先队列+记忆化搜索)

    题目地址:HDU 1428 先用BFS+优先队列求出全部点到机房的最短距离.然后用记忆化搜索去搜. 代码例如以下: #include <iostream> #include <str ...

  7. hdu1839(二分+优先队列,bfs+优先队列与spfa的区别)

    题意:有n个点,标号为点1到点n,每条路有两个属性,一个是经过经过这条路要的时间,一个是这条可以承受的容量.现在给出n个点,m条边,时间t:需要求在时间t的范围内,从点1到点n可以承受的最大容量... ...

  8. BFS+优先队列+状态压缩DP+TSP

    http://acm.hdu.edu.cn/showproblem.php?pid=4568 Hunter Time Limit: 2000/1000 MS (Java/Others)    Memo ...

  9. HDU 1242 -Rescue (双向BFS)&amp;&amp;( BFS+优先队列)

    题目链接:Rescue 进度落下的太多了,哎╮(╯▽╰)╭,渣渣我总是埋怨进度比别人慢...为什么不试着改变一下捏.... 開始以为是水题,想敲一下练手的,后来发现并非一个简单的搜索题,BFS做肯定出 ...

随机推荐

  1. 【转载】如何从win8/8.1中文版(核心版)升级到win8/8.1专业版

    最近帮助很多同学从win8/8.1的基础版本 - 中文版(核心版)升级到了专业版,经过咨询,升级系统的最主要原因是中文版(核心版)的功能限制,因为基础版本阉割掉了很多常用的功能,比如组策略,计算机管理 ...

  2. 【十大算法实现之KNN】KNN算法实例(含测试数据和源码)

    KNN算法基本的思路是比较好理解的,今天根据它的特点写了一个实例,我会把所有的数据和代码都写在下面供大家参考,不足之处,请指正.谢谢! update:工程代码全部在本页面中,测试数据已丢失,建议去UC ...

  3. spring mvc 跨域请求处理——spring 4.2 以上

    Controller method CORS configuration You can add to your @RequestMapping annotated handler method a  ...

  4. css方法 - 移动端h5在iphonex的适配

    @media only screen and (device-width:375px) and (device-height:812px) and (-webkit-device-pixel-rati ...

  5. Ubuntu Releases 版本下载站

    http://releases.ubuntu.com/

  6. C++的函数重载和main函数之外的工作

    今天被问到一个C++的函数重载问题,一下子没反应过来,这种基础的问题竟然忘记了,以下记录一下这些忘记的内容.     函数重载 函数重载的定义是:在相同的作用域中,如果函数具有相同名字而仅仅是形参表不 ...

  7. GDI+绘制半圆按钮

    新建一个用户控件: public partial class UserControl1 : UserControl { public UserControl1() { InitializeCompon ...

  8. 【CF662A】Gambling Nim 线性基

    [CF662A]Gambling Nim 题意:n长卡牌,第i张卡牌正面的数字是$a_i$,反面的数字是$b_i$,每张卡牌等概率为正面朝上或反面朝上.现在Alice和Bob要用每张卡牌朝上的数字玩N ...

  9. C++ 输入/输出

    std:: 是什么?有什么作用? 输入和输出的iostream 库.iostream 库的基础是两种命名为 istream 和 ostream 的类型,分别表示输入流和输出流. 标准库定义了 4 个 ...

  10. spring boot 部署

    指定运行的内存 java -Xms10m -Xmx200m -jar xxx.jar spring boot 打包成war包: 让 SpringbootApplication 类继承 SpringBo ...