第二个样例木有过,感觉问题有点特殊,木有调试出来,求个样例或大佬指点

P2895 [USACO08FEB] Meteor Shower S

aojiaoluolisaiban @ 2024-02-04 13:32:21

# 用两个二维数组分别记录掉落与到达时间

#include <bits/stdc++.h>
#include <stdlib.h>
using namespace std;
int main()
{
    int M;
    cin>>M;
    int bomb[303][303];
    int time[303][303];
    int dx[5] = {0,0,1,-1,0};
    int dy[5] = {1,-1,0,0,0};
    for(int i=0;i<303;i++)
    {
        for(int j=0;j<303;j++)
        {
            bomb[i][j] = 50001;
        }
    }
    memset(time,-1,sizeof(time));
    int bomb_x,bomb_y,bomb_t;
    for(int i=1;i<=M;i++)
    {
        cin >> bomb_x >> bomb_y >> bomb_t;
        for(int j=0;j<5;j++)
        {
            int a = bomb_x + dx[j];
            int b = bomb_y + dy[j];
            if(a>=0&&b>=0&&bomb[a][b]>bomb_t)
            bomb[a][b] = bomb_t;
        }
    }
    queue<int> q_x,q_y;
    q_x.push(0);
    q_y.push(0);
    time[0][0] = 0;
    while(!q_x.empty())
    {
        int x = q_x.front();
        int y = q_y.front();
        q_x.pop();
        q_y.pop();
        for(int i=0;i<5;i++)
        {
            int a = x + dx[i];
            int b = y + dy[i];
            if(a>=0 && b>=0 && bomb[a][b]>time[x][y]+1 &&time[a][b] == -1)
            {
                time[a][b] = time[x][y]+1;
                q_x.push(a);
                q_y.push(b);
            }
        }
        if((bomb[q_x.front()][q_y.front()] == 50001) || q_x.front() > 300 || q_y.front() > 300)
        break;
    }
    cout<<time[q_x.front()][q_y.front()];
    return 0;
}

by sybnb @ 2024-02-07 12:19:37

输入:

2
0 1 2
1 0 2

输出:

-1

你的输出:

0

@aojiaoluolisaiban


by sybnb @ 2024-02-07 12:20:10

#include <bits/stdc++.h>
#include <stdlib.h>
using namespace std;
int main()
{
    int M;
    cin>>M;
    //if(M==2){
    //    cout<<-1;
    //}
    int bomb[303][303];
    int time[303][303];
    int dx[5] = {0,0,1,-1,0};
    int dy[5] = {1,-1,0,0,0};
    for(int i=0;i<303;i++)
    {
        for(int j=0;j<303;j++)
        {
            bomb[i][j] = 50001;
        }
    }
    memset(time,-1,sizeof(time));
    int bomb_x,bomb_y,bomb_t;
    for(int i=1;i<=M;i++){
        cin >> bomb_x >> bomb_y >> bomb_t;
        for(int j=0;j<5;j++){
            int a = bomb_x + dx[j];
            int b = bomb_y + dy[j];
            if(a>=0&&b>=0&&bomb[a][b]>bomb_t)
            bomb[a][b] = bomb_t;
        }
    }
    queue<int> q_x,q_y;
    q_x.push(0);
    q_y.push(0);
    time[0][0] = 0;
    while(!q_x.empty()){
        int x = q_x.front();
        int y = q_y.front();
        q_x.pop();
        q_y.pop();
        for(int i=0;i<5;i++){
            int a = x + dx[i];
            int b = y + dy[i];
            if(a>=0 && b>=0 && bomb[a][b]>time[x][y]+1 &&time[a][b] == -1){
                time[a][b] = time[x][y]+1;
                q_x.push(a);
                q_y.push(b);
            }
        }
        if((bomb[q_x.front()][q_y.front()] == 50001) || q_x.front() > 300 || q_y.front() > 300)
        break;
    }
    if(time[q_x.front()][q_y.front()]==0){//改了这里
        cout<<-1;
    }
    else cout<<time[q_x.front()][q_y.front()];
    return 0;
}

by aojiaoluolisaiban @ 2024-02-11 10:34:50

@sybnb 蟹蟹嗷


|