Meteor Shower
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 12816   Accepted: 3451

Description

Bessie hears that an extraordinary meteor shower is coming; reports say that these meteors will crash into earth and destroy anything they hit. Anxious for her safety, she vows to find her way to a safe location (one that is never destroyed by a meteor) . She is currently grazing at the origin in the coordinate plane and wants to move to a new, safer location while avoiding being destroyed by meteors along her way.

The reports say that M meteors (1 ≤ M ≤ 50,000) will strike, with meteor i will striking point (XiYi) (0 ≤ X≤ 300; 0 ≤ Y≤ 300) at time Ti (0 ≤ Ti  ≤ 1,000). Each meteor destroys the point that it strikes and also the four rectilinearly adjacent lattice points.

Bessie leaves the origin at time 0 and can travel in the first quadrant and parallel to the axes at the rate of one distance unit per second to any of the (often 4) adjacent rectilinear points that are not yet destroyed by a meteor. She cannot be located on a point at any time greater than or equal to the time it is destroyed).

Determine the minimum time it takes Bessie to get to a safe place.

Input

* Line 1: A single integer: M
* Lines 2..M+1: Line i+1 contains three space-separated integers: XiYi, and Ti

Output

* Line 1: The minimum time it takes Bessie to get to a safe place or -1 if it is impossible.

Sample Input

4
0 0 2
2 1 2
1 1 2
0 3 5

Sample Output

5
题意思路:m个陨石将砸向第一象限和正坐标抽,每块陨石砸的时间分别为Ti,从0,0出发,如何走才能达到安全位置;
用一个T[][]数组记录每个点的最早爆炸时间,vis标记该点是否被砸,如果该点不可能被砸,那么为安全位置,否则向四个方向扩展,改点可走的条件为改点没有走过以及该点爆炸的时间大于当前时间,对于走过的点标记T[][]为0,那么改点在以后就不可能再走。 反正我的思路好麻烦,每次都要写个函数对所有陨石进行遍历判断是否为安全位置。
AC:
 #include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#define INF 0x3f3f3f
#define eps 1e-8
#define MAXN (50000+1)
#define MAXM (100000)
#define Ri(a) scanf("%d", &a)
#define Rl(a) scanf("%lld", &a)
#define Rf(a) scanf("%lf", &a)
#define Rs(a) scanf("%s", a)
#define Pi(a) printf("%d\n", (a))
#define Pf(a) printf("%.2lf\n", (a))
#define Pl(a) printf("%lld\n", (a))
#define Ps(a) printf("%s\n", (a))
#define W(a) while(a--)
#define CLR(a, b) memset(a, (b), sizeof(a))
#define MOD 1000000007
#define LL long long
#define lson o<<1, l, mid
#define rson o<<1|1, mid+1, r
#define ll o<<1
#define rr o<<1|1
using namespace std;
struct Node{
int x, y, step;
};
bool judge(int x, int y){
return x >= && y >= ;
}
bool vis[][];
int Move[][] = {,, ,-, ,, -,};
int T[][];
int BFS(int x, int y)
{
if(!vis[x][y])
return ;
queue<Node> Q;
Node now, next;
now.x = x; now.y = y; now.step = ;
Q.push(now);
while(!Q.empty())
{
now = Q.front();
Q.pop();
if(!vis[now.x][now.y])
return now.step;
for(int k = ; k < ; k++)
{
next.x = now.x + Move[k][];
next.y = now.y + Move[k][];
next.step = now.step + ;
if(!judge(next.x, next.y)) continue;
if(vis[next.x][next.y])
{
if(T[next.x][next.y] != INF && next.step < T[next.x][next.y])
{
T[next.x][next.y] = ;
Q.push(next);
}
}
else
return next.step;
}
}
return -;
}
int main()
{
int n;
while(Ri(n) != EOF)
{
CLR(vis, false); CLR(T, INF);
for(int i = ; i < n; i++)
{
int x, y, t;
Ri(x); Ri(y); Ri(t);
T[x][y] = min(T[x][y], t);
vis[x][y] = true;
for(int k = ; k < ; k++)
{
int xx = x + Move[k][];
int yy = y + Move[k][];
if(!judge(xx, yy)) continue;
vis[xx][yy] = true;
T[xx][yy] = min(T[xx][yy], t);
}
}
Pi(BFS(, ));
}
return ;
}

