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. [原]Sublime Text3 搭建16位汇编环境(windows)

    最近在学习王爽的<汇编程序>,参考<简单OS开发前奏<一>EDITPLUS+MASM32搭建汇编开发环境(16位+32位)>http://www.cnblogs.c ...

  2. REDIS主从配置及切换

    最近公司项目也用上了.但有个点没有弄很明白. requirepass只是给CLIENT认证用的,还是同时给从REDIS认证用的?(masterauth) 一般的思路是主服务器不开AOF,从服务器打开A ...

  3. Android导航菜单横向左右滑动并和下方的控件实现联动

    这个是美团网个人订单的效果,找了很多地方都没找到,自己研究了两天终于弄出来了^_^,有什么问题希望大家指出来,谢谢. 实现原理是上方使用HorizontalScrollView这个可以水平横向拖动的控 ...

  4. ActionBar Fragment运用最佳实践

    ActionBar Fragment运用最佳实践  

  5. opencart修改后台文件夹名

    在使用opencart进行二次开发时,若需要修改后台目录的文件夹名是可以操作的.具体步骤如下: 1.将网站后台文件夹名字改成opencartadmin 2.在该文件夹下找到config.php文件如图 ...

  6. 【PAT L2-001】最短路计数

    给定一个无向带权网络,无负边,无重边和自环,每个顶点有一个正数权值.首先求特定原点s到终点d的最短路的个数:然后求所有最短路中顶点权值a[i]之和最大的那条,输出这条路径. 可用dijkstra算法求 ...

  7. Java中ThreadLocal无锁化线程封闭实现原理

    虽然现在可以说很多程序员会用ThreadLocal,但是我相信大多数程序员还不知道ThreadLocal,而使用ThreadLocal的程序员大多只是知道其然而不知其所以然,因此,使用ThreadLo ...

  8. spring MVC上传文件演示

    //相比smartUpload功能上感觉确实有点心有意力不足的感觉,就安全性判断后缀,smartUpload就非常方便. public ModelAndView addFileUp(HttpServl ...

  9. 【多线程】--生产者消费者模式--Lock版本

    在JDK1.5发布后,提供了Synchronized的更优解决方案:Lock 和 Condition 我们使用这些新知识,来改进例子:[多线程]--生产者消费者模式--Synchronized版本 改 ...

  10. Linux下使用JNI的常见问题及解决方案

    JNI是java和C/C++混合编程的接口,可以很方便地实现java调用C/C++语言.具体的使用方法,网上有很多教程,在此不做过多介绍.本博客只关注在使用JNI的过程中的常见问题. 1.     生 ...