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层 ...
随机推荐
- Web前端的35个jQuery小技巧
1. 禁止右键点击 $(document).ready(function(){ $(document).bind("contextmenu",function(e){ ...
- jackson 解析结合类(需要传入Class, 和 Class.Class, 回调方法是List<Class>)
import java.util.HashMap; import java.util.List; import org.codehaus.jackson.map.ObjectMapper; impor ...
- jQuery scroll(滚动)延迟加载
延迟加载 $(window).scroll(function(){ var scrollHeight = $(document).height(); //文档高度 var scrollTop = $( ...
- Spark Streaming揭秘 Day17 资源动态分配
Spark Streaming揭秘 Day17 资源动态分配 今天,让我们研究一下一个在Spark中非常重要的特性:资源动态分配. 为什么要动态分配?于Spark不断运行,对资源也有不小的消耗,在默认 ...
- linux 输入子系统(2)----简单实例分析系统结构(input_dev层)
实例代码如下: #include <linux/input.h> #include <linux/module.h> #include <linux/init.h> ...
- 获取股票历史数据和当前数据的API
关键字:股票,stock,API,接口 1.获取股票当前数据 新浪数据接口:http://hq.sinajs.cn/list={code}.{code}替换为股票代码,沪市股票代码加前缀sh,深市股票 ...
- 2016年辛星less教程发布了
首先简介一下less吧,它是一个css预处理器.当我们的项目比较大的时候,我们的css的代码量也会急剧的膨胀,因此就有很多方案来让编程更加容易,没错,less就是这样一种技术. 在百度网盘的下载地址为 ...
- 编译andriod源码出错:java.lang.UnsupportedClassVersionError: com/google/doclava/Doclava : Unsupported
问题:java.lang.UnsupportedClassVersionError: com/google/doclava/Doclava : Unsupported update-java-alte ...
- 【css】 收藏 纯css打造 mackbook air
http://www.cnblogs.com/myvin/p/4621231.html <html lang="en"> <head> <meta c ...
- WPF 自定义窗口标题栏
1.建一个WPF资源词典,在其中定义窗口样式,并在App.xaml中指定其为程序资源 2.写一个继续自windows的类,并指定这个类的Style为第一步资源里的样式 3.新建窗口时,分别把xaml文 ...