洛谷 P2895 [USACO08FEB]流星雨Meteor Shower

P2895 [USACO08FEB]流星雨Meteor Shower

题目描述

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 (Xi, Yi) (0 ≤ Xi ≤ 300; 0 ≤ Yi ≤ 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.

牛去看流星雨,不料流星掉下来会砸毁上下左右中五个点。每个流星掉下的位置和时间都不同,求牛能否活命,如果能活命,最短的逃跑时间是多少?

输入输出格式

输入格式:

  • Line 1: A single integer: M
  • Lines 2..M+1: Line i+1 contains three space-separated integers: Xi, Yi, and Ti

输出格式:

  • Line 1: The minimum time it takes Bessie to get to a safe place or -1 if it is impossible.

输入输出样例

输入样例#1:

4
0 0 2
2 1 2
1 1 2
0 3 5

输出样例#1:

5思路:爆搜。错因:本题应该用宽搜,用了深搜。吐槽:大佬竟然是这样判断宽搜深搜的:深搜TLE就是宽搜,宽搜MLE/RE就是深搜╮(╯▽╰)╭。63分的深搜:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int dx[4]={0,0,-1,1};
int dy[4]={-1,1,0,0};
int ans=0x7f7f7f7f,m,t,map[310][310],mm[310][310];
void dfs(int x,int y,int step){
    if(map[x][y]>2000000000){
        ans=min(ans,step);
        return ;
    }
    for(int i=0;i<4;i++){
        int cx=x+dx[i];
        int cy=y+dy[i];
        if(map[cx][cy]>step&&cx>=0&&cy>=0){
            map[cx][cy]-=1;
            dfs(cx,cy,step+1);
            map[cx][cy]+=1;
        }
    }
}
int main(){
    scanf("%d",&m);
    memset(map,0x7f,sizeof(map));
    for(int i=1;i<=m;i++){
        int x,y,z;
        scanf("%d%d%d",&x,&y,&z);
        map[x][y]=min(map[x][y],z);
        map[x+1][y]=min(map[x+1][y],z);
        map[x][y+1]=min(map[x][y+1],z);
        if(x>0)    map[x-1][y]=min(map[x-1][y],z);
        if(y>0)    map[x][y-1]=min(map[x][y-1],z);
    }
    dfs(0,0,1);
    if(ans!=2139062143)    cout<<ans-1;
    else cout<<"-1";
}

AC的宽搜:

#include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define oo 1e9
using namespace std;
struct nond{
    int x,y,z;
};
queue<nond>que;
bool b[310][310];
int n,map[310][310];
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};
int main(){
    memset(map,0x7f,sizeof(map));
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        int x,y,z;
        scanf("%d%d%d",&x,&y,&z);
        map[x][y]=min(map[x][y],z);
        map[x+1][y]=min(map[x+1][y],z);
        map[x][y+1]=min(map[x][y+1],z);
        if(x>0)    map[x-1][y]=min(map[x-1][y],z);
        if(y>0)    map[x][y-1]=min(map[x][y-1],z);
    }
    nond now,pre;
    b[0][0]=1;
    now.x=now.y=now.z=0;
    que.push(now);
    while(!que.empty()){
        now=que.front();
        que.pop();
        if(map[now.x][now.y]==2139062143){
            printf("%d",now.z);
            return 0;
        }
        for(int i=0;i<4;i++){
            int cx=now.x+dx[i];
            int cy=now.y+dy[i];
            if(cx>=0&&cy>=0&&!b[cx][cy]&&map[cx][cy]>now.z+1){
                b[cx][cy]=1;
                pre.x=cx;pre.y=cy;pre.z=now.z+1;
                que.push(pre);
            }
        }
    }
    cout<<"-1";
    return 0;
}
				
时间: 2024-12-14 09:20:25

洛谷 P2895 [USACO08FEB]流星雨Meteor Shower的相关文章

bzoj1611 / P2895 [USACO08FEB]流星雨Meteor Shower

P2895 [USACO08FEB]流星雨Meteor Shower 给每个点标记一下能够走的最迟时间,蓝后bfs处理一下 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<cctype> 6 #include<queue> 7 #define re register 8 using namespac

P2895 [USACO08FEB]流星雨Meteor Shower

链接:Miku ---------------- 这题没边界,用bfs比较好 与处理完了就没什么好说的特别之处 ------------------ #include<iostream> #include<cstdio> #include<cstring> #include<queue> using namespace std; int safe[305][305]; int t[305][305]; int m; int mx[5]={1,-1,0,0};

【流星雨Meteor Shower】

题意翻译 贝茜听说了一个骇人听闻的消息:一场流星雨即将袭击整个农场,由于流星体积过大,它们无法在撞击到地面前燃烧殆尽,届时将会对它撞到的一切东西造成毁灭性的打击.很自然地,贝茜开始担心自己的安全问题.以FJ牧场中最聪明的奶牛的名誉起誓,她一定要在被流星砸到前,到达一个安全的地方(也就是说,一块不会被任何流星砸到的土地).如果将牧场放入一个直角坐标系中,贝茜现在的位置是原点,并且,贝茜不能踏上一块被流星砸过的土地. 根据预报,一共有M颗流星(1 <= M <= 50,000)会坠落在农场上,其中

洛谷P2896 [USACO08FEB]一起吃饭Eating Together

P2896 [USACO08FEB]一起吃饭Eating Together 题目描述 The cows are so very silly about their dinner partners. They have organized themselves into three groups (conveniently numbered 1, 2, and 3) that insist upon dining together. The trouble starts when they lin

洛谷 P2896 [USACO08FEB]一起吃饭Eating Together

P2896 [USACO08FEB]一起吃饭Eating Together 题目描述 The cows are so very silly about their dinner partners. They have organized themselves into three groups (conveniently numbered 1, 2, and 3) that insist upon dining together. The trouble starts when they lin

洛谷—— P2896 [USACO08FEB]一起吃饭Eating Together

https://www.luogu.org/problem/show?pid=2896 题目描述 The cows are so very silly about their dinner partners. They have organized themselves into three groups (conveniently numbered 1, 2, and 3) that insist upon dining together. The trouble starts when th

1611: [Usaco2008 Feb]Meteor Shower流星雨

1611: [Usaco2008 Feb]Meteor Shower流星雨 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1010  Solved: 446[Submit][Status][Discuss] Description 去年偶们湖南遭受N年不遇到冰冻灾害,现在芙蓉哥哥则听说另一个骇人听闻的消息: 一场流星雨即将袭击整个霸中,由于流星体积过大,它们无法在撞击到地面前燃烧殆尽, 届时将会对它撞到的一切东西造成毁灭性的打击.很自然地,芙蓉哥哥开

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

bzoj1611[Usaco2008 Feb]Meteor Shower流星雨*

bzoj1611[Usaco2008 Feb]Meteor Shower流星雨 题意: 给个网格,有m个流星,每个流星在ti时刻打在(xi,yi)的格子上,并把该格子和相邻的格子打烂.有个人从(0,0)出发,问最短逃离时间(格子被打烂之后就不能走). 题解: bfs一发,如果某格子被打烂的时间小于到达时间则不能到达,最后如果到达打烂时间为正无穷的格子即为成功逃出.注意网格的边界应该大于流星的边界,因为如果逃到那些地方也算安全地带. 代码: 1 #include <cstdio> 2 #incl