您的位置:首页 > 其它

POJ 3669 Meteor Shower 挑战P135

2016-05-19 21:53 399 查看
题目大意:

给定一些点,这些点会在t时刻被毁灭。 同时,这个点上下左右4个点也被毁灭了。

问从(0,0)点出发,多久可以到一个安全的点。

我们可以计算出地图上所有的点, 被毁灭的最早时刻 TIME[i][j] 表示(i,j)这个点,在TIME[i][j] 以后,就变的不可访问了。

那么我们可以从(0,0)开始BFS,记录g[i][j]表示到(i,j)这个点,最早可以在啊[b]g[i][j]的时候到达。[/b]



[b]那么就可以直接BFS了, 如果   preTime表示前驱位置, 那么  g[preTime]表示前驱位置的时间, 那么到当前位置的时间显然就是[b] g[preTime] + 1[/b][/b]



只要保证 当前到k这个位置的时间,这个点没有被毁灭就行了。

然后BFS一下就行了。。不用回头!记住不用回头……

为什么不用回头呢。。因为边权是1啊…… 直接BFS就是最短路

#include <iostream>
#include <queue>
#include <cstdio>
#include <map>
#include <vector>
#include <set>
#include <algorithm>
#include <cstring>
using namespace std;
#define debug(k) cout<<k<<endl
#define debugA (cout<<"@"<<endl)

inline void sd(int &x){scanf("%d", &x);}
inline void sd(int &x, int &y){scanf("%d%d", &x, &y);}
inline void sd(int &x, int &y, int &z){scanf("%d%d%d", &x, &y, &z);}

int N;
int TIME[320][320];
int inf;
const int dx[]={-1,1,0,0,0};
const int dy[]={0,0,-1,1,0};

void init()
{
memset(TIME, 65, sizeof(TIME));
inf = TIME[0][0];
for (int i = 0; i != N; ++ i)
{
int x, y, t;
sd(x, y, t);
for (int j = 0; j != 5; ++ j)
{
int wx = x + dx[j];
int wy = y + dy[j];
if (wx<0 || wy <0) continue;
TIME[wx][wy] = min(TIME[wx][wy], t);
}
}
//debug
/*
for (int i = 0; i <= 5; ++ i)
{
for (int j = 0; j <= 5; ++j)
cout<<TIME[i][j]<<" "; cout<<endl;
}
*/

}

struct node
{
int x,y;
node(){}
node(int a,int b)
{
x=a;
y=b;
}
};

int g[350][350];

queue<node>q;
void bfs()
{
memset(g, 65, sizeof(g));
int ans = inf,nx, ny;
q.push(node(0,0));
g[0][0] = 0;
while (!q.empty())
{
nx=q.front().x;
ny=q.front().y;
q.pop();
if (TIME[nx][ny]==inf)
{
ans = min(ans, g[nx][ny]);
continue;
}
int nd = g[nx][ny];
for (int i = 0; i != 4; ++ i)
{
int wx=nx + dx[i];
int wy=ny + dy[i];
if (wx<0 || wy <0) continue;
if (g[wx][wy] != inf) continue;
if (TIME[wx][wy] <= nd + 1) continue;
g[wx][wy] = nd + 1;
q.push(node(wx, wy));
}
}
if (ans == inf) ans = -1;
printf("%d\n", ans);
}

int main()
{
while (EOF !=scanf("%d", &N))
{
init();
bfs();
}
return 0;
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: