LeetCode 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, current count of 0 position, target count of 0 position, and visited grid.

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

类似Sudoku SolverUnique PathsUnique Paths II.

posted @ 2019-12-16 08:59  Dylan_Java_NYC  阅读(215)  评论(0编辑  收藏  举报