使用递归和回溯解决迷宫问题

时间:2019-02-19 18:43:02

标签: java algorithm recursion

我正在尝试使用Java解决迷宫问题,代码仅使用两个方向(向右和向下)完全可以正常工作,但是,我想使其在所有方向上搜索/移动,从而引发错误(堆栈溢出) 。

以下代码:

  public class Maze {

    int n,m,startX,startY,endX,endY;

    public Maze(int n,int m) {
        this.n = n;
        this.m = m;
    }

    public void findLocation(int[][]array)
    {
        for(int i=0; i < array.length;i++)
            for(int j = 0 ; j <array[i].length;j++)
                {if(array[i][j] == 3)
                {
                    startX = i;
                    startY = j;
                }
                if(array[i][j] == 9)
                {
                    endX = i;
                    endY = j;
                }
                }
    }

    public boolean isSafe(int[][]array,int x, int y)
    {
        return (x>=0 && x < n && y>=0 && y<m && array[x][y]!=0);
    }

    public boolean solution(int[][] array)
    {
        int[][]sol = new int[n][m];

        findLocation(array);
        if(solutionUtil(array,startX,startY,sol)==false)
        {
            System.out.println("no solution");
            return false;
        }

        printsolution(sol);
        return true;
    }

private void printsolution(int[][] sol) {
        // TODO Auto-generated method stub
    for(int i=0; i < sol.length;i++)
    {   for(int j = 0 ; j <sol[i].length;j++)
             System.out.print(" " + sol[i][j] + 
                     " "); 
        System.out.println(); 
             }
    }

private boolean solutionUtil(int[][] array, int x, int y, int[][] sol) {
        // TODO Auto-generated method stub
        if(x == endX && y == endY)
        {
            sol[x][y] = 1;
            return true;
        }

        if(isSafe(array,x,y))
        {
            sol[x][y] = 1;

            if(x!=0)
            {
                if(solutionUtil(array,x-1,y,sol)==true)
                    return true;
            }
            if(y!=0)
            {
                if(solutionUtil(array,x,y-1,sol)==true)
                    return true;
            }

            if(solutionUtil(array,x+1,y,sol)==true)
                return true;
            if(solutionUtil(array,x,y+1,sol)==true)
                return true;

            sol[x][y] =0;
            return false;
        }

        return false;
    }

public static void main(String[] args)
{

    int[][] array = {{0, 0, 0, 0}, 
            {1, 1, 3, 1}, 
            {0, 1, 0, 0}, 
            {1, 1, 1, 9}};

    Maze run = new Maze(4,4);
    run.solution(array);


}

}

起点是3,终点是9,1表示它可以移动,0表示它不能移动(障碍),我该如何避免错误,因此,可以遍历所有方向,我的代码有误吗?

1 个答案:

答案 0 :(得分:1)

您无需检查之前是否未尝试过该步骤,因此您的代码可以进入重复步骤的无限循环。

将解决方案数组传递到function mysql_insert_array_serialised_test($conn, $table, $data, $addon = "mc_api") { $random_code = substr(str_shuffle(MD5(microtime())), 0, 12); $settings = serialize($data); $uid = $_SESSION['uid']; $sql_insert = 'INSERT INTO ' . $table . ' (uid, addon, sn, settings) VALUES (?, ?, ?, ?)'; if ($stmt = $conn->prepare($sql_insert)) { $stmt->bind_param('ssss', $uid, $addon, $random_code, $settings); $stmt->execute(); if (!$stmt->execute()) { echo $stmt->error; } else { return array( "mysqli_error" => false, "sn" => $random_code ); } } else { echo 'Could not prepare statement!'; } } 并检查之前是否未访问过坐标:

isSafe