首页 > 编程语言 >c++求助bfs流星雨题目为什么代码编不过

c++求助bfs流星雨题目为什么代码编不过

时间:2024-09-11 23:49:39浏览次数:3  
标签:Xi int c++ bfs define && table include 流星雨

题目链接

3669 -- Meteor Shower (poj.org)

英文题目

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 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. Input * Line 1: A single integer: M * Lines 2..M+1: Line i+1 contains three space-separated integers: Xi, Yi, and Ti Output * Line 1: The minimum time it takes Bessie to get to a safe place or -1 if it is impossible.

中文题目

描述
贝茜听说一场非凡的流星雨即将来临;报道称这些流星将会撞击地球并摧毁它们所击中的一切。由于担心自己的安全,她发誓要找到一个安全的地方(一个永远不会被流星摧毁的地方)。她目前在坐标平面的原点上放牧,想要移动到一个新的,更安全的位置,同时避免被沿途的流星摧毁。

报告说,将有M颗流星(1≤M≤50000)撞击地球,其中流星i将撞击点(Xi, Yi)(0≤Xi≤300;(0≤Ti≤1000)时,0≤Yi≤300。每颗流星都会破坏它所击中的点以及四个直线相邻的点阵点。

贝西在时间0离开原点,可以在第一象限平行于轴线,以每秒一个距离单位的速度移动到任何(通常是4)相邻的直线点,这些点还没有被流星摧毁。她不能在任何时间被定位在一个点大于或等于它被摧毁的时间)。

确定贝西到达安全地点所需的最短时间。

输入
*第一行:单个整数:M

*第2行…M+1:第i+1行包含三个空格分隔的整数:Xi、Yi和Ti

输出
*第一行:贝茜到达安全地点所需的最短时间,如果不可能,则为-1。

思路

bfs找到符合题目要求的位置返回最短时间

具体思路体现代码中的注释

代码

#include<iostream>
#include<string>
#include<vector>
#include<queue>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long ll;
#define N 1001
#define U 0x3f3f3f3f
#define pii pair<int,int>
#define mp make_pair
#define f first
#define s second

int table[301][301];
bool check[301][301];
int dx[5] = { 1,-1,0,0,0 };
int dy[5] = { 0,0,1,-1,0 };

int bfs()


{
    int t = 0;//当前时间
    queue<pii> q;//放置暂时安全的位置
    if (table[0][0] == U)//原点安全返回0
    {
        return t;
    }
    else if (table[0][0] > t)//原点不安全入队列找安全地方
    {
        q.push(mp(0, 0));
        check[0][0] = true;
    }

    //原点为0被砸死while循环不进入返回-1

    while (q.size())
    {
        int sz = q.size();
        for (int j = 0; j < sz; ++j)//找第t秒下1秒暂时安全的左右位置
        {
            pii front = q.front();
            int x = front.f;
            int y = front.s;
            for (int i = 0; i < 4; ++i)//四个方向
            {
                x += dx[i];
                y += dy[i];
                if (x >= 0 && x <= 300 && y >= 0 && y <= 300
                    && check[x][y] == false && t + 1 < table[x][y])//坐标合法且为访问且下一秒位置不会被砸死
                {
                    if (table[x][y] == U)//下一秒永远安全则返回下一秒
                    {
                        return t + 1;
                    }
                        
                    check[x][y] = true;
                    q.push(mp(x, y));
                }
                x -= dx[i];
                y -= dy[i];
            }
            q.pop();
        }
        ++t;
    }
    return -1;//不存在永远安全位置
}

int main()


{
    int n, x, y, t;
    cin >> n;

    memset(table, U, sizeof(table));//每个位置流星掉落最短时间,初始为最大值
    memset(check, false, sizeof(check));//判断是否访问每个位置,false为未访问

    for (int i = 0; i < n; ++i)//读入每个流星雨坐标和时间
    {
        cin >> x >> y >> t;
        for (int j = 0; j < 5; ++j)//流星雨及其相邻坐标
        {
            x += dx[j];
            y += dy[j];
            if (x >= 0 && x <= 300 && y >= 0 && y <= 300 && t < table[x][y])//坐标合法且求最短时间
            {
                table[x][y] = t;
            }
            x -= dx[j];
            y -= dy[j];
        }
    }
    cout << bfs() << endl;
}

