POJ - 3669 Meteor Shower(bfs)

题意:某人在时刻0从原点出发,在第一象限范围内移动。已知每个炸弹爆炸的地点和时刻,炸弹爆炸可毁坏该点和它上下左右的点。不能经过已毁坏的点,也不能在某点毁坏的时候位于该点。不会被炸弹毁坏的地方是安全之处,问某人到达安全之处的最短时间。

分析:bfs即可。注意:1、某点多次爆炸或受爆炸点影响而毁坏,取最早时间为该点的毁坏时间。2、bfs走过的点不能再走,注意标记。

#pragma comment(linker, "/STACK:102400000, 102400000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define Min(a, b) ((a < b) ? a : b)
#define Max(a, b) ((a < b) ? b : a)
const double eps = 1e-8;
inline int dcmp(double a, double b){
    if(fabs(a - b) < eps) return 0;
    return a > b ? 1 : -1;
}
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, -1, 0, 1, -1, -1, 1, 1};
const int dc[] = {-1, 0, 1, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const int MAXN = 300 + 10;
const int MAXT = 10000 + 10;
using namespace std;
int vis[MAXN][MAXN];
int a[MAXN][MAXN];
bool judge(int x, int y){
    return x >= 0 && y >= 0;
}
int bfs(int sx, int sy){
    queue<int> x, y, step;
    x.push(sx);
    y.push(sy);
    step.push(0);
    a[sx][sy] = 1;
    while(!x.empty()){
        int tmpx = x.front();
        x.pop();
        int tmpy = y.front();
        y.pop();
        int tmpstep = step.front();
        step.pop();
        if(vis[tmpx][tmpy] == INT_INF) return tmpstep;
        for(int i = 0; i < 4; ++i){
            int tx = tmpx + dr[i];
            int ty = tmpy + dc[i];
            if(judge(tx, ty)){
                if(vis[tx][ty] == INT_INF) return tmpstep + 1;
                if(a[tx][ty]) continue;
                if(vis[tx][ty] > tmpstep + 1){
                    x.push(tx);
                    y.push(ty);
                    step.push(tmpstep + 1);
                    a[tx][ty] = 1;
                }
            }
        }
    }
    return -1;
}
int main(){
    int M;
    scanf("%d", &M);
    memset(vis, INT_INF, sizeof vis);
    while(M--){
        int x, y, t;
        scanf("%d%d%d", &x, &y, &t);
        vis[x][y] = Min(vis[x][y], t);
        for(int i = 0; i < 4; ++i){
            int tmpx = x + dr[i];
            int tmpy = y + dc[i];
            if(judge(tmpx, tmpy)) vis[tmpx][tmpy] = Min(vis[tmpx][tmpy], t);
        }
    }
    int ans = bfs(0, 0);
    printf("%d\n", ans);
    return 0;
}

  

时间: 2024-12-24 10:04:50

POJ - 3669 Meteor Shower(bfs)的相关文章

POJ 3669 Meteor Shower(流星雨)

POJ 3669 Meteor Shower(流星雨) Time Limit: 1000MS    Memory Limit: 65536K 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 sa

POJ 3669 Meteor Shower【BFS】

POJ 3669 去看流星雨,不料流星掉下来会砸毁上下左右中五个点.每个流星掉下的位置和时间都不同,求能否活命,如果能活命,最短的逃跑时间是多少? 思路:对流星雨排序,然后将地图的每个点的值设为该点最早被炸毁的时间 #include <iostream> #include <algorithm> #include <queue> #include <cstring> using namespace std; #define INDEX_MAX 512 int

poj 3669 Meteor Shower(bfs)

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 destroy

POJ 3669 Meteor shower 简单BFS, 有坑点

Meteor Shower 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

POJ 3669 Meteor Shower (BFS + 预处理)

Meteor Shower Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9677   Accepted: 2718 Description Bessie hears that an extraordinary meteor shower is coming; reports say that these meteors will crash into earth and destroy anything they hi

题解报告:poj 3669 Meteor Shower(bfs)

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 destroy

poj 3669 Meteor Shower

Meteor Shower Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16339   Accepted: 4293 Description Bessie hears that an extraordinary meteor shower is coming; reports say that these meteors will crash into earth and destroy anything they h

【POJ 3669 Meteor Shower】简单BFS

流星雨撞击地球(平面直角坐标第一象限),问到达安全地带的最少时间. 对于每颗流星雨i,在ti时刻撞击(xi,yi)点,同时导致(xi,yi)和上下左右相邻的点在ti以后的时刻(包括t)不能再经过(被封锁).安全地带为永远不会被封锁的点. 简单bfs,开始WA在把平面空间上限当成300*300,但根据题目,这只是有流星雨撞击的范围.实际可走的空间理论上没上限,但分析可得,离原点最近的安全地带一定在(302,302)范围内,所以应可把数组至少开为303*303. 后来WA在把G[0][0]==1的情

[水+bfs] poj 3669 Meteor Shower

题意: 一个迷宫内有有M个炸弹,分别在(xi,yi) 处并在ti时刻爆炸,爆炸包括本身和四周一个单位. 也就是说爆炸完点就不能走了. 然后人在(0,0)点,为走多少步能到达一个永远安全的点,否则输出-1. 思路: 首先预处理所有炸弹,标记每个点在哪个时刻之前是可以通过的. 然后人走一遍bfs就ok了. 代码: #include"cstdlib" #include"cstdio" #include"cstring" #include"cm