QAQ_liu @ 2024-08-21 16:23:10
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,m,sum,d[4][2]={{0,1},{1,0},{0,-1},{-1,0}};
char a[1005][1005];
void dfs(int x,int y,int s){
int nx,ny;
char h;
if(x==n&&y==m) sum=max(sum,s);
if(a[x][y]=='1') h='1';
else h='0';
for(int i=0;i<4;i++){
nx=x+d[i][0];
ny=y+d[i][1];
if(nx<1||ny<1||nx>n||ny>n) continue;
if(a[nx][ny]=='1'&&h!=a[nx][ny]) dfs(nx,ny,s+1);
else continue;
}
}
signed main(){
cin>>n>>m;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++) cin>>a[i][j];
}
while(m--){
sum=0;
int x,y;
cin>>x>>y;
dfs(x,y,0);
cout<<sum<<"\n";
}
return 0;
}
by xionghao2013 @ 2024-08-21 16:34:33
让我看看
by xionghao2013 @ 2024-08-21 16:35:30
你用深搜做???
by xionghao2013 @ 2024-08-21 16:35:48
不应该用广搜吗
by QAQ_liu @ 2024-08-21 16:37:35
@xionghao2013 e,没学广搜
by xionghao2013 @ 2024-08-21 16:38:10
搞错了
by xionghao2013 @ 2024-08-21 16:43:12
第16行应该是h==a[nx][ny]
by xionghao2013 @ 2024-08-21 16:43:49
写错了
by xionghao2013 @ 2024-08-21 16:44:06
再让我看看
by xionghao2013 @ 2024-08-21 16:50:33
你应该加一个数组表示这个格子走过没有,走过就不能走了,否则会停不下来的
by xionghao2013 @ 2024-08-21 16:53:15
还有一些小问题,帮你改好了,样例能过了