POJ-3669 Meteor Shower(bfs)
http://poj.org/problem?id=3669
注意理解题意:有m颗行星将会落在方格中(第一象限),第i颗行星在ti时间会摧毁(xi,yi)这个点和四周相邻的点,一个人开始在原点,然后只能在第一象限内行走,每单位时间移动一格,只能移动到当前未摧毁的点,问多少时间能到达安全地方。
开始题意理解错了,没有明白每一颗行星最多摧毁5个点,我们可以预处理出被行星摧毁的点,然后用bfs搜索。
#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <cstring>
#include <string>
#include <algorithm>
#include <string>
#include <set>
#include <functional>
#include <numeric>
#include <sstream>
#include <stack>
//#include <map>
#include <queue> #define CL(arr, val) memset(arr, val, sizeof(arr)) #define ll long long
#define inf 0x7f7f7f7f
#define lc l,m,rt<<1
#define rc m + 1,r,rt<<1|1
#define pi acos(-1.0) #define L(x) (x) << 1
#define R(x) (x) << 1 | 1
#define MID(l, r) (l + r) >> 1
#define Min(x, y) (x) < (y) ? (x) : (y)
#define Max(x, y) (x) < (y) ? (y) : (x)
#define E(x) (1 << (x))
#define iabs(x) (x) < 0 ? -(x) : (x)
#define OUT(x) printf("%I64d\n", x)
#define lowbit(x) (x)&(-x)
#define Read() freopen("a.txt", "r", stdin)
#define Write() freopen("dout.txt", "w", stdout);
#define maxn 1000000000
#define N 1010
using namespace std; int dir[][]={-,,,,,,,-,,};
int vis[][];
struct node
{
int x,y,time;
}; int bfs()
{
if(vis[][]==-) return ;
node p;
p.x=p.y=p.time=;
queue<node>que;
que.push(p);
while(que.size())
{
node q=que.front();
que.pop();
for(int i=;i<;i++)
{
p=q;
p.x=q.x+dir[i][];
p.y=q.y+dir[i][];
p.time++;
if(p.x<||p.y<) continue; //出界了
if(vis[p.x][p.y]==-) return p.time; //到达安全地方
if(p.time<vis[p.x][p.y]) //当前这个点还未摧毁
{
vis[p.x][p.y]=p.time; //必须赋值,不然会超时,这样可以防止往回走的情况,节省不必要的搜索
que.push(p);
}
}
}
return -;
}
int main()
{
//freopen("a.txt","r",stdin);
int m,x,y,t;
scanf("%d",&m);
memset(vis,-,sizeof(vis));
while(m--)
{
scanf("%d%d%d",&x,&y,&t);
for(int i=;i<;i++)
{
int xx=x+dir[i][];
int yy=y+dir[i][];
if(xx>=&&yy>=) //预处理所有被摧毁的点,并赋值为被摧毁的最小时间。
{
if(vis[xx][yy]==-) vis[xx][yy]=t;
else vis[xx][yy]=min(t,vis[xx][yy]);
}
}
}
if(vis[][]==) printf("-1\n");
else printf("%d\n",bfs());
return ;
}
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 BFS 水~
http://poj.org/problem?id=3669 题目大意: 一个人从(0,0)出发,这个地方会落下陨石,当陨石落在(x,y)时,会把(x,y)这个地方和相邻的的四个地方破坏掉,求该人到达 ...
- 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& ...
随机推荐
- WebService流行框架之Axis和CXF
转自:http://www.cnblogs.com/snake-hand/archive/2013/06/09/3129915.html 前言 上节课我们对WebService进行了简单的介绍,对于其 ...
- JS中函数的基础知识
函数 一. 函数定义 函数又叫方法,在程序里面函数是用来执行某些特定功能的代码.为了减少重复使用代码,可以把特定功能的代码做成函数,需要使用时拿出来调用.alert();就是一个很常见的.简单的函数 ...
- spring mvc注解@RequestParam
在spring mvc 的使用过程中 获取 页面传来的参数的时候,我平时都习惯 @RequestParam String name,突然有一天我发现 直接在方法参数后面写 String name , ...
- unset之讲解
unset (PHP 4, PHP 5) unset — 释放给定的变量 说明¶ void unset ( mixed $var [, mixed $... ] ) unset() 销毁指定的变量. ...
- 关于gzip压缩
关于gzip压缩 http://httpd.apache.org/docs/2.0/mod/mod_deflate.html http://www.phpchina.com/resource/manu ...
- CSS自定义文件上传按钮
今天一同事问我文件上传按钮的问题,情况是这样的,他页面上有3个按钮,分为左中右三个,左边的位按钮甲,右边的位按钮乙,而中间的就是个文件选择按钮,情况大概是这个样子的: 两边的按钮都有了样式,但中间的选 ...
- Winform设置相关
>> Winform查找根目录 1) AppDomain.CurrentDomain.BaseDirectory 地址为: d:\MyProject\Bin\ Application. ...
- 导出含有图片的项目成jar文件后运行,图片不显示
在编写完Java程序后,打包成Jar时发布,会发现找不到Jar文件中的图片和文本文件,其原因是程序中载入图片或文本文件时,使用了以当前工作路径为基准的方式来指定文件和路径.这与用户运行Jar包时的当前 ...
- Libevent详细说明
文章来自官方文档的部分翻译:http://www.wangafu.net/~nickm/libevent-book/ 通过这部分的了解,基本上可以使用libevent的常用功能了.有时间建议直接看官方 ...
- POJ 3484
Showstopper Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1060 Accepted: 303 Descri ...