这是我的第一个问题,是的,这是一项“家庭作业”任务。我已经工作了几个小时但无法使算法工作。我编写的程序应该包含一个接受12乘12阵列的函数,并找到结束的路径。主函数找到迷宫的开始并将其改为“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);
}
答案 0 :(得分:4)
在每个break;
代码块之后,您错过了switch(face)
中的case:
个语句。如果没有break;
语句,您的代码将落入下一个case:
中的每一个,这不是您想要的。
答案 1 :(得分:3)
我认为你的方向错了。你应该从maze[2][0]
开始,面向'6'。你想要前进到maze[2][1]
,面对仍然是'6'。但是如果你看一下方向6的mazeTraversal
代码,就会遇到这种情况:
if(maze[y][x+1] == "."){
maze[y][x+1] = "x";
face = 8;
}
因此,您正在错误地设置结果方向 可能有助于保持正确的一件事是使用枚举而不是随机数字代码:
enum Facing {
face_EAST,
face_SOUTH,
face_WEST,
face_NORTH } face = face_EAST;
我甚至可能会忘记指南针方向并使用特定问题的指示。这应该有助于在代码中保持方向。
enum Facing {
face_Xplus,
face_Yplus,
face_Xminus,
face_Yminus } face = face_Xplus;