您的位置:首页 > 其它

POJ-3669-Meteor Shower

2014-02-20 11:30 459 查看
其实不难的一个BFS题目,写了好久~~最后发现是搜索完后流星还没攻击完的状况忽略了~该死!

代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
using namespace std;
const int maxn=405;
const int inf=1<<29;
struct Node
{
int x;
int y;
int t;
bool operator < (const Node &a)const
{
return t<a.t;
}
}a[51000];
int n,movex[4]={0,0,-1,1},movey[4]={1,-1,0,0};
queue<Node> q;
int dist[maxn][maxn];
bool isok(int x)
{
if(x<0||x>=maxn)
return false;
return true;
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
for(int i=0;i<n;i++)
scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].t);
for(int i=0;i<maxn;i++)
for(int j=0;j<maxn;j++)
dist[i][j]=inf;
sort(a,a+n);
Node node;
node.x=0;
node.y=0;
node.t=0;
dist[0][0]=0;
q.push(node);
int index=0;
while(!q.empty())
{
int x=q.front().x;
int y=q.front().y;
int t=q.front().t;
q.pop();
while(index<n&&a[index].t<=t)
{
dist[a[index].x][a[index].y]=-1;
for(int i=0;i<4;i++)
if(isok(a[index].x+movex[i])&&isok(a[index].y+movey[i]))
dist[a[index].x+movex[i]][a[index].y+movey[i]]=-1;
index++;
}
if(dist[x][y]==-1)
continue;
if(index==n)
{
while(!q.empty())
q.pop();
break;
}
for(int i=0;i<4;i++)
{
int itx=x+movex[i];
int ity=y+movey[i];
if(isok(itx)&&isok(ity)&&dist[itx][ity]!=-1&&dist[itx][ity]==inf)
{
node.x=itx;
node.y=ity;
node.t=t+1;
dist[itx][ity]=t+1;
q.push(node);
}
}
}
while(index<n)
{
dist[a[index].x][a[index].y]=-1;
for(int i=0;i<4;i++)
if(isok(a[index].x+movex[i])&&isok(a[index].y+movey[i]))
dist[a[index].x+movex[i]][a[index].y+movey[i]]=-1;
index++;
}
int ans=inf;
for(int i=0;i<maxn;i++)
for(int j=0;j<maxn;j++)
if(dist[i][j]!=-1)
ans=min(ans,dist[i][j]);
if(ans!=inf)
printf("%d\n",ans);
else
printf("-1\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: