#include<stdio.h>
int a[51][51],book[51][51];
int min = 99999999,n,m,p,q;
void dfs(int x,int y,int step){
int next[4][2] = {{0,1},{1,0},{0,-1},{-1,0}};
if(x == p && y == q){ //无论如何都要return 这是递归
if(step < min){
min = step;
}
return;
}
int tx,ty,k;
for(k = 0;k <= 3;k ++){
tx = x + next[k][0];
ty = y + next[k][1];
if( tx > n || tx < 1 || ty > m || ty < 1 ){
continue;
}
if(book[tx][ty] == 0 && a[tx][ty] == 0){
book[tx][ty] = 1;
dfs(tx,ty,step+1);
book[tx][ty] = 0;
}
} //括号?
return;
}
int main(){
int startx,starty;
int i,j;
scanf("%d %d",&n,&m);
for(i = 1;i <= n;i ++){
for(j = 1;j <= m;j ++){ //j <= m 不是 i <= m 下次不要直接复制
scanf("%d",&a[i][j]);
}
}
scanf("%d %d %d %d",&startx,&starty,&p,&q);
book[startx][starty] = 1;
dfs(startx,starty,0);
printf("%d\n",min);
return 0;
}
错误在注释