ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2019-12-16 09:00:08  阅读:323  来源: 互联网

标签:count Paths square 原题 int 980 length grid visited


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

标签:count,Paths,square,原题,int,980,length,grid,visited
来源: https://www.cnblogs.com/Dylan-Java-NYC/p/12047339.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有