样例输出-1,求助

P1746 离开中山路

XingChen_MoNian @ 2024-02-07 21:01:19

为什么大家都是用结构体,就我用pair吗? pair做不了吗?我的代码求大家debug

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
int n,vis[1005][1005],x,y,xx,yy,tmp[1005][1005],X[]={1,-1,0,0},Y[]={0,0,1,-1};
char a[1005][1005];
int bfs(int x,int y){
    queue<pii> q;
    q.push({x,y});
    vis[x][y]=1;
    while(!q.empty()){
        pii p=q.front();
        q.pop();
        int u=p.first,v=p.second;
        if(u==xx && v==yy) return tmp[u][v];
        for(int i=0;i<4;i++){
            int idx=x+X[i],idy=y+Y[i];
            if(idx<1 || idx>n || idy<1 || idy>n || vis[idx][idy] || a[idx][idy]=='1') continue;
            tmp[idx][idy]=tmp[u][v]+1;
            vis[idx][idy]=1;
            q.push({idx,idy});
        }
    }
    return -1;
}
int main(){
//  freopen(".in","r",stdin);
//  freopen(".out","w",stdout);
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    cin>>n;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            cin>>a[i][j];       
        }
    }
    cin>>x>>y>>xx>>yy;
    cout<<bfs(x,y);
//  fclose(stdin);
//  fclose(stdout);
    return 0;
}
/*

*/

by weak_in_code @ 2024-02-07 21:40:26

结构体可做pair肯定可做啊。。


by NO_OI_NO_LIFE @ 2024-02-07 21:41:52

@xhx_st2025 @XingChen_MoNian

int idx=x+X[i],idy=y+Y[i];

改为

int idx=u+X[i],idy=v+Y[i];

by weak_in_code @ 2024-02-07 21:42:12

不过这么惨吗,现在还没放假


by NO_OI_NO_LIFE @ 2024-02-07 21:43:09

@xhx_st2025 原来其他人也有人看杀戮天使


by weak_in_code @ 2024-02-07 21:43:53

@zyh0516_lucky 正确的,我是札克厨


by NO_OI_NO_LIFE @ 2024-02-07 21:46:30

@xhx_st2025 介意HQK这个称号吗


by weak_in_code @ 2024-02-07 21:47:55

@zyh0516_lucky 我也这么叫。。但并不妨碍他脸好看(因为缠着绷带


by NO_OI_NO_LIFE @ 2024-02-08 13:56:23

@xhx_st2025 去掉绷带也好看


by XingChen_MoNian @ 2024-02-09 15:32:01

@zyh0516_lucky 谢谢,关注了


|