Meteor Shower
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 18796 Accepted: 4879Description
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 5Sample Output
5Source
USACO 2008 February Silver
AC代码:
#include <iostream>
#include <cstdio>
#include <queue>
#include <algorithm>
#include <cstring>
using namespace std;
struct Node {
int i;
int j;
int t;
};
int a[310][310];
int vis [310][310];
int b[4][2] = {0,1,0,-1,1,0,-1,0};
void fun1 (int u, int v, int t)
{
for (int i = 0; i < 4; i++)
{
int u1, v1;
u1 = u + b[i][0];
v1 = v + b[i][1];
if (v1 >= 0 && u1 >= 0)
{
if (a[u1][v1] == -1)
a[u1][v1] = t;
else if(a[u1][v1] > t )
a[u1][v1] = t;
}
}
}
int bfs()
{
int ans = -1;
Node st;
st.i = 0;
st.j = 0;
st.t = 0;
queue <Node> Q;
vis[st.i][st.j] = 1;
Q.push(st);
while (Q.size() != 0)
{
Node t = Q.front();
Q.pop();
if (a[t.i][t.j] == -1) {ans = t.t;break;}
for (int i = 0; i < 4; i++)
{
Node temp;
temp.i = t.i + b[i][0];
temp.j = t.j + b[i][1];
temp.t = t.t + 1;
if (temp.i >=0 && temp.j >= 0 && vis[temp.i][temp.j] == 0)
{
if (a[temp.i][temp.j] == -1) {ans = temp.t; return ans;}
if (temp.t < a[temp.i][temp.j]) {Q.push(temp);vis[temp.i][temp.j] = 1; continue;}
}
}
}
return ans;
}
int main()
{
int M,ans = -1;
memset (a,-1,sizeof(a));
memset (vis,0,sizeof(vis));
scanf("%d",&M);
for (int i =0; i < M; i++)
{
int u,v,t;
scanf("%d%d%d",&u,&v,&t);
if (a[u][v] == -1)
a[u][v] = t;
else if(a[u][v] > t )
a[u][v] = t;
fun1 (u,v,t);
}
ans = bfs();
cout << ans << endl;
return 0;
}
题解:
1.BFS(注意判重)
2.一个方块和相邻四个方块会t时毁灭(注意比较每一格最小的t)
3.输入的t可能=0;数组a初值初始化为-1或极大值 。//我原先设的处置为0,error掉无数发!!!
4.大数据输入输出用scanf 和printf, 不要用cin/cout,用cin/cout , g++会tle,C++可过,不过不建议用!!