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做肯定出 ...
随机推荐
- 微信小程序源码案例大全
微信小程序demo:足球,赛事分析 小程序简易导航 小程序demo:办公审批 小程序Demo:电魔方 小程序demo:借阅伴侣 微信小程序demo:投票 微信小程序demo:健康生活 小程序demo: ...
- Find–atime –ctime –mtime的用法与区别总结
转自 周五有同事问起find命令中-mtime n.-mtime –n以及-mtime +n的用法区别,当时虽然记得这里n是n个24个小时的意思,也是对所有这几个属性详细的用法却一知半解,索性周末仔细 ...
- 【Shell脚本编程系列】Shell脚本开发的习惯和规范
1.开头指定脚本解释器 #!/bin/sh或#!/bin/bash 2.开头加版本版权信息 #Date #Author #Mail #Function #Version 提示:可配置vim编辑文件时自 ...
- Android SAX、DOM、Pull解析xml文件剖析与案例讲解
XML介绍 XML(Extensible Markup Language) 即可扩展标记语言,与HTML一样,都是SGML(Standard Generalized Markup Language,标 ...
- LeetCode 42 Trapping Rain Water(积水体积)
题目链接: https://leetcode.com/problems/trapping-rain-water/?tab=Description Problem: 根据所给数组的值,按照上图的示意 ...
- jenkins不安装任何插件时,是什么样的
因为jenkins中插件众多,很多时候大家默认安装的话,会默认安装一堆插件.而很多插件会给jenkins的各个方面去增强它, 一致于不清楚哪些功能是插件提供的,还是jenkins自带的. 所以下面将没 ...
- 使用PowerShell替代WinDbg在高分辨率笔记本下调试、排错
最近换新的开发笔记本,分辨率较高发现部分应用在High DPI下几乎没法用.比较纠结的就是WinDbg,频繁切换分辨率.显示文字大小影响工作节奏.幸好PowerShell在High DPI下效果不错. ...
- Linux 修改文件和文件夹权限
查看当前目录和文件权限 ls -l 文件属性 连接数 文件拥有者 所属群组 文件大小 文件修改时间 文件名drwxr-xr-x 4 william wheel 136 Apr 10 23:34 ven ...
- php中属性和方法的修饰符
<?php class A{ private function do1(){ echo "do1 called"; } protected function do2(){ e ...
- vim与终端的切换
在vim中按ctrl+z,可以stop vim退出到终端. 在终端工作完后,想要返回vim,则输入fg然后回车. ref: linux任务控制的几个技巧ctrl+z,fg,bg,jobs,kill