RY111 @ 2024-06-19 18:31:14
题目给的样例都输出-1;大佬帮忙看看吧
#include<bits/stdc++.h>
#define x first
#define y second
//define后面不用分号;
//define a b 是将b定义成a;注意顺序;
using namespace std;
typedef pair<int,int> PII;
const int N=1010;
char G[N][N];
int dist[N][N];
int n;
queue<PII> q;
int dx[]={0,1,0,-1};
int dy[]={1,0,-1,0};
int dfs(int x1,int y1,int x2,int y2)
{
//忘记初始化数组
memset(dist,-1,sizeof(dist));
dist[x1][y1]=0;
q.push({x1,y1});
while(q.size())
{auto t=q.front();
q.pop();
for(int i=0;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(G[a][b]=='1')
continue;
if(dist[a][b]>=0)
continue;
q.push({a,b});
dist[a][b]=1+dist[t.x][t.y];
if(a==x2&&b==y2)
return dist[x2][y2];
}
}
return -1;
}
int main()
{
cin>>n;
int x1,x2,y1,y2;
for(int i=0;i<n;i++)
{
scanf("%s",G[i]);
}
cin>>x1>>y1>>x2>>y2;
cout<<dfs(x1,y1,x2,y2);
return 0;
}
by ouxiyao @ 2024-07-15 17:16:27
32行应为if(a<1||a>n||b<1||b>m)