dfs28分求助,悬赏一个关注qaq

P2895 [USACO08FEB] Meteor Shower S

n_bluetea @ 2023-10-04 02:09:23

#include<bits/stdc++.h>
#define maxn 0x3f3f3f3f
using namespace std;

int b[500][500];
int c[500][500];
int dir[10][10] = { {0,0},{0,1},{1,0},{-1,0},{0,-1} };

struct node
{
    int x;
    int y;
    int t;
};

int main()
{
    freopen("ans.txt", "r", stdin);

    int m;
    cin >> m;

    for (int i = 0; i <= 500; i++)
    {
        for (int j = 0; j <= 500; j++)
        {
            b[i][j] = maxn;
        }
    }//预处理时间数组

    while (m--)
    {
        int x, y, t;
        cin >> x >> y >> t;

        for (int i = 0; i <= 4; i++)
        {
            int tx = x + dir[i][0];
            int ty = y + dir[i][1];

            if (tx < 0 || ty < 0 || b[tx][ty] != maxn)
            {
                continue;
            }

            b[tx][ty] = t;
        }
    }//预处理陨石坑

    queue<node>s;
    s.push({ 0,0,0 });
    while (!s.empty())
    {
        node first = s.front();
        s.pop();

        for (int i = 1; i <= 4; i++)
        {
            int tt = first.t + 1;
            int tx = first.x + dir[i][0];
            int ty = first.y + dir[i][1];

            if (tx < 0 || ty < 0 || tt>=b[tx][ty]||c[tx][ty]==1)
            {
                continue;
            }

            if (b[tx][ty] == maxn)
            {
                cout << tt;
                return 0;
            }
            c[tx][ty] = 1;
            s.push({ tx,ty,tt });
        }
    }
    cout << "-1";
    return 0;
}

by ge_yiyang_001_DT @ 2023-10-12 22:04:36

也许你应该换个方法做


by ge_yiyang_001_DT @ 2023-10-12 22:05:04

bfs而不是dfs


by Cgetier01 @ 2023-10-21 19:17:07

@n_blutea 帮你改了一下,流星砸向的格子你没有判断时间,枚举焦土的时候判断也错了,还有预处理时间数组下标越界了

#include<iostream>
using namespace std;
#include<algorithm>
#include<vector>
#include<queue>
#include<deque>
#include<climits>
#include<cstring>
#include<string>
#include<cmath>
#include<stdlib.h>
#include<map>
#include<unordered_map>
#include<set>
#define inf INT_MAX
#define maxn 0x3f3f3f3f
using namespace std;
int b[500][500];
int c[500][500];
int dir[10][10] = { {0,0},{0,1},{1,0},{-1,0},{0,-1} };

struct node
{
    int x;
    int y;
    int t;
};

int main()
{
    //freopen("ans.txt", "r", stdin);

    int m;
    cin >> m;

    for (int i = 0; i < 500; i++)
    {
        for (int j = 0; j < 500; j++)
        {
            b[i][j] = maxn;
        }
    }//预处理时间数组

    while (m--)
    {
        int x, y, t;
        cin >> x >> y >> t;
        if (t < b[x][y])
        {
            b[x][y] = t;
        }
        for (int i = 0; i <= 4; i++)
        {
            int tx = x + dir[i][0];
            int ty = y + dir[i][1];

            if (tx < 0 || ty < 0 || t>b[tx][ty])
            {
                continue;
            }

            b[tx][ty] = t;
        }
    }//预处理陨石坑

    queue<node>s;
    s.push({ 0,0,0 });
    c[0][0] = 1;
    while (!s.empty())
    {
        node first = s.front();
        s.pop();

        for (int i = 1; i <= 4; i++)
        {
            int tt = first.t + 1;
            int tx = first.x + dir[i][0];
            int ty = first.y + dir[i][1];

            if (tx < 0 || ty < 0 || tt >= b[tx][ty] || c[tx][ty] == 1)
            {
                continue;
            }

            if (b[tx][ty] == maxn)
            {
                cout << tt;
                return 0;
            }
            c[tx][ty] = 1;
            s.push({ tx,ty,tt });
        }
    }
    cout << "-1";
    return 0;
}

by Cgetier01 @ 2023-10-21 19:18:34

@Cgetier01 还有(0,0)点在广搜前记为已搜过


|