poj 2049 Finding Nemo(优先队列+bfs)
题目:http://poj.org/problem?id=2049
题意:
有一个迷宫,在迷宫中有墙与门 有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)最少要穿过多少条门
代码是根据网上大神的稍微改了一下,就交了
#include <iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<stack>
#include<queue>
#include<cmath>
#include<algorithm>
using namespace std; #define maxn 250
#define inf 99999999
struct node
{
int x;
int y;
int len;
bool operator <(const node &a)const//运算符重载,就是根据len从小到大排序
{
return len>a.len;
}
}; int n,m,sx,sy,xmax,ymax;
int dis[maxn][maxn],h[maxn][maxn],l[maxn][maxn]; priority_queue<node>q;
void bfs()
{
int x,y;
for(x=; x<=xmax; x++)
{
for(y=; y<=ymax; y++)
dis[x][y]=inf;
}
while(!q.empty())q.pop();
node pn;
pn.x=;
pn.y=;
pn.len=;
dis[][]=;
q.push(pn);
while(!q.empty())
{
pn=q.top();
q.pop();
x=pn.x;
y=pn.y;
if(x==sx&&y==sy)return ; //向上走
if(y+<=ymax&&dis[x][y+]>dis[x][y]+h[x][y+])
{
dis[x][y+]=dis[x][y]+h[x][y+];
pn.x=x;
pn.y=y+;
pn.len=dis[x][y+];
q.push(pn);
}
if(y->=&&dis[x][y-]>dis[x][y]+h[x][y])//向下走
{
dis[x][y-]=dis[x][y]+h[x][y];
pn.x=x;
pn.y=y-;
pn.len=dis[x][y-];
q.push(pn);
}
if(x->=&&dis[x-][y]>dis[x][y]+l[x][y])//向左走
{
dis[x-][y]=dis[x][y]+l[x][y];
pn.x=x-;
pn.y=y;
pn.len=dis[x-][y];
q.push(pn);
}
if(x+<=xmax&&dis[x+][y]>dis[x][y]+l[x+][y])//向右走
{
dis[x+][y]=dis[x][y]+l[x+][y];
pn.x=x+;
pn.y=y;
pn.len=dis[x+][y];
q.push(pn);
}
}
dis[sx][sy]=-;
} int main()
{
int i,j,x,y,d,t;
double tx,ty;
while(scanf("%d%d",&n,&m)!=EOF)
{
if(m==-&&n==-)break;
xmax=ymax=-;
memset(h,,sizeof(h));
memset(l,,sizeof(l));
for(i=; i<n; i++)
{
scanf("%d%d%d%d",&x,&y,&d,&t);
if(d==)
{
for(j=; j<t; j++)
{
h[x+j][y]=inf;
}
xmax=max(xmax,x+t);
ymax=max(ymax,y);
}
else
{
for(j=; j<t; j++)
{
l[x][y+j]=inf;
}
xmax=max(xmax,x);
ymax=max(ymax,y+t);
}
}
for(i=; i<m; i++)
{
scanf("%d%d%d",&x,&y,&d);
if(d==)
{
h[x][y]=;
}
else l[x][y]=;
}
scanf("%lf%lf",&tx,&ty);
if(tx>xmax||ty>ymax)
{
printf("0\n");
}
else
{
sx=(int)tx;
sy=(int)ty;
bfs();
printf("%d\n",dis[sx][sy]);
}
}
}
poj 2049 Finding Nemo(优先队列+bfs)的更多相关文章
- POJ 2049— Finding Nemo(三维BFS)10/200
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u013497151/article/details/29562915 海底总动员.... 这个题開始 ...
- POJ 2049 Finding Nemo bfs 建图很难。。
Finding Nemo Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 6952 Accepted: 1584 Desc ...
- POJ 2049 Finding Nemo
Finding Nemo Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 8631 Accepted: 2019 Desc ...
- Finding Nemo(bfs)
Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 6988 Accepted: 1600 Description Nemo ...
- poj 2312 Battle City(优先队列+bfs)
题目链接:http://poj.org/problem?id=2312 题目大意:给出一个n*m的矩阵,其中Y是起点,T是终点,B和E可以走,S和R不可以走,要注意的是走B需要2分钟,走E需要一分钟. ...
- Finding Nemo 分类: POJ 2015-07-11 10:11 10人阅读 评论(0) 收藏
Finding Nemo Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 8117 Accepted: 1883 Desc ...
- POJ 2449 - Remmarguts' Date - [第k短路模板题][优先队列BFS]
题目链接:http://poj.org/problem?id=2449 Time Limit: 4000MS Memory Limit: 65536K Description "Good m ...
- hdu 1026 Ignatius and the Princess I【优先队列+BFS】
链接: http://acm.hdu.edu.cn/showproblem.php?pid=1026 http://acm.hust.edu.cn/vjudge/contest/view.action ...
- POJ 2251 Dungeon Master --- 三维BFS(用BFS求最短路)
POJ 2251 题目大意: 给出一三维空间的地牢,要求求出由字符'S'到字符'E'的最短路径,移动方向可以是上,下,左,右,前,后,六个方向,每移动一次就耗费一分钟,要求输出最快的走出时间.不同L层 ...
随机推荐
- linux下挂载移动硬盘ntfs格式
http://jingyan.baidu.com/article/f96699bba93dce894e3c1bec.html fdisk -l安装后 使用命令mount -t ntfs-3g /dev ...
- js鼠标及对象坐标控制属性详细解析
对js鼠标及对象坐标控制属性进行了详细的分析介绍. offsetTop获取对象相对于版面或由 offsetParent 属性指定的父坐标的计算顶端位置. offsetLeft获取对象相对于版面或由 ...
- JavaScript 高级程序设计 目录
为什么会写这个学习教程呢??因为一直以来,学习JavaScript都没有系统的学过,用什么学什么,所以今天开始,重新把JavaScript系统的学一遍!(本人也是菜鸟一枚,语文水平也还是小学程度,看得 ...
- PHP MYSQL 数据库配置连接
//配置文件 Config.php // 默认使用数据库配置 ***************************************** $Config['ConnectTag'] = 'de ...
- Nat网络地址转换
Nat中的术语 -------------------------------------------------------------------------------------------- ...
- DHT网络第一部分研究结果 加入长期在线的node
源码:http://jijiea.com/upfile/DHT_Part1_How_To_Join_In_DHT.zip
- Ubuntu安装google Gtest
(1) 下载源码:http://code.google.com/p/googletest/gtest-1.7.0 (2013)gtest-1.7.0 (2010) (2) README编译指南126 ...
- WPF简单入门总结
WPF简单总结 最近看了点关于WPF的东西,总结了点点入门的东西. XAML语法基础 1. 定义样式 <Window.Resources><!--窗体资源的定义--> < ...
- 内置对象之Cookie
if (!this.IsPostBack) { try { HttpCookie MyCookie = new HttpCookie("MyCookie"); MyCookie.V ...
- xcode7 app loader error itms 90168
. $ cd ~/.itmstransporter . $ rm update_check* . $ mv softwaresupport softwaresupport.bak . $ ...