原题链接在这里:980. Unique Paths III
原题链接在这里:https://leetcode.com/problems/unique-paths-iii/
题目:
On a 2-dimensional grid, there are 4 types of squares:
1represents the starting square. There is exactly one starting square.2represents the ending square. There is exactly one ending square.0represents empty squares we can walk over.-1represents 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 <= 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 Solver, Unique Paths, Unique Paths II.
原题链接在这里: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 e ...
- 【LeetCode】980. Unique Paths III解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...
- 980. Unique Paths III
题目来源: https://leetcode.com/problems/unique-paths-iii/ 自我感觉难度/真实难度: 题意: 分析: 回溯法,直接DFS就可以了 自己的代码: clas ...
- leetcode 980. Unique Paths III
On a 2-dimensional grid, there are 4 types of squares: 1 represents the starting square. There is e ...
- 【leetcode】980. Unique Paths III
题目如下: On a 2-dimensional grid, there are 4 types of squares: 1 represents the starting square. Ther ...
- Leetcode之深度优先搜索&回溯专题-980. 不同路径 III(Unique Paths III)
Leetcode之深度优先搜索&回溯专题-980. 不同路径 III(Unique Paths III) 深度优先搜索的解题详细介绍,点击 在二维网格 grid 上,有 4 种类型的方格: 1 ...
- [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 ...
- [leetcode] 62 Unique Paths (Medium)
原题链接 字母题 : unique paths Ⅱ 思路: dp[i][j]保存走到第i,j格共有几种走法. 因为只能走→或者↓,所以边界条件dp[0][j]+=dp[0][j-1] 同时容易得出递推 ...
- #LOJ2564 SDOI2018 原题识别 主席树
转载请注明原文地址:http://www.cnblogs.com/LadyLex/p/9057297.html 原题链接: 今天考试考了前天的SDOI考题 天啊我菜爆,只有T2拿了30分 然后考试后半 ...
随机推荐
- CentOS7 配置ip地址
1.查看网络配置文件 可能因为不同的系统,配置文件都不一样,总体来说,配置文件都是 前缀:ifcfg-eno + 后缀:随机数组成 进入到 /etc/sysconfig/network-scripts ...
- sql server 2019 & spark
https://cloudblogs.microsoft.com/sqlserver/2019/04/01/how-to-develop-and-submit-spark-jobs-to-sql-se ...
- XC7K325TFFG900 Device 内部结构图
- oracle批量新增更新数据
本博客介绍一下Oracle批量新增数据和更新数据的sql写法,业务场景是这样的,往一张关联表里批量新增更新数据,然后,下面介绍一下批量新增和更新的写法: 批量新增数据 对于批量新增数据,介绍两种方法 ...
- Ansible17:Playbook之tags
目录 简介 为task打tag 使用tag 执行指定tag的task 排除指定tag的task 查看playbook中的所有tag 打tag的几种方式 ansible内置tag 简介 在大型项目当中, ...
- 机器学习之TensorFlow介绍
TensorFlow的概念很简单:使用python定义一个计算图,然后TensorFlow根据计算图生成高性能的c++代码. 如上图所示,使用图的方式实现了函数\(f(x,y)=x^2y+y+2\)的 ...
- Jenkins生成APK链接的二维码
Window环境 1.下载安装Python如3.7.5版本,安装Python到电脑上如C:\Python37,将C:\Python37,C:\Python37\Scripts添加到Path环境变量中 ...
- zbar android sdk在CentOS 7下的编译和使用
环境:CentOS 7+NDK 20.0.5594570+libiconv-1.14 下载 下载libiconv-1.14版本,地址: https://ftp.gnu.org/pub/gnu/libi ...
- Flask send_file request
send_file: send_file( filename_or_fp, mimetype=None, as_attachment=False, attachment_filename=None, ...
- 转:Windows系统环境下安装dlib
原文链接 因为今天安装Face Recognition,需要先按照 dlib .需要在windows环境下做一些图片处理,所以需要在pycharm中配置环境,而其中需要的主要是dlib的安装: 下面说 ...