简体   繁体   English

需要Java代码说明(递归迷宫求解器)

[英]Java code explanation needed (recursive maze solver)

There might come some follow up questions for this (and yes this is homework), but we have to solve a maze in my Java programming class (complete beginner) with using a recursive method. 对此可能会有一些后续问题(是的,这是家庭作业),但是我们必须使用递归方法来解决我的Java编程类(完整的初学者)中的迷宫。 Now my problem is not the actual solving (yet, I am sure it will soon be my problem), but the fact that we have been given a part of the code by the teacher and there is some stuff in it that I have absolutely no clue what it is, or what it means. 现在我的问题不是实际的解决方案(但是,我敢肯定,这很快将成为我的问题),但是事实上,老师已经给了我们一部分代码,并且其中有些东西我绝对没有。了解它是什么,或它的意思。

Yes, I could ask my teacher, but I really don't like this guy and I am really just trying to learn Java and get some college credit points. 是的,我可以问我的老师,但是我真的不喜欢这个人,我只是想学习Java并获得一些大学学分。

public class MazeSolver {

    public static void main(String[] args) {

        int[][] mArr = {
            {2, 1, 1, 1, 0, 1, 1, 1},
            {1, 1, 0, 1, 0, 1, 1, 0},
            {0, 1, 1, 0, 1, 1, 1, 1},
            {0, 0, 1, 1, 1, 0, 1, 0},
            {1, 0, 0, 0, 1, 0, 1, 1},
            {0, 0, 0, 0, 1, 1, 0, 1},
            {0, 0, 0, 0, 0, 1, 1, 3}
        };

        boolean result = solve(mArr, 0, 0); // i = 0, j = 0: Point of entry in the upper left corner
        String str = (result) ? "" : " nicht";
        System.out.println("Das Labyrinth ist" + str + " loesbar");
    }

        static boolean solve(int[][] mArr, int i, int j) {


        return false;
    }

        static void print(int[][] mArr) {
        System.out.println();
        for (int[] arr : mArr) {
            for (int n : arr) {
                System.out.print(n + " ");
            }
            System.out.println();
        }
        System.out.println();
    }

}

Ok, so my problem is this line: boolean result = solve(mArr, 0, 0); 好的,所以我的问题是这一行:布尔结果= solve(mArr,0,0); // i = 0, j = 0: Point of entry in the upper left corner . // i = 0,j = 0:入口点在左上角。

I am assuming this means that result is the result of the method which I am still to define, but what is (mArr, 0, 0) ? 我假设这意味着结果是我仍然要定义的方法的结果,但是(mArr, 0, 0)什么? I am guessing that is supposed to give the location in the maze but I thought that a location in an array is ie mArr [0][0] . 我猜想应该在迷宫中给出位置,但是我认为数组中的位置就是mArr [0][0] And how does the program know that 0 and 0 is i and j or is that something that I need to tell it at some point? 程序如何知道0和0是i和j或在某个时候需要告诉我的东西?

solve() calls the method you have defined static boolean solve(int[][] mArr, int i, int j) { . static boolean solve(int[][] mArr, int i, int j) { ()调用您已定义的方法static boolean solve(int[][] mArr, int i, int j) { It passes the array, along with i and j to that method. 它将数组以及i和j传递给该方法。 i and j represent the start position. i和j代表开始位置。 You are correct that the starting position would be mArr[i][j], so inside the solve method, do this. 您的正确位置是起始位置为mArr [i] [j],因此在Solve方法内部执行此操作。

ie

static boolean solve(int[][] mArr, int i, int j) {
    int startPosValue = mArr[i][j]
    //code to determine return value
}

Let's assume that you can only move right or down, as otherwise it's not possible to solve with those three arguments. 假设您只能向右或向下移动,否则无法使用这三个参数来解决。 Then, the algorithm will be: 然后,该算法将是:

static boolean solve(int[][] mArr, int i, int j) {
    if (mArr[i][j] == 3) {
        return true;
    } else {
    try {
        if (arr[i+1][j] == 1 &&solve(mArr, i+1, j))
            return true;
    }catch(IndexOutOfBoundsException e) {};
    try {
        if (arr[i][j+1] == 1 && solve(mArr, i, j+1))
            return true;
    }catch(IndexOutOfBoundsException e) {};
    return false;
}

It'll return true if there is existing path and false otherwise. 如果存在路径,则返回true ,否则返回false

How it works? 这个怎么运作?

The signature could be rewritten: 签名可以重写:

static boolean solve(int[][] mArr, int currentX, int currentY)

It checks if mArr[i][j] is 3 (end). 它检查mArr[i][j]是否为3(结束)。 If yes, it returns true . 如果是,则返回true Otherwise it checks if there is a possibility to reach the end by moving right or down. 否则,它将检查是否有可能通过向右或向下移动到达终点。

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

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