求助,哪里出错了

P1746 离开中山路

Iverson_ @ 2022-08-17 17:13:31

#include<iostream>
#include<queue>
using namespace std;
int n,x1,x2,y1,y2;
int g[1010][1010];
char p[1010][1010];
bool vis[1010][1010]={false};
const int dx[4]={0,1,-1,0};
const int dy[4]={1,-1,0,0};
struct PII
{
    int x,y;
};
queue <PII> q;
int bfs(int x1,int y1)
{
    q.push((PII){x1,y1});
    vis[x1][y1]=true;
    while(!q.empty())
    {
        struct PII t=q.front();
        q.pop();
        for(int i=1;i<=4;i++)
        {
                int a=t.x+dx[i];
                int b=t.y+dy[i];
                if(a<=0||a>n||b<=0||b>n) continue;
                if(p[a][b]=='1'||vis[a][b]==true) continue;
                    vis[a][b]=true;
                    g[a][b]=g[t.x][t.y]+1;
                    q.push((PII){a,b});
        }
    }
    return g[x2][y2];
}
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        cin>>p[i][j];
     } 
     cin>>x1>>y1>>x2>>y2;
     cout<<bfs(x1,y1);
     return 0;
}


by caizehua @ 2022-08-17 17:29:56

const int dx[4]={0,1,-1,0};
const int dy[4]={1,-1,0,0};

数组下标从0开始

 for(int i=1;i<=4;i++)

i应该从0开始,i<4


by Iverson_ @ 2022-08-17 17:43:24

@caizehua 扫噶,mad看了半天了


|