include
using namespace std;
int m, n, p, q, min_step = 99999;
int map[20][20], visited[20][20];
void dfs(int x, int y, int step) {
//到达终点
if (x == p && y == q) {
if (step < min_step) {
min_step=step;
return;
}
}
/*当前格实施顺时针试探的原则,也就是右下左上
比如当前位置是(x,y),如果:
右移一步(x,y+1),下移一步(x+1,y)
左移一步(x,y-1),上移一步(x-1,y)
/
//向右试探
if (map[x][y + 1] == 1 && visited[x][y + 1] == 0) {
visited[x][y + 1] = 1;
dfs(x, y + 1, step + 1);
visited[x][y + 1] = 0;
}
//向下试探
if (map[x + 1][y] == 1 && visited[x + 1][y] == 0) {
visited[x + 1][y] = 1;
dfs(x + 1, y, step + 1);
visited[x + 1][y] = 0;
}
//向左试探
if (map[x][y - 1] == 1 && visited[x][y - 1] == 0) {
visited[x][y - 1] = 1;
dfs(x, y - 1, step + 1);
visited[x][y - 1] = 0;
}
//向上试探
if (map[x - 1][y] == 1 && visited[x - 1][y] == 0) {
visited[x - 1][y] = 1;
dfs(x - 1, y, step + 1);
visited[x - 1][y] = 0;
}
}
/
5 4
1 1 0 1
1 1 1 1
1 1 0 1
1 0 1 1
1 1 1 0
1 1 4 3
*/
int main() {
int startX, startY;
scanf("%d%d", &m, &n); //初始化迷宫数据关系值
for (int i = 1; i <= m; i++) {for (int j = 1; j <= n; j++) {scanf("%d", &map[i][j]);}
}
//初始化起始和重点
scanf("%d%d%d%d", &startX, &startY,&p,&q);
visited[0][0]=1;
dfs(startX,startY,0);
printf("%d",min_step);
return 1;
}