求调玄关

P2895 [USACO08FEB] Meteor Shower S

xuhaicheng @ 2024-08-13 09:59:19

#include<bits/stdc++.h>
using namespace std;
#define int long long
int m;
bool last[311][311];
bool vis[311][311];
bool des[311][311];
int ans[311][311]={-1};
int dx[4]={1,0,-1,0};
int dy[4]={0,-1,0,1};
struct node{
    int x;
    int y;
};
struct note{
    int x;
    int y;
    int t;
}n[311];
signed main(){
    cin>>m;
    for(int i=1;i<=m;i++){
        cin>>n[i].x>>n[i].y>>n[i].t;
        last[n[i].x][n[i].y]=1;
        for(int j=0;j<4;j++){
            if(0<=n[i].x+dx[j] and n[i].x+dx[j]<=310 and 0<=n[i].y+dy[j] and n[i].y+dy[j]<=310) last[n[i].x+dx[j]][n[i].y+dy[j]]=1;
        }
    }
    sort(n+1,n+m+1,[](note n1,note n2){
        return n1.t<n2.t;
    });
    queue<note>q2;
    for(int i=1;i<=m;i++){
        q2.push(n[i]);
    }
    queue<node>q;
    q.push({0,0});
    ans[0][0]=0;
    vis[0][0]=1;
    if(last[0][0]==0){
        cout<<0;
        exit(0);
    }
    while(!q.empty()){
        node t=q.front();
        q.pop();
        if(last[t.x][t.y]==0){
            cout<<ans[t.x][t.y];
            exit(0);
        }
        while(!q2.empty() and q2.front().t==ans[t.x][t.y]){
            des[q2.front().x][q2.front().y]=1;
            for(int i=0;i<4;i++){
                if(0<=q2.front().x and q2.front().x<=310 and 0<=q2.front().y and q2.front().y<=310) des[q2.front().x+dx[i]][q2.front().y+dy[i]]=1;
            }
            q2.pop();
        }
        if(des[t.x][t.y]==1){
            ans[t.x][t.y]=-1;
            continue;
        }
        for(int i=0;i<4;i++){
            int xx=t.x+dx[i];
            int yy=t.y+dy[i];
            if(0<=xx and xx<=310 and 0<=yy and yy<=310 and vis[xx][yy]==0 and des[xx][yy]==0){
                vis[xx][yy]=1;
                ans[xx][yy]=ans[t.x][t.y]+1;
                q.push({xx,yy});
            }
        }
    }
    cout<<-1;
    return 0;
}

63pts
wa on t10-14

bfs做法 大佬求调


by xuhaicheng @ 2024-08-14 10:42:36

此帖结


|