标签:Xi,int,c++,bfs,define,&&,table,include,流星雨
From: https://blog.csdn.net/2301_80281361/article/details/142153331

相关文章

  • c++primer第四章复合类型学习笔记
    数组数组创建声明:存储元素类型数组名数组的元素个数#include<iostream>usingnamespacestd;intmain(){intyams[3];yams[0]=7;yams[1]=8;yams[2]=6;intyamcosts[3]={20,30,5};cout<<"Totalyams=";cout<<......
  • c++primer第五章循环和关系表达式学习笔记
    for循环简单for循环#include<iostream>usingnamespacestd;intmain(){//5.1inti;for(i=0;i<5;i++)cout<<"C++knowsloops.\n";cout<<"C++knowswhentostop.\n";return0;}for循环组成部分#......
  • C++primer 第十章对象和类学习笔记
    OPP特性:抽象封装和数据隐藏多态继承代码的可重用性过程性和面向对象的编程从用户的角度考虑对象抽象和类基本类型完成的工作:决定数据对象需要的内存单元决定如何解释内存中的位决定可使用数据对象执行的操作或方法 c++中的类类规范类声明:以数据成员的方式描述数......
  • 【C++】new的底层实现原理
    文章目录理解C++`new`的原理1.`new`的基本工作流程2.`new`和`malloc`的区别3.`new`的底层实现4.`new[]`与`delete`的配对使用5.自定义`new`和`delete`6.定位new7.内存泄漏与异常安全理解C++new的原理在C++中,new操作符用于在堆上动态分......
  • 【C++】C++ 多态的底层实现原理
    文章目录1.多态的定义与作用2.虚函数与虚函数表(vtable)3.虚函数表(vtable)4.虚函数调用的底层过程5.内存布局中的虚函数指针6.多重继承中的虚函数表7.RTTI与动态类型识别1.多态的定义与作用多态指的是同一操作在不同对象上具有不同的表现。C++中多态分为两......
  • 递归(c++)
    递归1、斐波那契额数列基础代码#include<iostream>usingnamespacestd;intf(intn){ if(n==1)return1; if(n==2)return2; returnf(n-1)+f(n-2);}intmain(){ intn; cin>>n; cout<<f(n)<<endl; return0;}递归实现指数......
  • c++ 数字转化成 string
       ULONG转换成string方法1:使用std::to_string(C++11及更高版本)std::to_string是将数字转换为字符串的简单方式,适用于C++11及更高版本。#include<iostream>#include<string>intmain(){ULONGvalue=1234567890UL;//定义一个ULONG类型的值/......
  • c++ string 转换成 guid
      在C++中,将一个字符串转换为GUID(GloballyUniqueIdentifier)可以通过以下方法实现。GUID通常是128位(16字节)的标识符,以标准格式表示,例如:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx。在C++中,常用的库之一是WindowsAPI,它提供了处理GUID的相关功能。这里是一个示例代码,将字符串转换......
  • 【自用22.】C++类的静态数据成员以及静态成员函数
    需要获取总的人数,如何实现?方案一:使用全局变量,在构造函数中对这个全局变量进行修改具体代码如下:在Human.cpp中定义全局变量,并在构造函数中对人数进行增加操作#include"Human.h"#include<iostream>usingnamespacestd;intHumanCount=0;Human::Human(){ name......
  • C++复习day10
    智能指针为什么需要智能指针?#include<iostream>usingnamespacestd;intdiv(){ inta,b; cin>>a>>b; if(b==0) throwinvalid_argument("除0错误"); returna/b;}voidFunc(){ //1、如果p1这里new抛异常会如何? //2、如果p2这里new抛异常会......