原题链接在这里: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 <= 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分 然后考试后半 ...
随机推荐
- ORA-12528: TNS:listener: all appropriate instances are blocking new connections
Oracle问题:ORA-12528: TNS: 监听程序: 所有适用例程都无法建立新连接 问题原始描述: ORA-12528: TNS:listener: all appropriate insta ...
- mybatis解决字段名和实体属性不相同
两种方法: 1.在xml文件里面使用别名 2.使用resultMap标签
- html5预加载图片的写法
插件还是用 jquery.imgpreload.min.js 只不过初始化加载 必须用window.onload 图片dom都加载完成再显示 不然会有bug <pre> window.on ...
- windows远程桌面无法拷贝文件的问题与解决方法
在开发完往windows服务器上部署系统或者给系统打补丁的时候,都会需要远程桌面的双向拷贝文件功能. 但是有些时候却会发现没有办法拷贝文件,原因主要有两个. 01 远程桌面的剪贴板设置 一个是在远程桌 ...
- Reactor的NIO线程模型
1.Reactor单线程模型 传统的javaNIO通信的线程模型.该线程模型仅有一个I/O线程处理所有的I/O操作,如下图: 单线程模型的Reactor 所有的客户端都连接到一个I/O线程负责的A ...
- .NET EF执行sql报数组超出了索引
使用ef查询,写sql语句的 一般情况报数组超出了索引都认为是[i]里面的值超出了,但是执行sql报超出了索引,让人很蒙 在网上找了半天也没有结果,后来只能自己来解决了. 在异常里面能看到dbnull ...
- 2019 上海轻轻java面试笔试题 (含面试题解析)
本人5年开发经验.18年年底开始跑路找工作,在互联网寒冬下成功拿到阿里巴巴.今日头条.上海轻轻等公司offer,岗位是Java后端开发,因为发展原因最终选择去了上海轻轻,入职一年时间了,也成为了面 ...
- 面试官:你知道Spring中有哪些可以让我们扩展的地方么
大家都知道我这段时间陆续更新了Spring系列源码分析以及各种扩展点的文章,到了今天可以总算可以更新这篇文章了 首先列举一下一个经典的面试题:Spring中Bean的生命周期: 开始初始化容器 加载B ...
- Spring扩展点之Aware接口族
引言 Spring中提供了各种Aware接口,方便从上下文中获取当前的运行环境,比较常见的几个子接口有:BeanFactoryAware,BeanNameAware,ApplicationContex ...
- Spring扩展点之BeanPostProcessor
前言 BeanPostProcessor接口是Spring中一个非常重要的接口,它的接口定义如下 public interface BeanPostProcessor { Object postPro ...