简体   繁体   English

C中的右手迷宫遍历

[英]Right hand maze traversal in C

This is my first question, and yes, it is a "homework" assignment. 这是我的第一个问题,是的,这是一项“作业”作业。 I have been working on it for hours but cannot get the algorithm to work. 我已经工作了几个小时,但无法使算法正常工作。 The program I've written is supposed to contain a function that accepts a 12 by 12 array and finds the path to the end. 我编写的程序应该包含一个接受12 x 12数组并找到末尾路径的函数。 The main function finds the start of the maze and changes it to an "x" to represent the position of the "player." 主要功能找到迷宫的起点并将其更改为“ x”以表示“玩家”的位置。 The main function then calls the mazePrint function that accepts the array prints it after clearing the screen. 然后,主函数调用mazePrint函数,该函数接受数组在清除屏幕后将其打印出来。 Then it calls the mazeTraversal function which accepts the array. 然后,它调用接受数组的mazeTraversal函数。 The first part of mazeTraversal attempts to locate the position of "x" and replace it with a "." mazeTraversal的第一部分尝试找到“ x”的位置并将其替换为“。”。 The next part is supposed to determine which direction the "x" is facing. 下一部分应该确定“ x”面向的方向。 I used 4, 5, 6, and 8 (west, south, east, and north (look at the number pad)) to represent the way it is facing. 我用4、5、6和8(西,南,东和北(请看数字键盘))表示它的朝向。 Based on the way it is facing, mazeTraversal attempts to determine if there is an open path to the right, then in front, then to the left, and then behind and to then put an X in that position and change the way the x is facing. 根据其面对的方式,mazeTraversal尝试确定右侧是否有开放路径,然后是前方,然后是左侧,然后是背后,然后将X放置在该位置并更改x的方式面对。 Something goes wrong after the second move when I run the program. 当我运行程序时,第二步移动后出现了问题。 Thanks for any help, and sorry if this is not the place for such questions. 感谢您的任何帮助,如果不是这样的问题,对不起。

#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);
}

You are missing the break; 你错过了break; statements in the switch(face) after each case: block of code. 每种case:后在switch(face)中的语句case:代码块。 Without the break; 没有break; statements, your code will fall through to each of the next case: , which is not what you want. 语句,您的代码将进入下case: ,这不是您想要的。

I think you've got your directions wrong. 我认为您的路线指示有误。 You should be starting at maze[2][0] , with facing '6'. 您应该从maze[2][0] ,面向'6'。 You want to advance to maze[2][1] , with facing still '6'. 您想前进到maze[2][1] ,仍然面对'6'。 But if you look at the mazeTraversal code for direction 6, you get this case: 但是,如果您查看方向6的mazeTraversal代码,您会得到以下情况:

if(maze[y][x+1] == "."){     
   maze[y][x+1] = "x";     
   face = 8;
 }

So you are setting the resulting direction incorrectly. 因此,您错误地设置了结果方向。
One thing that might help keep it straight is to use an enumeration instead of random numeric codes: 可能有助于保持直率的一件事是使用枚举而不是随机数字代码:

enum Facing {
   face_EAST,
   face_SOUTH,
   face_WEST, 
   face_NORTH } face = face_EAST;

I might even forget about compass directions and use directions specific to the problem. 我什至可能会忘记指南针方向,而使用针对该问题的方向。 That should help keep the direction straight in the code. 这应该有助于保持代码中的方向正确。

enum Facing {
   face_Xplus,
   face_Yplus, 
   face_Xminus, 
   face_Yminus } face = face_Xplus;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM