Meteor Shower 广搜

#include <cstdio>
#include <queue>
#include <cstring>
using namespace std;

#define MAX 310
#define Inf 0x7fffffff

typedef struct pot
{
    int x, y;
    int t;
}pot;
int dre[5][2]={ {0,0},{0,1},{0,-1},{1,0},{-1,0} };
int time[MAX][MAX];
bool pos[MAX][MAX];

int Min(int a, int b)
{
    if(a<b) return a;
    return b;
}
bool judge_it(int x, int y)
{
    if(x<0 || y<0) return false;
    return true;
}
bool judge(pot next)
{
    if(next.x<0 || next.y<0 || next.t>=time[next.x][next.y] || pos[next.x][next.y] ) return false;
    return true;
}

int bfs()
{
    queue<pot>Q;
    pot now, next;

    memset(pos, false, sizeof(pos));
    now.x=0; now.y=0; now.t=0;
    pos[now.x][now.y]=true;
    Q.push(now);
    while( !Q.empty() ){
        now=Q.front(); Q.pop();
        if(time[now.x][now.y]==Inf) return now.t;

        for(int i=1; i<5; i++){
            next.x=now.x+dre[i][0]; next.y=now.y+dre[i][1]; next.t=now.t+1;
            if( judge(next) ) {
                Q.push( next );
                pos[next.x][next.y]=true;
            }
        }
    }
    return -1;
}
int main()
{
    int n;
    while( scanf("%d", &n)!=-1 ) {
        for(int i=0; i<MAX; i++){
            for(int j=0; j<MAX; j++)
                time[i][j]=Inf;
        }
        int x, y, t;
        for(int i=0; i<n; i++){
            scanf("%d%d%d", &x, &y, &t);
            for(int j=0; j<5; j++){
                if( judge_it(x+dre[j][0], y+dre[j][1]) ){
                    time[x+dre[j][0]][y+dre[j][1]]=Min(time[x+dre[j][0]][y+dre[j][1]], t);
                }
            }
        }
       int ans;
       ans = bfs();
       printf("%d\n", ans);
    }

    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值