WA:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
int vis[][],b[][];
struct node {int x,y,t;}m[];
bool cmp(node a,node b) {return a.t<=b.t;}
int n,Mint;
int dx[]={,,,,-},dy[]={,,-,,};
bool safe(node p)
{
int l=-,r=n,mid;
while(r-l>)
{
mid=(l+r)/;
if(m[mid].t>=p.t)
r=mid;
else
l=mid;
}
for(int i=r;i<n;i++)
{
for(int j=;j<;j++)
{
if(p.x==(m[i].x+dx[j])&&p.y==(m[i].y+dy[j]))
return ;
}
}
return ;
}
int bfs()
{
int i,j;
Mint=-;
queue<node> s;
node temp,next;
temp.x=temp.y=temp.t=;
s.push(temp);
while(!s.empty())
{
temp=s.front();
s.pop();
if(safe(temp))
{
Mint=temp.t;
break;
}
temp.t++;
int l=-,r=n,mid;
while(r-l>)
{
mid=(l+r)/;
if(m[mid].t>=temp.t)
r=mid;
else
l=mid;
}
while(m[r].t==temp.t)
{ for(int i=;i<=;i++)
b[m[r].x+dx[i]][m[r].y+dy[i]]=;
r++;
}
for(i=;i<=;i++)
{
next.x=temp.x+dx[i];
next.y=temp.y+dy[i];
next.t=temp.t;
if(next.x>=&&next.y>=&&vis[next.x][next.y]==&&b[next.x][next.y]==)
{
vis[next.x][next.y]=;
s.push(next);
}
//cout<<"ads";
}
}
return Mint;
}
int main()
{
int i,j;
freopen("in.txt","r",stdin);
while(scanf("%d",&n)!=EOF)
{
memset(vis,,sizeof(vis));
memset(b,,sizeof(vis));
for(i=;i<n;i++)
scanf("%d%d%d",&m[i].x,&m[i].y,&m[i].t);
sort(m,m+n,cmp);
//for(i=0;i<n;i++)
// cout<<m[i].x<<" "<<m[i y<<" "<<m[i].t<<endl;
i=;
bool flag=;
while(m[i].t==)
{
if((m[i].x==&&m[i].y==)||(m[i].x==&&m[i].y==)||(m[i].x==&&m[i].y==))
{
cout<<-<<endl;
flag=;
break;
}
i++;
}
if(flag)
continue;
bfs();
printf("%d\n",Mint);
}
}

Meteor Shower(POJ 3669)的更多相关文章

  1. POJ 3669 Meteor Shower (BFS+预处理)

    Description Bessie hears that an extraordinary meteor shower is coming; reports say that these meteo ...

  2. poj3669 Meteor Shower(预处理+bfs)

    https://vjudge.net/problem/POJ-3669 先给地图a[][]预处理每个位置被砸的最小时间.然后再bfs. 纯bfs,还被cin卡了下时间.. #include<io ...

  3. POJ 3669 Meteor Shower(流星雨)

    POJ 3669 Meteor Shower(流星雨) Time Limit: 1000MS    Memory Limit: 65536K Description 题目描述 Bessie hears ...

  4. poj 3669 Meteor Shower(bfs)

    Description Bessie hears that an extraordinary meteor shower is coming; reports say that these meteo ...

  5. 【POJ - 3669】Meteor Shower(bfs)

    -->Meteor Shower Descriptions: Bessie听说有场史无前例的流星雨即将来临:有谶言:陨星将落,徒留灰烬.为保生机,她誓将找寻安全之所(永避星坠之地).目前她正在平 ...

  6. 题解报告:poj 3669 Meteor Shower(bfs)

    Description Bessie hears that an extraordinary meteor shower is coming; reports say that these meteo ...

  7. 01背包问题:Charm Bracelet (POJ 3624)(外加一个常数的优化)

    Charm Bracelet    POJ 3624 就是一道典型的01背包问题: #include<iostream> #include<stdio.h> #include& ...

  8. Scout YYF I(POJ 3744)

    Scout YYF I Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5565   Accepted: 1553 Descr ...

  9. 广大暑假训练1(poj 2488) A Knight's Journey 解题报告

    题目链接:http://vjudge.net/contest/view.action?cid=51369#problem/A   (A - Children of the Candy Corn) ht ...

随机推荐

  1. jQuery验证表单格式

    工作之余整理一些工作中编写的代码,记录自己工作中的技术要点,便于自己记忆已经整合.以下是关于此jQuery验证的一些标记以及使用方法: 整个js代码都放入jquery_validate_1.1.0.j ...

  2. Gson解析复杂JSON对象

    例如以下格式JSON: 建立对应的Java对象,注意内部类要定义成静态的 public class HResult { public String total; public String recor ...

  3. CRC校验源码分析

    这两天做项目,需要用到 CRC 校验.以前没搞过这东东,以为挺简单的.结果看看别人提供的汇编源程序,居然看不懂.花了两天时间研究了一下 CRC 校验,希望我写的这点东西能够帮助和我有同样困惑的朋友节省 ...

  4. Powershell变量的类型和强类型

    Powershell变量的类型和强类型12 12月, 2011  在 Powershell  tagged Powershell教程 / 变量 / 存储 / 数据 / 类型 by Mooser Lee ...

  5. Advanced Bash-Scripting Guide

    在UC Berkeley 课程代号CS9E上看到的资源 http://www-inst.eecs.berkeley.edu/~selfpace/studyguide/cs9e/ http://www. ...

  6. flexbox自动完成

    1.引入文件 <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" ty ...

  7. spring 4 泛型注入

    最近对系统进行改造,发现在泛型实例初始化的时候,得不到想要的泛型.或者需要强制转换. spring 4 开始支持泛型对象初始化,初始化方法如下: 注:使用配置文件的方法暂时还没有发现,下面是使用jav ...

  8. Java vs Python

    面试时常问到这两种语言的区别,在此总结一下. Referrence: Udemy:python-vs-java Generally, Python is much simpler to use, an ...

  9. Windows 下如何安装配置Snort视频教程

    Windows 下如何安装配置Snort视频教程: 第一步: http://www.tudou.com/programs/view/UUbIQCng360/ 第二部: http://www.tudou ...

  10. BZOJ 4541 【HNOI2016】 矿区

    题目链接:矿区 这道题去年暑假就想写了,但是一直拖拉,以至于现在才来写这道题.以前一直在刻意回避几何类的题目,但到了现在这个时候,已经没有什么好害怕的了. 正巧今天神犇\(xzy\)讲了这道题,那我就 ...