原题链接在这里:980. Unique Paths III

原题链接在这里:https://leetcode.com/problems/unique-paths-iii/

题目:

On a 2-dimensional grid, there are 4 types of squares:

  • 1 represents the starting square.  There is exactly one starting square.
  • 2 represents the ending square.  There is exactly one ending square.
  • 0 represents empty squares we can walk over.
  • -1 represents obstacles that we cannot walk over.

Return the number of 4-directional walks from the starting square to the ending square, that walk over every non-obstacle square exactly once.

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: 
There is no path that walks over every empty square exactly once.
Note that the starting and ending square can be anywhere in the grid.

Note:

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

题解:

The DFS states need current coordinate, target coordinate, current count of 0 position, target count of 0 position, and visited grid.

If current coordinate is out of bound, or its value is -1 or it is visited before, simply return.

If it is current coordinate is target coordinate, if current 0 count == target count, we find a path. Whether we this is a path, we need to return here.

It its value is 0, accumlate 0 count.

Mark this position as visited and for 4 dirs, continue DFS.

Backtracking needs to reset visited as false at this coordinate.

Time Complexity: exponential.

Space: O(m*n). m = grid.length. n = grid[0].length.

AC Java:

 1 class Solution {
 2     int pathCount = 0;
 3     int [][] dirs = new int[][]{{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
 4     
 5     public int uniquePathsIII(int[][] grid) {
 6         if(grid == null || grid.length == 0){
 7             return 0;
 8         }
 9         
10         int m = grid.length;
11         int n = grid[0].length;
12         int startX = -1;
13         int startY = -1;
14         int endX = -1;
15         int endY = -1;
16         int zeroCount = 0;
17         
18         for(int i = 0; i<m; i++){
19             for(int j = 0; j<n; j++){
20                 if(grid[i][j] == 1){
21                     startX = i;
22                     startY = j;
23                 }else if(grid[i][j] == 2){
24                     endX = i;
25                     endY = j;
26                 }else if(grid[i][j] == 0){
27                     zeroCount++;
28                 }
29             }
30         }
31         
32         dfs(grid, startX, startY, endX, endY, 0, zeroCount, new boolean[m][n]);
33         return pathCount;
34     }
35     
36     private void dfs(int [][] grid, int i, int j, int endX, int endY, int count, int targetCount, boolean [][] visited){
37         if(i < 0 || i >= grid.length || j < 0 || j>= grid[0].length || grid[i][j] == -1 || visited[i][j]){
38             return;
39         }
40         
41         if(grid[i][j] == 2){
42             if(count == targetCount){
43                 pathCount++;
44             }
45             
46             return;
47         }
48         
49         if(grid[i][j] == 0){
50             count++;
51         }
52         
53         visited[i][j] = true;
54         for(int [] dir : dirs){
55             int dx = i + dir[0];
56             int dy = j + dir[1];
57             dfs(grid, dx, dy, endX, endY, count, targetCount, visited);
58         }
59         
60         visited[i][j] = false;
61     }
62 }

类似Sudoku SolverUnique PathsUnique Paths II.

上一篇:Codeforces 1294F Three Paths on a Tree


下一篇:【leetcode】1301. Number of Paths with Max Score