原题链接在这里: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. Visual Studio 2019(VS2019)下载极慢的问题

    今天给新电脑安装 vs2019,下载速度只有 TMD 10kb/s,实在忍无可忍,在经过了一系列的百度之后,找到解决方法. 具体方法很简单: 用站长工具查询了一下 download.visualstu ...

  2. ffmpeg Operation not permitted 报错的解决过程记录

    问题重现 由于视频的录制过程出现了一些小问题,需要重新将视频文件切割和合并,找了几个视频编辑软件来做这个事情,最终的结果都不是特别满意,当时已经挺晚的了,本来打算上床睡觉第二天再去想辙,从椅子上起身的 ...

  3. HTML禁用Flash文件右键

    在项目中遇到一个需求,由于制作Flash的同事没有做禁用Flash文件右键功能!而Flash文件比较多,一个个改不太现实,于是要求用在网页显示的时候禁用Flash右键功能!未禁用之前! 禁用之前: 禁 ...

  4. docker 安装 apollo

    apollo作为携程开源的配置中心,很多大厂在使用,在此记录下安装历程 服务器环境: 安装mysql 1.拉取镜像 docker pull idoop/docker-apollo 2.新建3个数据库, ...

  5. fiddler抓包-2-5分钟学会手机端抓包

    前言 小伙伴们在前篇是否学会了简单的电脑web或其它软件抓包了呢?今天小编给大家带来的是fiddler设置手机代理抓手机端的数据包. 大致流程如下: 1.准备一台wifi功能正常的真机或虚拟机:2.设 ...

  6. HTTP之URL的快捷方式

    URL快捷方式 ==================摘自<HTTP权威指南>======================= WEB客户端可以理解并使用几种URL快捷方式.相对URL是在某职 ...

  7. 中文情感分析——snownlp类库 源码注释及使用

    最近发现了snownlp这个库,这个类库是专门针对中文文本进行文本挖掘的. 主要功能: 中文分词(Character-Based Generative Model) 词性标注(TnT 3-gram 隐 ...

  8. Jmeter参数化之数据库读取数据

    以读取mysql数据库为例 1.下载一个mysql驱动包,最好去mysql官网下载 下载网址:https://dev.mysql.com/downloads/connector/j/ Select O ...

  9. java 中遍历Map的几种方法

    方法分为两类: 一类是基于map的Entry:map.entrySet(); 一类是基于map的key:map.keySet() 而每一类都有两种遍历方式: a.利用迭代器 iterator: b.利 ...

  10. GET请求的请求参数最大长度

    在HTTP规范RFC-2616中有这样一段描述: The HTTP protocol does not place any a priori limit on the length of a URI. ...