您的位置:首页 > 其它

Meteor Shower(POJ 3669)

2016-01-22 10:59 447 查看
Meteor Shower

Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 12816Accepted: 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 (Xi, Yi) (0 ≤ Xi ≤ 300; 0 ≤ Yi ≤ 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: Xi, Yi, 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 >= 0 && y >= 0;
}
bool vis[301][301];
int Move[4][2] = {0,1, 0,-1, 1,0, -1,0};
int T[301][301];
int BFS(int x, int y)
{
if(!vis[x][y])
return 0;
queue<Node> Q;
Node now, next;
now.x = x; now.y = y; now.step = 0;
Q.push(now);
while(!Q.empty())
{
now = Q.front();
Q.pop();
if(!vis[now.x][now.y])
return now.step;
for(int k = 0; k < 4; k++)
{
next.x = now.x + Move[k][0];
next.y = now.y + Move[k][1];
next.step = now.step + 1;
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] = 0;
Q.push(next);
}
}
else
return next.step;
}
}
return -1;
}
int main()
{
int n;
while(Ri(n) != EOF)
{
CLR(vis, false); CLR(T, INF);
for(int i = 0; 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 = 0; k < 4; k++)
{
int xx = x + Move[k][0];
int yy = y + Move[k][1];
if(!judge(xx, yy)) continue;
vis[xx][yy] = true;
T[xx][yy] = min(T[xx][yy], t);
}
}
Pi(BFS(0, 0));
}
return 0;
}


WA:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
int vis[305][305],b[305][305];
struct node {int x,y,t;}m[50005];
bool cmp(node a,node b)    {return a.t<=b.t;}
int n,Mint;
int dx[]={0,0,0,1,-1},dy[]={0,1,-1,0,0};
bool safe(node p)
{
int l=-1,r=n,mid;
while(r-l>1)
{
mid=(l+r)/2;
if(m[mid].t>=p.t)
r=mid;
else
l=mid;
}
for(int i=r;i<n;i++)
{
for(int j=0;j<5;j++)
{
if(p.x==(m[i].x+dx[j])&&p.y==(m[i].y+dy[j]))
return 0;
}
}
return 1;
}
int bfs()
{
int i,j;
Mint=-1;
queue<node> s;
node temp,next;
temp.x=temp.y=temp.t=0;
s.push(temp);
while(!s.empty())
{
temp=s.front();
s.pop();
if(safe(temp))
{
Mint=temp.t;
break;
}
temp.t++;
int l=-1,r=n,mid;
while(r-l>1)
{
mid=(l+r)/2;
if(m[mid].t>=temp.t)
r=mid;
else
l=mid;
}
while(m[r].t==temp.t)
{

for(int i=0;i<=4;i++)
b[m[r].x+dx[i]][m[r].y+dy[i]]=1;
r++;
}
for(i=1;i<=4;i++)
{
next.x=temp.x+dx[i];
next.y=temp.y+dy[i];
next.t=temp.t;
if(next.x>=0&&next.y>=0&&vis[next.x][next.y]==0&&b[next.x][next.y]==0)
{
vis[next.x][next.y]=1;
s.push(next);
}
//cout<<"ads";
}
}
return Mint;
}
int main()
{
int i,j;
freopen("in.txt","r",stdin);
while(scanf("%d",&n)!=EOF)
{
memset(vis,0,sizeof(vis));
memset(b,0,sizeof(vis));
for(i=0;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=0;
bool flag=0;
while(m[i].t==0)
{
if((m[i].x==0&&m[i].y==0)||(m[i].x==0&&m[i].y==1)||(m[i].x==1&&m[i].y==0))
{
cout<<-1<<endl;
flag=1;
break;
}
i++;
}
if(flag)
continue;
bfs();
printf("%d\n",Mint);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: