关于O2优化的疑惑

P2895 [USACO08FEB] Meteor Shower S

taoqiniu @ 2024-10-31 19:31:51

#include<bits/stdc++.h>
using namespace std;
struct node{
    int x,y;
};
queue<node> q;
int ans[500][500],death[500][500];
int walk[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
int m,Ans=100000;
int main(){
    cin>>m;
    memset(ans,-1,sizeof(ans));
    memset(death,0x7f,sizeof(death));
    for(int i=1;i<=m;i++){
        int x,y,t;
        cin>>x>>y>>t;
        death[x][y]=min(death[x][y],t);
        for(int k=0;k<4;k++){
            int tx=x+walk[k][0],ty=y+walk[k][1];
            death[tx][ty]=min(death[tx][ty],t);
        }
    }
    q.push((node){0,0});
    ans[0][0]=0;
    while(!q.empty()){
        node u=q.front();
        q.pop();
        int ux=u.x,uy=u.y;
        for(int k=0;k<4;k++){
            int x=ux+walk[k][0],y=uy+walk[k][1];
            if(x<0 || y<0 || ans[x][y]!=-1 || ans[ux][uy]+1>=death[x][y] || x>305 || y>305) continue;
            ans[x][y]=ans[ux][uy]+1;
            q.push((node){x,y});
        }
    }
    for(int i=0;i<=305;i++){
        for(int j=0;j<=305;j++){
            if(death[i][j]>1000 && ans[i][j]!=-1) Ans=min(Ans,ans[i][j]);
        }
    }
    if(Ans==100000) puts("-1");
    else cout<<Ans;
    return 0;
}

为什么开O2全wa啊
不开O2
开O2


by LionBlaze @ 2024-10-31 19:39:42

@taoqiniu 数组越界


by LionBlaze @ 2024-10-31 19:40:02

@taoqiniu

    for(int i=0;i<=305;i++){
        for(int j=0;j<=305;j++){

there.


by SuperAlex4 @ 2024-10-31 19:55:12

for(int k=0;k<4;k++){
    int tx=x+walk[k][0],ty=y+walk[k][1];
    death[tx][ty]=min(death[tx][ty],t);
}

判一下


by taoqiniu @ 2024-10-31 20:28:20

@SuperAlex4 @LionBlaze
谢谢 已关


|