zoukankan      html  css  js  c++  java
  • [GeeksForGeeks] Rat in a Maze

    A Maze is given as N*N binary matrix of blocks where source block is the upper left most block i.e., maze[0][0] and destination block is lower rightmost block i.e., maze[N-1][N-1]. A rat starts from source and has to reach destination. The rat can move only in two directions: forward and down.
    In the maze matrix, 0 means the block is dead end and 1 means the block can be used in the path from source to destination. Note that this is a simple version of the typical Maze problem. For example, a more complex version can be that the rat can move in 4 directions and a more complex version can be with limited number of moves.

    Following is an example maze.

     Gray blocks are dead ends (value = 0). 

    Following is binary matrix representation of the above maze.

                    {1, 0, 0, 0}
                    {1, 1, 0, 1}
                    {0, 1, 0, 0}
                    {1, 1, 1, 1}
    

    Following is maze with highlighted solution path.

    Following is the solution matrix (output of program) for the above input matrx.

                    {1, 0, 0, 0}
                    {1, 1, 0, 0}
                    {0, 1, 0, 0}
                    {0, 1, 1, 1}
     All enteries in solution path are marked as 1.


    Solution. DFS Backtracking
     1 import java.util.ArrayList;
     2 public class Solution {
     3     class Point{
     4         int x;
     5         int y;
     6         Point(int x, int y){
     7             this.x = x;
     8             this.y = y;
     9         }
    10         void display(){
    11             System.out.println(Integer.toString(x) + "," + Integer.toString(y));
    12         }
    13     }
    14     public boolean findPath(int[][] maze, ArrayList<Point> path){
    15         if(maze == null || maze.length == 0 || maze[0].length == 0){
    16             return false;
    17         }
    18         int row = maze.length; int col = maze[0].length;
    19         if(maze[0][0] == 0 || maze[row - 1][col - 1] == 0){
    20             return false;
    21         }
    22         return findPathRecursive(maze, path, 0, 0);
    23     }
    24     private boolean findPathRecursive(int[][] maze, ArrayList<Point> path,int x, int y){
    25         if(!isInBound(maze, x, y) || maze[x][y] == 0){
    26             return false;
    27         }
    28         path.add(new Point(x, y));
    29         if(x == maze.length - 1 && y == maze.length - 1){
    30             return true;
    31         }
    32         if(findPathRecursive(maze, path, x, y + 1)){
    33             return true;
    34         }
    35         if(findPathRecursive(maze, path, x + 1, y)){
    36             return true;
    37         }
    38         path.remove(path.size() - 1);
    39         return false;
    40     }
    41     private boolean isInBound(int[][] maze, int x, int y){
    42         return x >= 0 && x < maze.length && y >= 0 && y < maze[0].length;
    43     }
    44     public static void main(String[] args){
    45         int[][] maze1 = {{1,0,0,0}, {1,1,0,1},{0,1,0,0},{1,1,1,1}};
    46         Solution sol = new Solution();
    47         ArrayList<Point> path = new ArrayList<Point>();
    48         sol.findPath(maze1, path);
    49         for(Point p: path){
    50             p.display();
    51         }
    52     }
    53 }

    Follow up question: 

    How many unique paths does this rat have to reach from source to destination? 

    This question is essentially identical with Unique Paths II.

    Solution 1. Recursion

    The rat can only move either downward or right, one grid at a time.  Assume the start point is (x, y), then 

    this problem can be solved by first solving the following 2 subproblems.

    1. find the number of all unique paths from (x + 1, y) to destination

    2. find the number of all unique paths from (x, y + 1) to destination

    However, this solution is not effiecient as it does a lot of duplicated work.  For a given grid(x, y), it can be reached 

    either via (x - 1, y) or (x, y - 1). Both route calculates the number of all unique paths from (x, y) to the destination.

    This is clearly redundant work. To avoid this inefficiency, we should use dynamic programming.

     1 public class Solution {
     2     private int totalPaths = 0;
     3     public int uniquePathsWithObstacles(int[][] obstacleGrid) {
     4         if(obstacleGrid == null || obstacleGrid.length == 0 || obstacleGrid[0].length == 0){
     5             return 0;
     6         }
     7         findPaths(obstacleGrid, 0, 0);
     8         return totalPaths;
     9     }
    10     private void findPaths(int[][] grid, int x, int y){
    11         if(!isInBound(grid, x, y) || grid[x][y] == 1){
    12             return;
    13         }
    14         if(x == grid.length - 1 && y == grid[0].length - 1){
    15             totalPaths++;
    16             return;
    17         }
    18         findPaths(grid, x + 1, y);
    19         findPaths(grid, x, y + 1);
    20     }
    21     private boolean isInBound(int[][] grid, int x, int y){
    22         return x >= 0 && x < grid.length && y >= 0 && y < grid[0].length;    
    23     }
    24 }

    Solution 2. Dynamic Programming, O(n^2) runtime, O(n^2) space

    State: T[j][k] is the total number of unique paths from (0,0) to (j, k)

    Function: T[j][k] = 0, if obstacleGrid[j][k] == 1

         T[j][k] = T[j - 1][k] + T[j][k - 1], if obstacleGrid[j][k] == 0

     1 public class Solution {
     2     public int uniquePathsWithObstacles(int[][] obstacleGrid) {
     3         if(obstacleGrid == null || obstacleGrid.length == 0 || obstacleGrid[0].length == 0){
     4             return 0;
     5         }
     6         int[][] T = new int[obstacleGrid.length][obstacleGrid[0].length];
     7         int i = 0;
     8         for(; i < obstacleGrid[0].length; i++){
     9             if(obstacleGrid[0][i] == 1){
    10                 break;
    11             }
    12             T[0][i] = 1;
    13         }
    14         for(; i < obstacleGrid[0].length; i++){
    15             T[0][i] = 0;
    16         }
    17         i = 0;
    18         for(; i < obstacleGrid.length; i++){
    19             if(obstacleGrid[i][0] == 1){
    20                 break;
    21             }
    22             T[i][0] = 1;
    23         }
    24         for(; i < obstacleGrid.length; i++){
    25             T[i][0] = 0;
    26         }
    27         for(int j = 1; j < T.length; j++){
    28             for(int k = 1; k < T[0].length; k++){
    29                 if(obstacleGrid[j][k] == 1){
    30                     T[j][k] = 0;
    31                 }
    32                 else{
    33                     T[j][k] = T[j - 1][k] + T[j][k - 1];
    34                 }
    35             }
    36         }
    37         return T[T.length - 1][T[0].length - 1];
    38     }
    39 }

    Solution 3. Dynamic Programming with optimzied space usage, O(n) space

     1 public class Solution {
     2     public int uniquePathsWithObstacles(int[][] obstacleGrid) {
     3         if(obstacleGrid == null || obstacleGrid.length == 0 || obstacleGrid[0].length == 0){
     4             return 0;
     5         }
     6         int[][] T = new int[2][obstacleGrid[0].length];
     7         int i = 0;
     8         for(; i < obstacleGrid[0].length; i++){
     9             if(obstacleGrid[0][i] == 1){
    10                 break;
    11             }
    12             T[0][i] = 1;
    13         }
    14         for(; i < obstacleGrid[0].length; i++){
    15             T[0][i] = 0;
    16         }
    17         boolean reachable = (obstacleGrid[0][0] == 0 ? true : false);
    18         for(int j = 1; j < obstacleGrid.length; j++){
    19             if(reachable && obstacleGrid[j][0] == 0){
    20                 T[j % 2][0] = 1;
    21             }
    22             else{
    23                 reachable = false;
    24                 T[j % 2][0] = 0;
    25             }
    26             for(int k = 1; k < obstacleGrid[0].length; k++){
    27                 if(obstacleGrid[j][k] == 1){
    28                     T[j % 2][k] = 0;
    29                 }
    30                 else{
    31                     T[j % 2][k] = T[(j - 1) % 2][k] + T[j % 2][k - 1];
    32                 }
    33             }
    34         }
    35         return T[(obstacleGrid.length - 1) % 2][obstacleGrid[0].length - 1];
    36     }
    37 }

    Related Problems

    Unique Paths

    Unique Paths II

  • 相关阅读:
    做汉堡
    结对运算(14曾柏树,15张奇聪)小学生四则运算总结体会
    一个能自动生成小学四则运算题目的程序
    css样式实现立方体制作
    bootstrap导航条+模态对话框+分页样式
    bootstrap 表单+按钮+对话框
    百度地图控件
    百度地图覆盖物0.1版本
    HTML5 javascript 音乐 音频
    html5 javascript 小型计算器
  • 原文地址:https://www.cnblogs.com/lz87/p/7266534.html
Copyright © 2011-2022 走看看