加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 在迷宫中修改过的老鼠

发布时间:2020-12-15 01:07:46 所属栏目:Java 来源:网络整理
导读:在接受采访时我问过你.你给我的矩阵填充了“1”和“0”.“1”表示你可以使用单元格,“0”表示单元格被阻挡.你可以向4个方向移动左,右,上,下每次你向上或向下移动它都会花费你1美元.向左或向右移动不会增加成本.所以你必须编写一个代码来检查是否可以达到目的

在接受采访时我问过你.你给我的矩阵填充了“1”和“0”.“1”表示你可以使用单元格,“0”表示单元格被阻挡.你可以向4个方向移动左,右,上,下每次你向上或向下移动它都会花费你1美元.向左或向右移动不会增加成本.所以你必须编写一个代码来检查是否可以达到目的地索引( x,y)从(0,0)开始.如果是,则打印最低成本,否则打印“-1”.

我无法计算成本.
这是我的代码

public class Test {

    /**
     * @param args the command line arguments
     */
    static int dest_x = 0;
    static int dest_y = 0;
    static int cost = 0;
    static int m = 0;
    static int n = 0;

    public static void main(String[] args) {
        // TODO code application logic here
        Scanner in = new Scanner(System.in);
        Test rat = new Test();
        int maze[][] = {
                {1,1,1},{1,0},{0,1}
        };
        dest_x = in.nextInt();
        dest_y = in.nextInt();
        m = in.nextInt();
        n = in.nextInt();
        if (rat.solveMaze(maze))
            System.out.println("YES");

        else {
            System.out.println("NO");
        }
        System.out.println("cost = " + cost);
    }

    boolean isSafe(int maze[][],int x,int y) {
        // if (x,y outside maze) return false
        return (x >= 0 && x < m && y >= 0 &&
                y < n && maze[x][y] == 1);
    }

    boolean solveMaze(int maze[][]) {
        int sol[][] = {{0,0}
        };

        if (!solveMazeUtil(maze,sol)) {
            return false;
        }
        return true;
    }

    boolean solveMazeUtil(int maze[][],int y,int sol[][]) {
        // if (x,y is goal) return true
        if (x <= dest_x || y <= dest_y)
            if (x == dest_x && y == dest_y) {
                sol[x][y] = 1;
                return true;
            } else if (sol[x][y] == 1) {
                return false;
            }

            // Check if maze[x][y] is valid
            else if (isSafe(maze,x,y)) {
                // mark x,y as part of solution path
                sol[x][y] = 1;

                // Move forward in x direction 
                if (solveMazeUtil(maze,x + 1,y,sol)) {
                    //System.out.println("here at x+1 x = " + x  + " y = " + y);
                    return true;
                }

                // Move down in y direction 
                if (solveMazeUtil(maze,y + 1,sol)) {
                    //System.out.println("here at y+1 x = " + x  + " y = " + y);
                    cost++;
                    return true;
                }
                cost--;
                if (solveMazeUtil(maze,x - 1,sol)) {
                    // System.out.println("here at x-1 x = " + x  + " y = " + y);  
                    return true;
                }
                if (solveMazeUtil(maze,y - 1,sol)) {
                    //System.out.println("here at y-1 x = " + x  + " y = " + y);  
                    cost++;
                    return true;
                }
                cost--;
                /* If no solution then
                   BACKTRACK: unmark x,y as part of solution
                   path */
                sol[x][y] = 0;
                return false;
            }
        return false;
    }
}
最佳答案
将迷宫变成定向图并解决最短路径问题

>标记为“1”的单元格是节点(应标记为“0”)
>如果我们可以从一个单元(节点)移动到另一个单元(节点),请使用有向边连接它们;将成本(0或1)放在边缘(请注意,我们有一个多图:两个节点可以连接两个不同的边).
>最后,在Dijkstra’s algorithm的帮助下,找到所需节点之间的最短路径(即最小成本).

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读