POJ 3669 Meteor Shower BFS 水~
http://poj.org/problem?id=3669
题目大意:
一个人从(0,0)出发,这个地方会落下陨石,当陨石落在(x,y)时,会把(x,y)这个地方和相邻的的四个地方破坏掉,求该人到达安全地点的最小时间,若无解输出-1
思路:
好吧,我最近都在做水题。
输入的时候更新地图,每个点取最小的被破坏的时间,然后进行BFS,在BFS途中,如果当前时间>=被破坏的,那么就代表不能进入。
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
const int MAXN=300+10;
const int dx[]={1,-1,0,0,0};
const int dy[]={0,0,1,-1,0};
int map[MAXN][MAXN];
bool vis[MAXN][MAXN];
void update(int x,int y,int t)
{
if(x<0||y<0) return;
if(map[x][y] !=-1 && map[x][y]<=t) return;
map[x][y]=t;
} struct state
{
int x,y,t;
state(int x=0,int y=0,int t=0):x(x),y(y),t(t){}
}; int bfs()
{
memset(vis,0,sizeof(vis));
queue<state> q;
q.push(state(0,0,0));
while(!q.empty())
{
state cur=q.front();
q.pop();
for(int i=0;i<4;i++)
{
int nx=cur.x+dx[i];
int ny=cur.y+dy[i];
if( nx<0 || ny<0 ) continue;
if(map[nx][ny]==-1) return cur.t+1;
int nt=cur.t+1;
if( vis[nx][ny] || nt>=map[nx][ny]) continue;
q.push(state(nx,ny,nt));
vis[nx][ny]=true;
}
}
return -1;
}
int main()
{
int m;
while(~scanf("%d",&m))
{
memset(map,-1,sizeof(map));
for(int i=0;i<m;i++)
{
int x,y,t;
scanf("%d%d%d",&x,&y,&t);
for(int i=0;i<5;i++)
update(x+dx[i],y+dy[i],t);
}
printf("%d\n",bfs());
}
return 0;
}
POJ 3669 Meteor Shower BFS 水~的更多相关文章
- POJ 3669 Meteor Shower BFS求最小时间
Meteor Shower Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 31358 Accepted: 8064 De ...
- POJ 3669 Meteor Shower(流星雨)
POJ 3669 Meteor Shower(流星雨) Time Limit: 1000MS Memory Limit: 65536K Description 题目描述 Bessie hears ...
- POJ 3669 Meteor Shower【BFS】
POJ 3669 去看流星雨,不料流星掉下来会砸毁上下左右中五个点.每个流星掉下的位置和时间都不同,求能否活命,如果能活命,最短的逃跑时间是多少? 思路:对流星雨排序,然后将地图的每个点的值设为该点最 ...
- poj 3669 Meteor Shower(bfs)
Description Bessie hears that an extraordinary meteor shower is coming; reports say that these meteo ...
- POJ 3669 Meteor Shower (BFS+预处理)
Description Bessie hears that an extraordinary meteor shower is coming; reports say that these meteo ...
- 题解报告:poj 3669 Meteor Shower(bfs)
Description Bessie hears that an extraordinary meteor shower is coming; reports say that these meteo ...
- poj 3669 Meteor Shower
Me ...
- 【POJ 3669 Meteor Shower】简单BFS
流星雨撞击地球(平面直角坐标第一象限),问到达安全地带的最少时间. 对于每颗流星雨i,在ti时刻撞击(xi,yi)点,同时导致(xi,yi)和上下左右相邻的点在ti以后的时刻(包括t)不能再经过(被封 ...
- poj3669 Meteor Shower(BFS)
题目链接:poj3669 Meteor Shower 我只想说这题WA了后去看讨论才发现的坑点,除了要注意原点外,流星范围题目给的是[0,300],到302的位置就绝对安全了... #include& ...
随机推荐
- 为什么我们须要复杂的password
前两天我打开邮箱一看.收到公司1331一封要求改动邮箱password的邮件. 为什么我们须要一个复杂的password呢?尽管我一直以来设置的password都非常复杂.可是公司这次要求改动pass ...
- leetCode 82.Remove Duplicates from Sorted List II (删除排序链表的反复II) 解题思路和方法
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
- jquery10 闭包示例
o = { a:1, o:{ b:2, f : function(){ alert(o.a); alert(o.b);//undefined } } } o.o.f(); o = { a:7, o : ...
- 2011年度十大杰出IT博客获奖感言
2011年度十大杰出IT博客获奖感言 在各位评委.网友的支持下,我的博客从前50名中脱颖而出跻身10强,得到这个消息之后心中充满了喜悦.在这里要感谢51CTO为大家提供这样一个良好的展示平台. ...
- java接口理解(转载)
今天和同事好好的讨论了java接口的原理和作用,发现原来自己的对接口的理解仅仅是局限在概念的高度抽象上,觉得好像理解了但是不会变化应用其实和没有理解差不多.以前看一个帖子说学习一个东西不管什么时候都要 ...
- js闭包中注意文字总结
//闭包注意的点 //闭包中使用外部变量不是复制而是引用 //闭包可以节省传递参数问题 //在循环中使用闭包
- Stacked Autoencoders
转自:http://www.cnblogs.com/tornadomeet/archive/2013/03/25/2980357.html 如果使用多层神经网络的话,那么将可以得到对输入更复杂的函数表 ...
- fetch 封装
fetch.js var http = { get: function (url) { return new Promise((resolve, reject) => { fetch(url) ...
- JS错误记录 - dom操作 - 排序
本次练习错误总结: 1. for循环要套到按钮的onclick里面,否则onclick点击事件无法依次执行. 2. var n1, var n2 这两个变量是arr.sort排序使用的,所以应该放在s ...
- POJ--2516--Minimum Cost【最小费用最大流】
链接:http://poj.org/problem?id=2516 题意:有k种货物,n个客户对每种货物有一定需求量,有m个仓库.每一个仓库里有一定数量的k种货物.然后k个n*m的矩阵,告诉从各个仓库 ...