退出带有箭头的迷宫,如何修复代码?

时间:2019-04-14 06:46:10

标签: java maze

我想创建一个模拟带有箭头的迷宫出口的代码。我认为我走出迷宫有问题。规则是何时可以进入迷宫的框架并在箭头朝向框架的情况下退出框架。

这是代码:

public static void main(String[] args) {
    final int NUMBER_OF_COLUMNS = 6;
    final int NUMBER_OF_ROWS    = 5;

    boolean entryFound  = false;
    boolean exitFound   = false;
    boolean work        = true;
    int     entryIndex  = -1;
    int     exitIndex   = -1;
    int     row         = -1;
    int     column      = -1;

    char[][] pathMatrix = { 
                            { 'O', 'V', 'O', '^', '<', '<' }, 
                            { 'O', 'V', 'V', '*', '^', '^' },
                            { '*', 'V', '*', 'O', '*', '^' },
                            { 'O', 'V', 'O', 'O', 'V', '^' },
                            { 'O', '>', '>', '>', '>', '^' },  };

    for(column = 0; column < NUMBER_OF_COLUMNS; column++){
        if(pathMatrix[0][column] == 'V'){
            entryFound = true;
            entryIndex = column;
        }
        if(pathMatrix[NUMBER_OF_ROWS -1][column] == 'V'){
            System.out.println(NUMBER_OF_ROWS -1 + " " + column);
            exitFound = true;
            exitIndex = column;
        }

        if(pathMatrix[0][column] == '^'){
            exitFound = true;
            exitIndex = column;
        }

        if(pathMatrix[0][column] == '>'){
            exitFound = true;
            exitIndex = column;
        }

    }
    System.out.println(exitIndex);


    for(row = 0; row < NUMBER_OF_ROWS; row++){
        for(column = 0; column < NUMBER_OF_COLUMNS; column++){
            System.out.print(pathMatrix[row][column] + " ");
        }
        System.out.print('\n');
    }

    if(entryFound == false || exitFound == false){
        System.out.println("No path has been found in the matrix above");

        return;
    }
    row = 0;
    column = entryIndex;

    do
    {
        System.out.println(row+" "+column);

        if(pathMatrix[row][column] == 'V'){
            row++;
        }

        else if(pathMatrix[row][column] == '>'){
            column++;
        }
        else if(pathMatrix[row][column] == '<'){
            column--;
        }
        else if(pathMatrix[row][column] == '^'){
            row--;
        }
        else {
            work = false;
        }

    }
    while(work == true && (0 < row && row < NUMBER_OF_ROWS) && (0 < column && column < NUMBER_OF_COLUMNS));

    if(row == NUMBER_OF_ROWS && column == exitIndex){
        System.out.println("The path has been found in the maze above");
    }
    else{
        System.out.println("No path has been found in the maze above");
    }
}

这是Route :(从0.4退出,但不起作用)

O V O ^ < < 
O V V * ^ ^ 
* V * O * ^ 
O V O O V ^ 
O > > > > ^ 
0 1
1 1
2 1
3 1
4 1
4 2
4 3
4 4
4 5
3 5
2 5
1 5
No path has been found in the maze above

2 个答案:

答案 0 :(得分:1)

问题

您的代码中有2个错误:

1-您循环中的情况表明

while(work == true && (0 < row && row < NUMBER_OF_ROWS) && (0 < column && column < NUMBER_OF_COLUMNS));

0 < row是错误的,这意味着您不能在索引为0的第一行上运行。但是您希望能够执行此操作,因此实际上您需要编写0 <= row 。因此,请通过以下方式进行纠正:

while(work && (0 <= row && row < NUMBER_OF_ROWS) && (0 <= column && column < NUMBER_OF_COLUMNS));

2-验证是否找到出口的条件

if(row == NUMBER_OF_ROWS && column == exitIndex)

在这里您检查是否已由最下面一行退出,要检查的是您是否已由最上一行退出,即该行是-1。因此,以这种方式重写它:

if(row < 0 && column == exitIndex)

旁注

我认为您将获得很多可读性,并且如果将代码拆分为方法,测试起来也将容易得多。这是一个示例:

