& Leetcode the backtracking depth-first search topic -980. Different paths III (Unique Paths III)

& Leetcode the backtracking depth-first search topic -980. Different paths III (Unique Paths III)

Problem-solving depth-first search details, click


 

In the two-dimensional grid  grid on, there are four types of grid:

  • 1 It represents the starting grid. And only one starting grid.
  • 2 It indicates the end of the box, and only one end of the square.
  • 0 It means that we can walk in the empty box.
  • -1 We represent insurmountable obstacles.

Back in four directions (up, down, left, right) when walking on the starting grid from the number of different paths to the end of squares, each square must be accessible by one.

 

Example 1:

Input: [[1,0,0,0], [0,0,0,0], [0,0,2, -1]] 
Output: 2 
Explanation: we have the following two paths: 
1. (0 , 0), (0,1), (0,2), (0,3), (1,3), (1,2), (1,1), (1,0), (2,0 ), (2,1), (2,2) 
2 (0,0), (1,0), (2,0), (2,1), (1,1), (0,1) , (0,2), (0,3), (1,3), (1,2), (2,2)

Example 2:

Input: [[1,0,0,0], [0,0,0,0], [0,0,0,2]] 
Output: 4 
Explanation: we have the following four paths: 
1. (0,0 ), (0,1), (0,2), (0,3), (1,3), (1,2), (1,1), (1,0), (2,0), (2,1), (2,2), (2,3) 
2 (0,0), (0,1), (1,1), (1,0), (2,0), ( 2,1), (2,2), (1,2), (0,2), (0,3), (1,3), (2,3) 
3 (0,0), (1 , 0), (2,0), (2,1), (2,2), (1,2), (1,1), (0,1), (0,2), (0,3 ), (1,3), (2,3) 
4 (0,0), (1,0), (2,0), (2,1), (1,1), (0,1) , (0,2), (0,3), (1,3), (1,2), (2,2), (2,3)

Example 3:

Input: [[0,1], [2,0]] 
Output: 0 
Explanation: 
no path is completely empty squares through each time. 
Note that the start and the end cell may be located anywhere in the grid.

 

prompt:

  1. 1 <= grid.length * grid[0].length <= 20

  


 

analysis:

DFS is the normal number of search paths, plus a final judgment of the number of steps is not equal to the number of non-obstacle on it.

 

AC Code:

class Solution {
    int count = 0;
    int ans =0;
    int dirx[] = {0,0,1,-1};
    int diry[] = {1,-1,0,0};
    public int uniquePathsIII(int[][] grid) {
        if(grid.length==0 || grid==null) return 0;
        
        for(int i=0;i<grid.length;i++){
            for(int j=0;j<grid[0].length;j++){
                if(grid[i][j]==0){
                    count++;
                }
            }
        }
        int[][] vis = new int[grid.length][grid[0].length];
        for(int i=0;i<grid.length;i++){
            for(int j=0;j<grid[0].length;j++){
                if(grid[i][j]==1){
                    dfs(grid,vis,i,j,0);
                }
            }
        }
        return ans;
    }
    public void dfs(int[][] grid,int[][] vis,int x,int y,int step){
        if(grid[x][y]==2){
            if(step-1==count){
                ans++;
            }
            return;
        }
        for(int i=0;i<4;i++){
            int xx = x + dirx[i];
            int yy = y + diry[i];
            if(xx>=0 && xx<grid.length && yy>=0 && yy<grid[0].length && (grid[xx][yy]==0 || grid[xx][yy]==2) &&vis[xx][yy]==0){
                vis[xx][yy]=1;
                dfs(grid,vis,xx,yy,step+1);
                vis[xx][yy]=0;
            }
        }
    }
}

 

Guess you like

Origin www.cnblogs.com/qinyuguan/p/11366961.html