搜索练习1

Posted 橘生淮南终洛枳

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了搜索练习1相关的知识,希望对你有一定的参考价值。

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
solution:
  预处理所有位置被砸毁的时间(最早砸毁时间)
  bfs直接搜索到达一个不会被砸毁的位置的最短时间(最小步数就是最短时间,因为每秒钟走1步)
  需要注意如果你到达某个位置的时间是晚于it被砸毁的时间的,那么这个位置是无效的
Code:
技术分享图片
#include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;

const int M = 50010;
const int N = 506;
int m,map[400][400],vis[400][400];
struct Node {
    int x,y,t;
} star[M];
queue<Node>que;
int dx[4]= {0,0,-1,1},
           dy[4]={-1,1,0,0};

int bfs() {
    if(map[0][0]==-1) return 0; //初始位置没被砸毁 
    if(map[0][0]==0) return -1; //初始位置在最开始就被砸毁 
    Node s;
    s.x=0,s.y=0,s.t=0;
    que.push(s);
    vis[0][0]=1;
    while(!que.empty()) {
        Node tmp=que.front();
        que.pop();
        for(int i=0; i<4; i++) {
            Node tmp2;
            tmp2.x=tmp.x+dx[i];
            tmp2.y=tmp.y+dy[i];
            tmp2.t=tmp.t+1;
            if(tmp2.x<0 || tmp2.x>=N || tmp2.y<0 || tmp2.y>=N) continue;//越界 
            if(map[tmp2.x][tmp2.y]==-1) return tmp2.t; //如果搜索点没被砸毁,那么直接返回这个时间,就是最短步数 
            if(tmp2.t>=map[tmp2.x][tmp2.y]) continue; //如果搜索点的时间比该位置被砸毁的时间大,这个点无贡献 
            if(tmp2.x>=0 && tmp2.x<N && tmp2.y>=0 &&tmp2.y<N) {
                if(!vis[tmp2.x][tmp2.y]) { //没搜过继续搜 
                    que.push(tmp2);
                    vis[tmp2.x][tmp2.y]=1;
                } else continue;
            }
        }
    }
    return -1;
}

int main() {
    memset(map,-1,sizeof(map));
    memset(vis,0,sizeof(vis));
    scanf("%d",&m);
    for(int i=1; i<=m; i++) { //处理每个位置被砸毁的最小时间 
        int x,y,t;
        scanf("%d%d%d",&x,&y,&t);
        if(map[x][y]==-1) map[x][y]=t;
        else map[x][y]=min(map[x][y],t);
        for(int j=0; j<4; j++) {
            int xx=x+dx[j],yy=y+dy[j];
            if(xx<0 || yy<0 || xx>=N || yy>=N) continue;
            if(map[xx][yy]==-1) map[xx][yy]=t;
            else map[xx][yy]=min(map[xx][yy],t);
        }
    }
    cout<<bfs();
    return 0;
}
AC

 

自己选的路,跪着也要走完!!!

以上是关于搜索练习1的主要内容,如果未能解决你的问题,请参考以下文章

Python练习册 第 0013 题: 用 Python 写一个爬图片的程序,爬 这个链接里的日本妹子图片 :-),(http://tieba.baidu.com/p/2166231880)(代码片段

csharp Epicor标准练习片段

golang 去练习片段

如何在 BottomNavigationView 的片段上打开搜索界面?

26个jQuery代码片段使用技巧

《安富莱嵌入式周报》第279期:强劲的代码片段搜索工具,卡内基梅隆大学安全可靠C编码标准,Nordic发布双频WiFi6 nRF7002芯片