关于走迷宫的一道问题~输出所有可能路径
测试数据:340111000101001023代码如下:(不知道错在哪里)#include<stdio.h>#defineMAXN10intMaze[MAXN][MAXN...
测试数据:
3 4
0 1 1 1
0 0 0 1
0 1 0 0
1 0 2 3
代码如下:(不知道错在哪里)
# include <stdio.h>
# define MAXN 10
int Maze[MAXN][MAXN], x, y;
int dx[4] = {-1, 1, 0, 0},
dy[4] = {0, 0, -1, 1};
int RouteX[MAXN], RouteY[MAXN];
int x2, y2, total, n, m;
void dfs(int xt, int yt, int rtsum)
{
if(xt == x2 && yt == y2) {
total++;
printf("%d:\n", total);
printf("(%d,%d)", RouteX[0], RouteY[0]);
for(int i = 1; i < rtsum; i++)
printf("->(%d, %d)", RouteX[i], RouteY[i]);
printf("\n");
} else for(int i = 0; i < 4; i++) {
if(xt+dx[i] >= 0 && yt+dy[i] >= 0 && xt+dx[i] < n && yt+dy[i] < m)
if(!Maze[xt+dx[i]][yt+dy[i]]) {
RouteX[rtsum] = xt+dx[i]; RouteY[rtsum] = yt+dy[i];
dfs(RouteX[rtsum], RouteY[rtsum], rtsum+1);
}
}
}
int main(void)
{
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
scanf("%d%d", &n, &m);
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++)
scanf("%d", &Maze[i][j]);
scanf("%d%d%d%d", &x, &y, &x2, &y2);
RouteX[0] = x; RouteY[0] = y;
dfs(x, y, 1);
return 0;
}
//如果把MAXN的值加大的话,输出的也不同~怎么搞的╮(╯▽╰)╭ 展开
3 4
0 1 1 1
0 0 0 1
0 1 0 0
1 0 2 3
代码如下:(不知道错在哪里)
# include <stdio.h>
# define MAXN 10
int Maze[MAXN][MAXN], x, y;
int dx[4] = {-1, 1, 0, 0},
dy[4] = {0, 0, -1, 1};
int RouteX[MAXN], RouteY[MAXN];
int x2, y2, total, n, m;
void dfs(int xt, int yt, int rtsum)
{
if(xt == x2 && yt == y2) {
total++;
printf("%d:\n", total);
printf("(%d,%d)", RouteX[0], RouteY[0]);
for(int i = 1; i < rtsum; i++)
printf("->(%d, %d)", RouteX[i], RouteY[i]);
printf("\n");
} else for(int i = 0; i < 4; i++) {
if(xt+dx[i] >= 0 && yt+dy[i] >= 0 && xt+dx[i] < n && yt+dy[i] < m)
if(!Maze[xt+dx[i]][yt+dy[i]]) {
RouteX[rtsum] = xt+dx[i]; RouteY[rtsum] = yt+dy[i];
dfs(RouteX[rtsum], RouteY[rtsum], rtsum+1);
}
}
}
int main(void)
{
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
scanf("%d%d", &n, &m);
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++)
scanf("%d", &Maze[i][j]);
scanf("%d%d%d%d", &x, &y, &x2, &y2);
RouteX[0] = x; RouteY[0] = y;
dfs(x, y, 1);
return 0;
}
//如果把MAXN的值加大的话,输出的也不同~怎么搞的╮(╯▽╰)╭ 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询