BFS· POJ - 3669 · Meteor Shower

题目大意:

给定一些位置,告知你t时间会有炸弹落在这个位置上,并且会波及到上下左右,问你从(0 , 0) 开始能否找到一个绝对安全的位置,如果可以输出最小步数;

解题思路:

求解最小步数,基本想到BFS,我们可以先自己建立一张图,我们不能走的只有,超出边界(有下限,无上限)或者到达这一位置的时间大于炸弹落下的位置。

AC代码:

#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <bitset>
#include <string>
#include <vector>
#include <cstdio>
#include <cctype>
#include <fstream>
#include <cstdlib>
#include <sstream>
#include <cstring>
#include <iostream>
#include <algorithm>
#pragma comment(linker, "/STACK:1024000000,1024000000")

using namespace std;
#define   maxn          1010
#define   lson          l,m,rt<<1
#define   rson          m+1,r,rt<<1|1
#define   ms(x,y)      memset(x,y,sizeof(x))
#define   rep(i,n)      for(int i=0;i<(n);i++)
#define   repf(i,a,b)   for(int i=(a);i<=(b);i++)
#define   pii           pair<int,int>
//#define   mp            make_pair
#define   FI            first
#define   SE            second
#define   IT            iterator
#define   PB            push_back
#define   Times         10
typedef   long long     ll;
typedef   unsigned long long ull;
typedef   long double   ld;
typedef   pair<int ,int > P;

const double eps = 1e-10;
const double  pi = acos(-1.0);
const  ll    mod = 1e9+7;
const  int   inf = 0x3f3f3f3f;
const  ll    INF = (ll)1e18+300;
const int   maxd = 50000 + 5;

struct node{
    int x, y;
    int t;
};
int m;
node ac[maxd];
int mapp[505][404];
int dir[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
bool cmp(node a, node b) {
    return a.t < b.t;
}

void init() {
    ms(mapp, -1);
    for (int i = 0; i < m; i++) {
        int a, b, c;
        cin >> a >> b >> c;
        if(mapp[a][b] != -1) {
            mapp[a][b] = min(mapp[a][b], c);
        }
        else {
            mapp[a][b] = c;
        }
        if(mapp[a][b + 1] != -1) {
            mapp[a][b + 1] = min(mapp[a][b + 1], c);
        }
        else {
            mapp[a][b + 1] = c;
        }
        if(mapp[a + 1][b] != -1) {
            mapp[a+1][b] = min(mapp[a + 1][b], c);
        }
        else {
            mapp[a + 1][b] = c;
        }
        if(a > 0) {
            if(mapp[a - 1][b] != -1) {
                mapp[a - 1][b] = min(mapp[a - 1][b], c);
            }
            else {
                mapp[a - 1][b] = c;
            }
        }
        if(b > 0) {
            if(mapp[a][b - 1] != -1) {
                mapp[a][b - 1] = min(mapp[a][b - 1], c);
            }
            else {
                mapp[a][b - 1] = c;
            }
        }
    }
}

bool judge(int x, int y) {
    if(x >= 0 && y >= 0 ) {
        return true;
    }
    else return false;
}

int BFS() {
    queue<node> que;
    node ini;
    ini.x = 0;
    ini.y = 0;
    ini.t = 0;
    que.push(ini);
    int vis[505][505];
    ms(vis, 0);
    vis[0][0] = 1;
    while(!que.empty()) {
        node res = que.front();
        que.pop();
        if(mapp[res.x][res.y] == -1) {
            return res.t;
        }
        for (int i = 0; i < 4; i++) {
            int dx = res.x + dir[i][0];
            int dy = res.y + dir[i][1];
            if(judge(dx, dy) && (res.t +1 < mapp[dx][dy] || mapp[dx][dy] == -1) && !vis[dx][dy] ) {
                vis[dx][dy] = 1;
                node cnt;
                cnt.x = dx;
                cnt.y = dy;
                cnt.t = res.t + 1;
                que.push(cnt);
            }
        }
    }
    return -1;
}

int main(){
    while(cin >> m) {
        init();
        cout << BFS() << endl;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值