35分求调

P2895 [USACO08FEB] Meteor Shower S

GreenMelon @ 2024-11-17 17:09:50

#include<bits/stdc++.h>
#define N 1005
using namespace std;
int dx[4]={1, -1, 0, 0};
int dy[4]={0, 0, 1, -1};
struct node{
    int t;
    bool is;
    int x, y;
} a[305][305];
queue<node> q;
bool b[305][305];
int bfs(){
    while(q.size()){
        node Q=q.front();
        q.pop();
        if(!a[Q.x][Q.y].is) return Q.t;
        for(int i=0;i<4;i++){
            int xx=Q.x+dx[i];
            int yy=Q.y+dy[i];
            int now=Q.t+1;
            if(!b[xx][yy] && xx>=0 && yy>=0){
                if(a[xx][yy].is){
                    if(a[xx][yy].t>now){
                        q.push({now, 0, xx, yy});
                        b[xx][yy]=1;
                    }
                }
                else{
                    q.push({now, 0, xx, yy});
                    b[xx][yy]=1;
                }
            }
        }
    }
    return -1;
}
int main(){
    int n;
    cin>>n;
    while(n--){
        int x, y, t;
        cin>>x>>y>>t;
        a[x][y].is=1;
        a[x][y].t=t;
        for(int i=0;i<4;i++){
            int xx=x+dx[i];
            int yy=y+dy[i];
            if(xx<0 || yy<0) continue;
            a[xx][yy].is=1;
            a[xx][yy].t=t;
        }
    }
    q.push({0, 0, 0, 0});
    cout<<bfs();
    return 0;
} 

|