一路坎坷的不行,INF少个3f,WA。输入用的cin,超时。
题意就是陨石砸下来,砸下来的地方不能行走也要避开,砸下来的位置上下左右也是一样的,每个陨石砸下的时间位置都不一样,问从(0,0)要走多少步才能安全,可以先把所有表都作为无限时间的表,因为陨石砸下来,包括自己和他周围四个格一共五格,在t时间后,都会成为不能行走和要避开的地方,如果有重复覆盖的,那肯定是记先下落的时间,所以用无限大的话可以直接一次性记录完整张表,然后主角要避开的话,只要经过这个位置的时间小于陨石下落到这里的时间就行了。整张表就变成了一张陨石下落的时间表,比如样例中下落的四个点(0,0,2)(x坐标,y坐标,时间)
0 0 2 2 1 2 1 1 2 0 3 5
x/y | 0 | 1 | 2 | 3 | 4 | 5 |
0 | 2 | 2 | 2 | INF | INF | INF |
1 | 2 | 2 | 2 | 2 | INF | INF |
2 | 5 | 2 | 2 | INF | INF | INF |
3 | 5 | 5 | INF | INF | INF | INF |
4 | 5 | INF | INF | INF | INF | INF |
5 | INF | INF | INF | INF | INF | INF |
表中的数字,代表多少时间后要避开这里,只要从0,0点开始走,每走一格时间加1,走过当前的时间小于他下落的时间的话,就可以走,走到他现在所在的点没有陨石下落的位置的时候,就是安全的,当map[x][y]==INF。
#include<iostream>
#include<algorithm>
#include<queue>
#include<cstring>
#include<cstdio>
const int INF=0x3f3f3f3f;
using namespace std;
int m,map[305][305];
int vis[305][305];
int xy[][2]={{0,1},{0,-1},{1,0},{-1,0}};
struct node{
int x;
int y;
int t;
};
bool judge(int x,int y,int t)
{
return 0<=x&&x<305&&0<=y&&y<305&&map[x][y]>t;
}
int bfs()
{
memset(vis,0,sizeof(vis));
queue<node>q;
node start;//起点
start.x=0;
start.y=0;
start.t=0;
q.push(start);
vis[0][0]=1;
while(!q.empty())
{
node pos=q.front();
q.pop();
if(map[pos.x][pos.y]==INF)
return pos.t;
node next;
for(int i=0;i<4;i++)
{
next.x=pos.x+xy[i][0];
next.y=pos.y+xy[i][1];
next.t=pos.t+1;
if(judge(next.x,next.y,next.t))
if(!vis[next.x][next.y])
{
q.push(next);
vis[next.x][next.y]=1;
}
}
}
return -1;
}
int main()
{
scanf("%d",&m);
memset(map,INF,sizeof(map));
int x,y,t;
for(int i=0;i<m;i++)
{
scanf("%d%d%d",&x,&y,&t);
if(map[x][y]>t)
map[x][y]=t;
for(int k=0;k<4;k++)
{
int xx=x+xy[k][0];
int yy=y+xy[k][1];
if(judge(xx,yy,t))
map[xx][yy]=t;
}
}
int ans=bfs();
printf("%d\n",ans);
return 0;
}