zoukankan      html  css  js  c++  java
  • POJ 3369 Meteor Shower (BFS,水题)

    题意:给定 n 个炸弹的坐标和爆炸时间,问你能不能逃出去。如果能输出最短时间。

    析:其实这个题并不难,只是当时没读懂,后来读懂后,很容易就AC了。

    主要思路是这样的,先标记所有的炸弹的位置,和时间,在数组中标记就好,只要赋值给它的爆炸时间就好,注意如果有多个,要赋值最小的那个,

    然后用BFS走就行了。

    代码如下:

    #include <cstdio>
    #include <string>
    #include <cstdlib>
    #include <cmath>
    #include <iostream>
    #include <cstring>
    #include <set>
    #include <queue>
    #include <algorithm>
    #include <vector>
    #include <map>
    using namespace std ;
    
    typedef long long LL;
    typedef pair<int, int> P;
    const int INF = 0x3f3f3f3f;
    const double inf = 0x3f3f3f3f3f3f3f;
    const double eps = 1e-8;
    const int maxn = 300 + 15;
    const int dr[] = {0, 0, -1, 1};
    const int dc[] = {-1, 1, 0, 0};
    int m, n;
    inline bool is_in(int r, int c){
        return r >= 0 && r < n && c >= 0 && c < m;
    }
    int s[maxn][maxn];
    int vis[maxn][maxn];
    struct node{
        int x, y, d;
        node(int xx, int yy, int dd) : x(xx), y(yy), d(dd) { }
    };
    
    int bfs(){
        queue<node> q;
        q.push(node(0, 0, 0));
        vis[0][0] = 1;
        if(!s[0][0])  return -1;
        while(!q.empty()){
            node u = q.front();  q.pop();
            for(int i = 0; i < 4; ++i){
                int x = u.x + dr[i];
                int y = u.y + dc[i];
                if(x < 0 || y < 0 || x > 305 || y > 305 || vis[x][y] || s[x][y] <= u.d+1)  continue;
                vis[x][y] = 1;
                if(s[x][y] == INF)  return u.d+1;
                q.push(node(x, y, u.d+1));
            }
        }
        return -1;
    }
    
    int main(){
        while(scanf("%d", &n) == 1){
            int x, y, t;
            for(int i = 0; i < 305; ++i)
                for(int j = 0; j < 305; ++j)
                    s[i][j] = INF;
            for(int i = 0; i < n; ++i){
                scanf("%d %d %d", &x, &y, &t);
                if(x > 0)  s[x-1][y] = min(t, s[x-1][y]);
                if(y > 0)  s[x][y-1] = min(t, s[x][y-1]);
                s[x+1][y] = min(t, s[x+1][y]);
                s[x][y+1] = min(t, s[x][y+1]);
                s[x][y] = min(t, s[x][y]);
            }
            memset(vis, 0, sizeof(vis));
            printf("%d
    ", bfs());
        }
        return 0;
    }
    
  • 相关阅读:
    contextMenu,右键菜单
    hashchange
    web攻击日志分析之新手指南
    工匠人生
    数学有卵用之通信篇
    精英主义(一)
    flaskbb部署笔记
    深入分析一波,你们说的云安全到底是什么鬼?
    Gh0st与云安全
    困境与突破
  • 原文地址:https://www.cnblogs.com/dwtfukgv/p/5754230.html
Copyright © 2011-2022 走看看