原题链接在这里: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.

原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/12047339.html

时间: 2024-08-06 11:59:27

原题链接在这里:980. Unique Paths III的相关文章

LC 980. 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 repre

980. 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 repre

LeetCode开心刷题二十九天——63. Unique Paths II**

63. Unique Paths II Medium 938145FavoriteShare A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The robot can only move either down or right at any point in time. The robot is trying to reach the bottom

Leetcode之深度优先搜索&amp;回溯专题-980. 不同路径 III(Unique Paths III)

深度优先搜索的解题详细介绍,点击 在二维网格 grid 上,有 4 种类型的方格: 1 表示起始方格.且只有一个起始方格. 2 表示结束方格,且只有一个结束方格. 0 表示我们可以走过的空方格. -1 表示我们无法跨越的障碍. 返回在四个方向(上.下.左.右)上行走时,从起始方格到结束方格的不同路径的数目,每一个无障碍方格都要通过一次. 示例 1: 输入:[[1,0,0,0],[0,0,0,0],[0,0,2,-1]] 输出:2 解释:我们有以下两条路径: 1. (0,0),(0,1),(0,2

[Swift Weekly Contest 120]LeetCode980. 不同路径 III | 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 repre

LeetCode——Unique Paths

A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in t

LeetCode——Unique Paths II

Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle and empty space is marked as 1 and 0 respectively in the grid. For example, There is one obstacle in the middl

LeetCode: 63. Unique Paths II(Medium)

1. 原题链接 https://leetcode.com/problems/unique-paths-ii/description/ 原文地址:https://www.cnblogs.com/huiAlex/p/8437069.html

#LOJ2564 SDOI2018 原题识别 主席树

转载请注明原文地址:http://www.cnblogs.com/LadyLex/p/9057297.html 原题链接: 今天考试考了前天的SDOI考题 天啊我菜爆,只有T2拿了30分 然后考试后半程一直在打T1 觉得考试思路很有意思,于是就顺着打下来了 个人感觉这个是$O(nlogn^{2})$的,但是在loj上我比claris的程序快了1s多,只不过编程复杂度不止翻倍啊…… 下面介绍一下我的解法 其实最早启发我的是链上的部分分 定义$pre_{i}$为i前面最近的和i同色的点的下标,我们把