POJ - 3669 Meteor Shower

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 5
Sample Output

5


广搜。
这是一道关于广搜,炸弹在一定时间爆炸问题,需要注意的是炸弹爆炸的时候,周围四个方向(上 下 左 右 )都会在同一时间爆炸。在这里,需要用memeset将数据初始化为最小值或者最大值。这道题用队列进行查找最安全的路,在输入数据的时候,一定要注意根性最小值的方法,如果向下面第一组数据一样,虽然运行会对,但是每一次提交却一直WA,
会不会很伤心,下面是大家认为看着是对的但提交会错核心部分
scanf("%d%d%d",&a,&b,&c);
turn[4][2] = {1,0,0,1,0,-1,-1,0}
if(c <= e[a][b]) //注意加等于号,不然周围的四个方向不能够改变数值
{
e[a][b] = c;
for(int k = 0; k < 4; k++)
{

tx = a + turn[k][0];
ty = b + turn[k][1];
if(tx >= 0 && ty >= 0 && tx <= 410 && ty <= 410)
{
if(c < e[tx][ty])
e[tx][ty] = c;
}
}
}
但如果改变更新最小值的方法,答案就对。正确方法具体内容如下:
turn[5][2] = {1,0,0,1,0,-1,-1,0,0,0}
for( int i = 0; i < 5; i++ )
{
int tx = a + turn[i][0];
int ty = b + turn[i][1];
if( tx<0 || ty<0 ) continue;
if( e[tx][ty] == inf ) //我这里开始数据初始化为最大值。
e[tx][ty] = c;
else
e[tx][ty] = min(c,e[tx][ty]);
}
值得注意的是,不要想着那路已经走过了,就不用标记了,其实还是有必要标记一下的,不然会超时的



#include<queue>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define inf 0x3f3f3f3f

int turn[5][2] = {1,0,0,1,0,-1,-1,0,0,0};
int e[310][310], flag, book[310][310];

struct note
{
    int x,y,s;
} f[90100];

void bfs()
{
    queue<note>Q;
    note now,tmp;
    now.x = 0;
    now.y = 0;
    now.s = 0;
    Q.push(now);
    while(!Q.empty())
    {
        now = Q.front();
        Q.pop();
        if(e[0][0] == inf )
        {
            flag = 1;
            printf("%d\n",now.s);
            return ;
        }
        for(int i = 0; i < 4; i++)
        {
            tmp.x = now.x + turn[i][0];
            tmp.y = now.y + turn[i][1];
            tmp.s = now.s + 1;
            if(tmp.x >= 0 && tmp.x <= 310 && tmp.y >= 0 && tmp.y <= 310)
            {
                if(e[tmp.x][tmp.y] == inf )
                {
                    flag = 1;
                    printf("%d\n",tmp.s);
                    return ;
                }
                if(e[tmp.x][tmp.y] > tmp.s && !book[tmp.x][tmp.y] )
                {
                    book[tmp.x][tmp.y] = 1;
                    Q.push(tmp);
                }
            }
        }
    }
    return ;
}

int main()
{
    int n;
    while(~scanf("%d",&n))
    {
//        for(int i = 0; i <= 310; i++)
//            for(int j = 0; j <= 310; j++)
//                e[i][j] = inf;
        memset(e,inf,sizeof(e));
        int a,b,c,tx,ty;
        for(int i = 0; i < n; i++)
        {
            scanf("%d%d%d",&a,&b,&c);
            for( int j = 0; j < 5; j++ )
            {
                int tx = a + turn[j][0];
                int ty = b + turn[j][1];
                if( tx<0 || ty<0 ) continue;
                e[tx][ty] = min(c,e[tx][ty]);
            }
        }
        flag = 0;
        memset(book,0,sizeof(book));
        book[0][0] = 1;
        bfs();
        if(!flag)
            printf("-1\n");
    }
    return 0;



猜你喜欢

转载自blog.csdn.net/Qin7_Victory/article/details/75646965