gpt4 book ai didi

c - C中的右手迷宫遍历

转载 作者:行者123 更新时间:2023-12-04 10:35:42 24 4
gpt4 key购买 nike

这是我的第一个问题,是的,这是一个“家庭作业”作业。我已经为此工作了几个小时,但无法使算法正常工作。我编写的程序应该包含一个函数,该函数接受一个 12 x 12 数组并查找到末尾的路径。 main 函数找到迷宫的起点并将其更改为“x”以表示“玩家”的位置。然后主函数调用接受数组的 mazePrint 函数,在清除屏幕后将其打印出来。然后它调用接受数组的 mazeTraversal 函数。 mazeTraversal 的第一部分尝试定位“x”的位置并将其替换为“.”。下一部分应该确定“x”面向哪个方向。我用 4、5、6 和 8(西、南、东、北(看数字键盘))来表示它面向的方向。根据它面对的方式,mazeTraversal 尝试确定是否有一条开放路径向右,然后在前面,然后在左边,然后在后面,然后将 X 放在那个位置并改变 x 的方式面对。当我运行程序时,第二次移动后出现问题。感谢您的帮助,如果这不是提出此类问题的地方,我们深表歉意。

#include <stdio.h>
#include <stdlib.h>

void mazePrint(char *maze[12][12]);
void mazeTraversal(char *maze[12][12]);
static int face = 6;

main()
{
int i = 0;
int j = 0;
int k = 0;
int start;
int xpos;

char *mazeone[12][12] = {
//0///1///2///3///4///5///6///7///8///9///10//11///
"#","#","#","#","#","#","#","#","#","#","#","#",//0
"#",".",".",".","#",".",".",".",".",".",".","#",//1
".",".","#",".","#",".","#","#","#","#",".","#",//2
"#","#","#",".","#",".",".",".",".","#",".","#",//3
"#",".",".",".",".","#","#","#",".","#",".",".",//4
"#","#","#","#",".","#",".","#",".","#",".","#",//5
"#","#",".","#",".","#",".","#",".","#",".","#",//6
"#","#",".","#",".","#",".","#",".","#",".","#",//7
"#",".",".",".",".",".",".",".",".","#",".","#",//8
"#","#","#","#","#","#",".","#","#","#",".","#",//9
"#",".",".",".",".",".",".","#",".",".",".","#",//10
"#","#","#","#","#","#","#","#","#","#","#","#",};//11

for (i = 0; i <12; i++)
if (mazeone[i][0] == "." ) {
start = i;
mazeone[start][0] = "x";
xpos = start;
break;
}

printf("X is the starting point.\n");
printf("Press Space Bar to watch the X move.\n\n\n");
getchar();
mazePrint(mazeone);
getchar();
return 0;
}

void mazePrint(char *maze[12][12])
{
int x = 0;
int y = 0;

system("cls");
for (x = 0; x < 12; x++) {
for (y = 0; y < 12; y++) {
printf("%s", maze[x][y]);
}
printf("\n");
}
getchar();
mazeTraversal(maze);
}

void mazeTraversal(char *maze[12][12])
{
int x = 0;
int y = 0;

for (x = 0; x < 12; x++) {
for (y = 0; y < 12; y++) {
if (maze[y][x] == "x")
break;
}
if(maze[y][x] == "x")
break;
}

for (y = 0; y < 12; y++) {
for (x = 0; x < 12; x++) {
if (maze[y][x] == "x")
break;
}
if (maze[y][x] == "x")
break;
}

maze[y][x] = ".";

switch (face) {
case 6:{
if (maze[y][x-1] == ".") {
maze[y][x-1] = "x";
face = 5;
} else if (maze[y + 1][x] == ".") {
maze[y + 1][x] = "x";
face = 6;
} else if (maze[y][x+1] == ".") {
maze[y][x+1] = "x";
face = 8;
} else if (maze[y - 1][x] == ".") {
maze[y - 1][x] = "x";
face = 4;
}
}
case 8:{
if (maze[y + 1][x] == ".") {
maze[y + 1][x] = "x";
face = 6;
} else if (maze[y][x+1] == ".") {
maze[y][x+1] = "x";
face = 8;
} else if (maze[y - 1][x] == ".") {
maze[y - 1][x] = "x";
face = 4;
} else if (maze[y][x-1] == ".") {
maze[y][x-1] = "x";
face = 5;
}
}
case 4:{
if (maze[y][x+1] == ".") {
maze[y][x+1] = "x";
face = 8;
} else if (maze[y - 1][x] == ".") {
maze[y - 1][x] = "x";
face = 4;
} else if (maze[y][x-1] == ".") {
maze[y][x-1] = "x";
face = 5;
} else if (maze[y + 1][x] == ".") {
maze[y + 1][x] = "x";
face = 6;
}
}
case 5:{
if (maze[y - 1][x] == ".") {
maze[y - 1][x] = "x";
face = 4;
} else if (maze[y][x-1] == ".") {
maze[y][x-1] = "x";
face = 5;
} else if (maze[y + 1][x] == ".") {
maze[y + 1][x] = "x";
face = 6;
} else if (maze[y][x+1] == ".") {
maze[y][x+1] = "x";
face = 8;
}
}
}

mazePrint(maze);
}

最佳答案

在每个 case: 代码块之后,您缺少 switch(face) 中的 break; 语句。如果没有 break; 语句,您的代码将落到下一个 case: 中,这不是您想要的。

关于c - C中的右手迷宫遍历,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10132459/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com