poj 3669(BFS)

Meteor Shower
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 25105   Accepted: 6487

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

题目大意:输入N组数据,前两个代表(x,y)后面代表什么时间会落下流星,求最少的移动步数

类似迷宫问题,肯定是广搜,我用了深搜之后TLE了,这道题最主要的是处理每次流星落下的时间,这里就用最开始的时间表示,假设在(1,1)的时候3s 的时候落一次5s 的时候落一次,肯定保留3,如果你在3s前已经经过了,那么以后不管有没有流星落下,都不会砸中。

#include<iostream>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
struct node
{
    int x;
    int y;
    int step;
}num;
queue<node>q;
int dp[410][410];//起码要是300以上,第一次交的时候开了302,结果MLE了
int yy[]={0,0,1,-1,0};
int xx[]={1,-1,0,0,0};
bool v[400][400];
int sum=0;
int MAX=0x3f3f3f3f;
int bfs()
{
    node next;
    while(q.size())
    {
        num=q.front();
        q.pop();
        if(dp[num.x][num.y]==-1)
            return num.step;
        for(int i=0;i<4;i++)
        {
            next.x=num.x+xx[i];
            next.y=num.y+yy[i];
            next.step=num.step+1;
            if(next.x>=0&&next.y>=0&&(next.step<dp[next.x][next.y]||dp[next.x][next.y]==-1)&&!v[next.x][next.y])//这里一开始写了是<=300结果WA
            {
                v[next.x][next.y]=1;
                
                q.push(next);
            }
        }
    }
    return -1;
}
int main()
{
    int n;
    cin>>n;
    memset(dp,-1,sizeof(dp));
    for(int i=0;i<n;i++)
        {
            int x,y,t;//要用需要加上ios::sync_with_stdio(0);cin.tie(0);就可以,具体为什么   BaiDu
            cin>>x>>y>>t;//这里如果是scanf的话用g++交就可以了,若用cin就用c++交,否则会TLE
            for(int j=0;j<5;j++)
            {
                if((x+xx[j])>=0&&(y+yy[j])>=0){
                        if(dp[xx[j]+x][y+yy[j]]==-1)
                        dp[xx[j]+x][y+yy[j]]=t;
                      else
                    dp[xx[j]+x][y+yy[j]]=min(dp[xx[j]+x][y+yy[j]],t);
                }
            }
        }
        num.x=0;
        num.y=0;
        num.step=0;
        v[num.x][num.y]=1;
        q.push(num);
        cout<<bfs()<<endl;
//        for(int i=0;i<=10;i++)
//        {
//            for(int j=0;j<=10;j++)
//            cout<<dp[i][j]<<"     ";
//            cout<<endl;
//        }
    return 0;
}

#include<iostream>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
struct node
{
    int x;
    int y;
    int step;
}num;
queue<node>q;
int dp[410][410];
int yy[]={0,0,1,-1,0};
int xx[]={1,-1,0,0,0};
bool v[400][400];
int sum=0;
int MAX=0x3f3f3f3f;
int bfs()
{
    node next;
    while(q.size())
    {
        num=q.front();
        q.pop();
        if(dp[num.x][num.y]==MAX)
            return num.step;
        for(int i=0;i<4;i++)
        {
            next.x=num.x+xx[i];
            next.y=num.y+yy[i];
            if(next.x>=0&&next.y>=0&&(num.step+1)<dp[next.x][next.y]&&v[next.x][next.y]==0)
            {
                v[next.x][next.y]=1;
                next.step=num.step+1;
                q.push(next);
            }
        }
    }
    return -1;
}
int main()
{
    int n;
    cin>>n;
    memset(dp,MAX,sizeof(dp));
    for(int i=0;i<n;i++)//10^4就要用scanf了
        {
            int x,y,t;
            scanf("%d%d%d",&x,&y,&t);//scanf比cin快很多

                dp[x][y]=min(dp[x][y],t);
            dp[x+1][y]=min(dp[x+1][y],t);
            dp[x][y+1]=min(dp[x][y+1],t);
            if(x-1>=0)
                dp[x-1][y]=min(dp[x-1][y],t);
            if(y-1>=0)
                dp[x][y-1]=min(dp[x][y-1],t);
        }
        num.x=0;
        num.y=0;
        num.step=0;
        v[num.x][num.y]=1;
        q.push(num);
        cout<<bfs()<<endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/c___c18/article/details/80954653