private static final char[][] pathMatrix = {
        { 'O', 'V', 'O', '^', '<', '<' },
        { 'O', 'V', 'V', '*', '^', '^' },
        { '*', 'V', '*', 'O', '*', '^' },
        { 'O', 'V', 'O', 'O', 'V', '^' },
        { 'O', '>', '>', '>', '>', '^' },  };

private static final int NUMBER_OF_COLUMNS = 6;
private static final int NUMBER_OF_ROWS    = 5;

public static void main(String[] args) {

    printMatrix();

    int[] currentPosition = findEntrance();

    System.out.println("Entrance: " + currentPosition[0] + " " +currentPosition[1] + " " + pathMatrix[currentPosition[0]][currentPosition[1]]);

    while(isInsideMatrix(currentPosition) && isArrow(currentPosition)) {
        System.out.println(currentPosition[0] + " " + currentPosition[1] + " " + pathMatrix[currentPosition[0]][currentPosition[1]]);
        currentPosition = move(currentPosition);
    }

    if(isInsideMatrix(currentPosition)) {
        System.out.println("No path has been found in the maze above");
    } else {
        System.out.println("The path has been found in the maze above");
    }
}

找到迷宫的入口。返回位置,形式为{rowIndex,colIndex}

的int []
private static int[] findEntrance() {
    char c;

    // scan first and last rows
    for(int column = 0; column < NUMBER_OF_COLUMNS; column++) {
        // first row
        c = pathMatrix[0][column];
        if(c == 'V') {
            return new int[] {0, column};
        }
        // last row
        c = pathMatrix[NUMBER_OF_ROWS-1][column];
        if(c == '^') {
            return new int[] {NUMBER_OF_ROWS-1, column};
        }
    }

    // scan first and last columns
    for(int row = 0; row < NUMBER_OF_ROWS; row++) {
        // first column
        c = pathMatrix[row][0];
        if(c == '>') {
            return new int[] {row, 0};
        }
        // last row
        c = pathMatrix[row][NUMBER_OF_COLUMNS-1];
        if(c == '<') {
            return new int[] {row, NUMBER_OF_COLUMNS-1};
        }
    }

    return null;
}

移动光标并返回下一个位置。假设我们当前正站在箭头上

private static int[] move(int[] position) {
    int row = position[0];
    int col = position[1];
    char charAtPosition = pathMatrix[position[0]][position[1]];

    int[] newPosition;
    if(charAtPosition == 'V') {
        newPosition = new int[] {row+1, col};
    } else if(charAtPosition == '^') {
        newPosition = new int[] {row-1, col};
    } else if(charAtPosition == '>') {
        newPosition = new int[] {row, col+1};
    } else if(charAtPosition == '<') {
        newPosition = new int[] {row, col-1};
    } else {
        throw new RuntimeException("Should never come in here.");
    }
    return newPosition;
}

检查在给定位置是否有箭头

private static boolean isArrow(int[] position) {
    int row = position[0];
    int col = position[1];
    char charAtPosition = pathMatrix[row][col];
    return charAtPosition == 'V' || charAtPosition == '^' 
        || charAtPosition == '<' || charAtPosition == '>';
}

检查给定位置是否在矩阵内部

private static boolean isInsideMatrix(int[] position) {
    int row = position[0];
    int col = position[1];
    return row >= 0 && row < NUMBER_OF_ROWS
        && col >= 0 && col < NUMBER_OF_COLUMNS;
}

打印矩阵

private static void printMatrix() {
    for(int row = 0; row < NUMBER_OF_ROWS; row++){
        for(int column = 0; column < NUMBER_OF_COLUMNS; column++){
            System.out.print(pathMatrix[row][column] + " ");
        }
        System.out.print('\n');
    }
}

这将输出:

O V O ^ < <
O V V * ^ ^
* V * O * ^
O V O O V ^
O > > > > ^
Entrance: 0 1 V
0 1 V
1 1 V
2 1 V
3 1 V
4 1 >
4 2 >
4 3 >
4 4 >
4 5 ^
3 5 ^
2 5 ^
1 5 ^
0 5 <
0 4 <
0 3 ^
The path has been found in the maze above

答案 1 :(得分:0)

就像其他人已经说过的那样,请尝试调试器或打印语句来测试您希望变量在执行的不同点处保留的值以及实际值是什么。在我看来,row==NUMBER_OF_ROWS不是您要在最终if中测试的内容。在您的示例中,您需要位于第一行才能退出。