求助 满江红

P1746 离开中山路

kevinf @ 2024-10-21 12:02:38

#include<bits/stdc++.h>
using namespace std;
char ma[1005][1005];
const int dx[4]={0,0,1,-1},dy[4]={1,-1,0,0};
struct point{
    int x,y,len;
};
bool vis[1005][1005];
vector<point> now,later;
int main(){
    memset(vis,0,sizeof(vis));
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            char c;
            scanf("%c",&c);
            if(c=='\n'){
                scanf("%c",&c);
                ma[i][j]=c;
                continue;
            }
            ma[i][j]=c;

        }   

    }

    int sx,sy,ex,ey;
    scanf("%d%d%d%d",&sx,&sy,&ex,&ey);
    later.push_back(point({sx,sy,0}));
    int ans=0x7fffff;
    while(!later.empty()){
        swap(now,later);
        later.clear();
        for(int i=0;i<now.size();i++){
            auto index=now[i];
            if(index.x==ex&&index.y==ey){
                ans=min(ans,index.len);
                continue;
            }
            for(int j=0;j<4;j++){
                int nx=index.x+dx[j],ny=index.y+dy[j];
                if(nx>=1&&ny>=1&&nx<=n&&ny<=n&&ma[nx][ny]=='0'&&!vis[nx][ny]){
                    later.push_back(point({nx,ny,index.len+1}));
                    vis[nx][ny]=1;
                }
            }
        }
    }
    printf("%d",ans);
    return 0;
}

本地跑答案是对的 in and out


by kuCna @ 2024-10-21 22:23:41

@kevinf 一开的输入数据有问题


by kuCna @ 2024-10-21 22:24:34

@kuCna 输入有问题


by kevinf @ 2024-10-23 09:50:43

@kuCna 谢谢


|