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

 class Solution {
int pathCount = 0;
int [][] dirs = new int[][]{{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; public int uniquePathsIII(int[][] grid) {
if(grid == null || grid.length == 0){
return 0;
} int m = grid.length;
int n = grid[0].length;
int startX = -1;
int startY = -1;
int endX = -1;
int endY = -1;
int zeroCount = 0; for(int i = 0; i<m; i++){
for(int j = 0; j<n; j++){
if(grid[i][j] == 1){
startX = i;
startY = j;
}else if(grid[i][j] == 2){
endX = i;
endY = j;
}else if(grid[i][j] == 0){
zeroCount++;
}
}
} dfs(grid, startX, startY, endX, endY, 0, zeroCount, new boolean[m][n]);
return pathCount;
} private void dfs(int [][] grid, int i, int j, int endX, int endY, int count, int targetCount, boolean [][] visited){
if(i < 0 || i >= grid.length || j < 0 || j>= grid[0].length || grid[i][j] == -1 || visited[i][j]){
return;
} if(grid[i][j] == 2){
if(count == targetCount){
pathCount++;
} return;
} if(grid[i][j] == 0){
count++;
} visited[i][j] = true;
for(int [] dir : dirs){
int dx = i + dir[0];
int dy = j + dir[1];
dfs(grid, dx, dy, endX, endY, count, targetCount, visited);
} visited[i][j] = false;
}
}

类似Sudoku SolverUnique PathsUnique Paths II.

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

  1. LC 980. Unique Paths III

    On a 2-dimensional grid, there are 4 types of squares: 1 represents the starting square.  There is e ...

  2. 【LeetCode】980. Unique Paths III解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...

  3. 980. Unique Paths III

    题目来源: https://leetcode.com/problems/unique-paths-iii/ 自我感觉难度/真实难度: 题意: 分析: 回溯法,直接DFS就可以了 自己的代码: clas ...

  4. leetcode 980. Unique Paths III

    On a 2-dimensional grid, there are 4 types of squares: 1 represents the starting square.  There is e ...

  5. 【leetcode】980. Unique Paths III

    题目如下: On a 2-dimensional grid, there are 4 types of squares: 1 represents the starting square.  Ther ...

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

    Leetcode之深度优先搜索&回溯专题-980. 不同路径 III(Unique Paths III) 深度优先搜索的解题详细介绍,点击 在二维网格 grid 上,有 4 种类型的方格: 1 ...

  7. [Swift]LeetCode980. 不同路径 III | Unique Paths III

    On a 2-dimensional grid, there are 4 types of squares: 1 represents the starting square.  There is e ...

  8. [leetcode] 62 Unique Paths (Medium)

    原题链接 字母题 : unique paths Ⅱ 思路: dp[i][j]保存走到第i,j格共有几种走法. 因为只能走→或者↓,所以边界条件dp[0][j]+=dp[0][j-1] 同时容易得出递推 ...

  9. #LOJ2564 SDOI2018 原题识别 主席树

    转载请注明原文地址:http://www.cnblogs.com/LadyLex/p/9057297.html 原题链接: 今天考试考了前天的SDOI考题 天啊我菜爆,只有T2拿了30分 然后考试后半 ...

随机推荐

  1. Scala字符串插值 - StringContext

    翻译自:STRING INTERPOLATION 简介 自2.10.0版本开始,Scala提供了一种新的机制来根据数据生成字符串:字符串插值.字符串插值允许使用者将变量引用直接插入处理过的字面字符中. ...

  2. qps.sh

    mysql -p'' -Bse'show global status like "com_%";' > qps.new while true do sleep 0.5 mv ...

  3. java核心技术(第十版卷一)笔记(纯干货!)

    这是我读过的第三本关于java基础的书.第一本<<java从入门到精通>>这本书让我灵识初开.第二本<<java敏捷开发>>这本书则是有一次被一位师傅批 ...

  4. 关于stm32f10x_conf.h文件

    简介 stm32f10x_conf.h文件有2个作用:①提供对assert_param运行时参数检查宏函数的定义.②将开发者用到的标准外设头文件集中在这个文件里面,而stm32f10x_conf.h又 ...

  5. [转帖]龙芯3A4000处理器实测:28nm工艺不变 性能仍可提升100%以上

    龙芯3A4000处理器实测:28nm工艺不变 性能仍可提升100%以上 http://news.mydrivers.com/1/663/663122.htm 龙芯是中科院下属的计算机所研发的自主产权国 ...

  6. UML类图记忆口诀

    UML类图在设计模式书籍中用的比较多,经常忘记,口诀挺重要的,比如我们从小到大,除了乘法口诀.元素周期表等口诀形式的知识,其它的知识都基本忘记了, 所以编写口诀如下 1.三级石 2.见关一 3.零足迹 ...

  7. WPF 精修篇 长时间线程加取消功能

    原文:WPF 精修篇 长时间线程加取消功能 <Grid> <Grid.RowDefinitions> <RowDefinition Height="11*&qu ...

  8. C#实现高性能高并发Socket服务器

    1.高并发服务器实现一 本文转载 转载地址 2.高并发服务器实现二 本文转载 转载内容在于学习C#实现的高并发服务器 以下个人观点 1 需要注意SocketAsyncEventArgs的使用 2 做到 ...

  9. python2.7写的图形密码生成器

    #coding:utf8import random,wxdef password(event): a = [chr(i) for i in range(97,123)] b = [chr(i) for ...

  10. java包装类和值类型的关系

    java包装类总是让人疑惑 它与值类型到底是怎么样一种关系? 本文将以int和Integer为例来探讨它们的关系 java值类型有int short char boolean byte long fl ...