这是我的第一个问题,是,这是“家务”任务。 我一直在工作数小时,但不能用算法工作。 第一期书面节目本应包含一项功能,接受12个阵列,并找到了结束的道路。 主要职能是马奇的开始,将其改为“x”,以代表“角色”的地位。 然后,主要职能指马齐蓬特功能,该功能在清理屏幕后接受阵列打印。 然后,它称为接受阵列的马齐-特拉佛功能。 麻木动物运动的第一部分试图确定“x”的位置,代之以“......”。 下一个部分应确定“x”所面临的方向。 我使用了4、5、6和8(西部、南部、东部和北部(在座标上角)来代表它所面临的方式。 根据它所面临的方式,马齐德河试图确定是否存在一条通往权利的开放道路,然后是前线,然后是左边,然后将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.
");
printf("Press Space Bar to watch the X move.
");
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("
");
}
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);
}