【poj3669】Meteor Shower 搜索

来源:互联网 发布:在线教育源码 编辑:程序博客网 时间:2024/05/20 20:45

Meteor Shower

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 16355 Accepted: 4303

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

Sample Input

4
0 0 2
2 1 2
1 1 2
0 3 5

Sample Output

5

Source

USACO 2008 February Silver

题目大意:一个n*m 的网格,某些时刻可能会有流星掉下砸坏一个格子以及与它四联通的格子,砸坏的格子之后无法通行,求从起点到终点的最短时间或输出无解

就是一道bfs的搜索题,先预处理出每个点的破坏时间,然后bfs每个点的时候判断被破坏没有,没有才走

很简单的一道题,代码如下:

#include<iostream>#include<cstdio>#include<cstring>#include<string>#include<set>#include<queue>#include<algorithm>#include<vector>#include<cstdlib>#include<cmath>#include<ctime>#include<stack>#define INF 2100000000#define ll long long#define clr(x)  memset(x,0,sizeof(x));#define M 305using namespace std;int map[M][M],vis[M][M];int n;const int dx[]={0,1,0,-1,0},dy[]={0,0,1,0,-1};struct node{    int x,y,t;    node(int x=0,int y=0,int t=0):x(x),y(y),t(t){}};queue<node>Q;void bfs(){    Q.push(node(0,0,0));    while(!Q.empty())    {        node u=Q.front();Q.pop();        for(int i=1;i<=4;i++)        {            int x=u.x+dx[i],y=u.y+dy[i];            if(x<0||y<0||vis[x][y])continue;            vis[x][y]=1;            if(map[x][y]>1000)            {                cout<<u.t+1;                exit(0);            }            if(map[x][y]>u.t+1)Q.push(node(x,y,u.t+1));        }    }    cout<<-1;}int main(){    memset(map,63,sizeof(map));    cin>>n;    for(int i=1;i<=n;i++)    {        int a,b,c;        scanf("%d%d%d",&a,&b,&c);        map[a][b]=min(map[a][b],c);        if(a>0)map[a-1][b]=min(map[a-1][b],c);        if(b>0)map[a][b-1]=min(map[a][b-1],c);        map[a+1][b]=min(map[a+1][b],c);        map[a][b+1]=min(map[a][b+1],c);    }    if(map[0][0]>1000)    {        cout<<0;        return 0;    }    bfs();    return 0;}

大概就是这个样子,如果有什么问题,或错误,请在评论区提出,谢谢。

0 0
原创粉丝点击