POJ 3669Meteor Shower(传说中的流星雨 BFS)

Meteor Shower
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 6878   Accepted: 2001

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 (XiYi) (0 ≤ X≤ 300; 0 ≤ Y≤ 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: XiYi, 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


意思很简单,一直没调出来,最后发现,二维数组memset有问题,这个也不确定是什么问题,编译器出问题了??

每个流星在t时刻会损坏x,y这个点和周围的四个点,上下左右。问至少需要多少时间能到到永远安全的地方。先统计每个地方被摧毁的时间,如果没有的话,那么就是安全地带。

AC代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
int maxn=10000;
int dir[4][2]={{-1,0},{1,0},{0,1},{0,-1}};
int mp[305][305];
int visi[305][305];
struct node
{
    int x,y,t;
}nod;

int bfs(int x,int y)
{
    memset(visi,0,sizeof(visi));
    queue<node> mq;
    nod.x=x,nod.y=y,nod.t=0;
    visi[x][y]=1;
    mq.push(nod);

    while(!mq.empty())
    {
        int cx,cy,px,py,time;
        nod=mq.front();
        mq.pop();
        cx=nod.x,cy=nod.y,time=nod.t;

        if(mp[cx][cy]==maxn)
        {
            //cout<<time<<endl;
            return time;
        }
        for(int i=0;i<4;i++)
        {
            px=cx+dir[i][0],py=cy+dir[i][1];
            nod.x=px,nod.y=py,nod.t=time+1;
            if(px>=0&&py>=0&&!visi[px][py]&&nod.t<mp[px][py])
            {
                visi[px][py]=1;
                mq.push(nod);
            }
        }
    }

    return -1;
}

void debug()
{
    for(int i=0;i<5;i++)
    {
        for(int j=0;j<5;j++)
            cout<<mp[i][j]<<" ";
        cout<<endl;
    }
}

int main()
{
    int n,i,j;
    int x,y,tim,cx,cy;
    while(cin>>n)
    {

        //memset(mp,maxn,sizeof(mp));   用了竟然出问题?把maxn换成100也输不出来。。
        for(i=0;i<304;i++)
            for(j=0;j<304;j++)
                mp[i][j]=maxn;
        while(n--)
        {
            scanf("%d%d%d",&x,&y,&tim);
            mp[x][y]=min(mp[x][y],tim);
            for(i=0;i<4;i++)
            {
                cx=x+dir[i][0],cy=y+dir[i][1];
                if(cx>=0&&cy>=0)    mp[cx][cy]=min(mp[cx][cy],tim);
            }
        }

        //debug();
        int res=bfs(0,0);
        cout<<res<<endl;
    }
    return 0;
}

/*
4
0 0 2
2 1 2
1 1 2
0 3 5
*/



你可能感兴趣的:(bfs)