33,010
社区成员
发帖
与我相关
我的任务
分享
#include<stdio.h>
int next[4][2] = {{0, 1}, {1,0}, {0,-1}, {-1, 0}};//每次递归都要调用,放在外面.
int p, q, min = 99999;
int map[101][101], vis[101][101];
void dfs(int x, int y, int step)
{
int tx, ty, k;
if (x == p && y == q)
{
if (min > step)
min = step;
return;
}
for (k = 0; k < 4; k++)
{
tx = x + next[k][0];
ty = y + next[k][1];//1.应该是ty=y+next
if (tx<1 || ty<1 || tx>5 || ty>4)//2. 看你的代码应该是5行4列
continue;
if (map[tx][ty] == 0 && vis[tx][ty] == 0)
{
vis[tx][ty] = 1;
dfs(tx, ty, step + 1);
vis[tx][ty] = 0;
}
}
return;
}
int main()
{
int sx, sy, i, j;
freopen("test_in.txt","r",stdin);
scanf_s("%d %d %d %d", &sx, &sy, &p, &q);
for (i = 1; i <= 5;i++)
for (j = 1; j <= 4; j++)
scanf_s("%d", &map[i][j]);
vis[sx][sy] = 1;
dfs(sx, sy, 0);
printf("%d", min);
return 0;
}
/*
1 1 4 3
0 0 1 0
0 0 0 0
0 0 1 0
0 1 0 0
0 0 0 1
*/