[Usaco2008 Feb]Meteor Shower流星雨

去年偶们湖南遭受N年不遇到冰冻灾害,现在芙蓉哥哥则听说另一个骇人听闻的消息: 一场流星雨即将袭击整个霸中,由于流星体积过大,它们无法在撞击到地面前燃烧殆尽, 届时将会对它撞到的一切东西造成毁灭性的打击。很自然地,芙蓉哥哥开始担心自己的 安全问题。以霸中至In型男名誉起誓,他一定要在被流星砸到前,到达一个安全的地方 (也就是说,一块不会被任何流星砸到的土地)。如果将霸中放入一个直角坐标系中, 芙蓉哥哥现在的位置是原点,并且,芙蓉哥哥不能踏上一块被流星砸过的土地。根据预 报,一共有M颗流星(1 <= M <= 50,000)会坠落在霸中上,其中第i颗流星会在时刻 T_i (0 <= T_i <= 1,000)砸在坐标为(X_i, Y_i) (0 <= X_i <= 300;0 <= Y_i <= 300) 的格子里。流星的力量会将它所在的格子,以及周围4个相邻的格子都化为焦土,当然 芙蓉哥哥也无法再在这些格子上行走。芙蓉哥哥在时刻0开始行动,它只能在第一象限中, 平行于坐标轴行动,每1个时刻中,她能移动到相邻的(一般是4个)格子中的任意一个, 当然目标格子要没有被烧焦才行。如果一个格子在时刻t被流星撞击或烧焦,那么芙蓉哥哥 只能在t之前的时刻在这个格子里出现。请你计算一下,芙蓉哥哥最少需要多少时间才能到 达一个安全的格子。



这题一看就是BFS即可。纯暴力

不过需要注意的是,人可以走的坐标并没有做限定。也就是说在(0,0) ----(300,300) 这个区域外的任何坐标都是安全的。这点需要注意

另外还要判断人刚开始所处的位置是否安全


 

#include <iostream>

#include <cstdio>

#include <cstring>

#include <cstdlib>

#include <string>

#include <map>

#include <algorithm>

#include <vector>

#include <queue>

#include <set>

#include <ctime>

#define INF 1000000007

using namespace std;

int v[333][333];

int m;

int vis[333][333];

int xx[] = {0, 0, -1, 1};

int yy[] = {1, -1, 0, 0};

struct node

{

    int x, y, num;

    node(){}

    node(int _x, int _y, int _num){x = _x; y = _y; num = _num;}

};

queue<node>q;

void bfs()

{

    node now = node(0, 0, 0);

    vis[0][0] = 1;

    if(v[0][0] > 0) q.push(now);

    int ans = -1;

    while(!q.empty())

    {

        now = q.front();

        q.pop();

        if(v[now.x][now.y] == INF)

        {

            ans = now.num;

            break;

        }

        for(int i = 0; i < 4; i++)

        {

            int tx = now.x + xx[i];

            int ty = now.y + yy[i];

            if(tx < 0 || ty < 0) continue;

            if(now.num + 1 < v[tx][ty] && !vis[tx][ty])

            {

                vis[tx][ty] = 1;

                q.push(node(tx, ty, now.num + 1));

            }

        }

    }

    printf("%d\n", ans);

}

int main()

{

    int x, y, t;

    for(int i = 0; i <= 330; i++)

        for(int j = 0; j <= 330; j++)

            v[i][j] = INF;

    scanf("%d", &m);

    while(m--)

    {

        scanf("%d%d%d", &x, &y, &t);

        v[x][y] = min(t, v[x][y]);

        if(x > 0) v[x - 1][y] = min(v[x - 1][y], t);

        if(y > 0) v[x][y - 1] = min(v[x][y - 1], t);

        v[x + 1][y] = min(v[x + 1][y], t);

        v[x][y + 1] = min(v[x][y + 1], t);

    }

    bfs();

    return 0;

}


 

 

你可能感兴趣的:(USACO)