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

题目链接

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;
}

  • 14
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当然可以!下面是一个基本的BFS(广度优先搜索)代码模板的C++实现: ```cpp #include <iostream> #include <queue> #include <vector> using namespace std; void bfs(vector<vector<int>>& graph, int start) { int n = graph.size(); vector<bool> visited(n, false); // 记录节点是否被访问过 queue<int> q; // 用于存储待访问的节点 visited[start] = true; q.push(start); while (!q.empty()) { int node = q.front(); q.pop(); // 处理当前节点 cout << node << " "; // 遍历当前节点的邻居节点 for (int neighbor : graph[node]) { if (!visited[neighbor]) { visited[neighbor] = true; q.push(neighbor); } } } } int main() { // 构建图的邻接表表示 vector<vector<int>> graph = { {1, 2}, // 节点0的邻居节点为1和2 {0, 3, 4}, // 节点1的邻居节点为0、3和4 {0, 5}, // 节点2的邻居节点为0和5 {1}, // 节点3的邻居节点为1 {1}, // 节点4的邻居节点为1 {2} // 节点5的邻居节点为2 }; int startNode = 0; // 从节点0开始进行BFS bfs(graph, startNode); return 0; } ``` 这个代码模板使用了邻接表来表示图,通过队列来实现BFS。首先,我们创建一个`visited`数组来记录节点是否被访问过,初始时所有节点都未被访问。然后,我们创建一个队列`q`,将起始节点加入队列,并将其标记为已访问。接下来,我们进入循环,直到队列为空。在每次循环中,我们取出队列的头部节点,并处理该节点。然后,我们遍历该节点的邻居节点,如果邻居节点未被访问过,则将其加入队列并标记为已访问。最终,当队列为空时,BFS结束。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值