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

思路:深搜, 终止条件到达目标位置,以及可到达的位置全部走了一遍,算一条路径。

 class Solution {
int dx[] = {, -, , };
int dy[] = {, , -, };
public:
int uniquePathsIII(vector<vector<int>>& grid) {
int m = grid.size();
if (m == )
return ;
int n = grid[].size();
int todo = ;
int start_x, start_y, end_x, end_y;
for (int i = ; i < m; i++) {
for (int j = ; j < n; j++) {
if (grid[i][j] != -) { //记录要走的总的位置数
todo++;
if (grid[i][j] == ) { //记录起始位置
start_x = i;
start_y = j;
} else if (grid[i][j] == ) { //记录终点
end_x = i;
end_y = j;
}
}
}
}
int ans = ;
dfs(grid, start_x, start_y, end_x, end_y, todo, ans, m, n);
return ans;
}
void dfs(vector<vector<int> > &grid, int sx, int sy, const int ex, const int ey, int todo, int &ans, int row, int col) {
todo--;
if (todo < )
return ;
if (sx == ex && sy == ey) {
if (todo == ) ans++;
return;
}
//上下左右四个方向
for (int k = ; k < ; k++) {
int new_x = sx + dx[k];
int new_y = sy + dy[k];
if (new_x >= && new_x < row && new_y >= && new_y < col) {
if (grid[new_x][new_y] == || grid[new_x][new_y] == ) {
grid[new_x][new_y] = -;
dfs(grid, new_x, new_y, ex, ey, todo, ans, row, col);
grid[new_x][new_y] = ;
}
}
}
}
};

leetcode 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. 原题链接在这里:980. Unique Paths III

    原题链接在这里:https://leetcode.com/problems/unique-paths-iii/ 题目: On a 2-dimensional grid, there are 4 typ ...

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

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

  4. 【leetcode】980. Unique Paths III

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

  5. 980. Unique Paths III

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

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

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

  7. [LeetCode] 63. Unique Paths II 不同的路径之二

    A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...

  8. LeetCode 63. Unique Paths II不同路径 II (C++/Java)

    题目: A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). ...

  9. [LeetCode] 62. Unique Paths 唯一路径

    A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...

随机推荐

  1. linux crontab 定时任务,任务命令单独linux执行正常,放定时任务就不执行了,解决办法 (原)

    这是我crontab里面的内容 */30 * * * *  ./usr/bin/wget -q -O sync_log.txt http://fly.dllm.cn/index.php/Home/In ...

  2. R_Studio读取xls文件

    百度经验 传送门 需要包xlsx 依赖包rjava 需要安装java编译环境 在R Console中执行命令install.packages("rjava"),install.pa ...

  3. iOS检测QQ是否安装

    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"mqq://"]]) {     ...

  4. EL表达式里面不能直接使用list.size()得到长度,

    在jsp页面中不能通过${list.size}取列表长度, 而是 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" pre ...

  5. C++入门经典-例2.8-输出整数,控制打印格式

    1:代码如下: // 2.8.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> #incl ...

  6. HTML控件 隐藏

    div的visibility可以控制div的显示和隐藏,但是隐藏后页面显示空白: style="visibility: none;" document.getElementById ...

  7. 搭建jenkins集群node结点

    配置结点 首先,需要在jenkins的系统设置中新增一个结点 系统管理 -> 节点管理 -> New Node 配置Remote root directory最好和主jenkins的路径一 ...

  8. LoadRunner 技巧之 脚本设计

    LoadRunner 技巧之 脚本设计 在做性能测试时,我们可能会遇到各种不同的业务需求与用户行为,在一个系统或网站中,每个用户的操作都不完全一样.我们如何来模拟这此用户的行为?经验与能力有限,我这里 ...

  9. 4.1 primitive and reference values

    ECMAScript variables may contains two different types of data: primitive values and reference values ...

  10. IDEA和VS快捷键对比

    IDEA和Visual Studio快捷键对比 VS     F5 F9            resume programe 恢复程序     Alt+F10       show executio ...