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. DS博客作业--课程总结

    1.当初你是如何做出选择计算机专业的决定的? 经过一年学习,你的看法改变了么,为什么? 你觉得计算机是你喜欢的领域吗,它是你擅长的领域吗? 为什么? 刚开始填报志愿的时候,因为我个人是没有什么比较特别 ...

  2. ES6 字符串的扩展(待细读)

    1.确定字符串中是否含有某个字符串 indexof(value,num):可返回某个指定的字符串值在字符串中首次出现的位置.ES5方法,num范围(0~length-1) includes(value ...

  3. react属性之exact

    exact是Route下的一个属性,react路由会匹配到所有能匹配到的路由组件,exact能够使得路由的匹配更严格一些. exact的值为bool型,为true时表示严格匹配,为false时为正常匹 ...

  4. ajaxform和ajaxgird中添加数据

    ajaxform添加数据 ajaxform.setRecord(response.getAjaxDataWrap("dataWrapBill").getData()); ajaxg ...

  5. PLSQL导出表的数据insert语句

    “Where clause”可以设置查询条件.设置好文件导出的路径(“Output file”),点击[Export]按钮,就可以导出INSERT语句了. 导出之后使用nodepad打开: 但是如果我 ...

  6. PowerDesigner 入门使用

    <转载于--https://www.cnblogs.com/biehongli/p/6025954.html> PowerDesigner最基础的使用方法入门学习   1:入门级使用Pow ...

  7. ffmpeg 视频过度滤镜 gltransition

    ffmpeg 视频过度滤镜 gltransition 上次随笔中提到的 ffmpeg-concat 可以处理视频过度,但是缺点是临时文件超大. 经过查找 ffmpeg 还有 gltransition ...

  8. WebSocket-Node

    WebSocket Client & Server Implementation for Node 参考资料:[https://github.com/theturtle32/WebSocket ...

  9. Fidder插件自动生成爬虫代码(C#)

    原创,效果如下: 1.新建项目,并添加Fidder.exe的引用: 2.添加代码 [assembly: Fiddler.RequiredVersion("2.2.8.6")]usi ...

  10. springboot-elasticsearch项目启动报错:'elasticsearchTemplate' that could not be found

    解决: 将elasticsearch的相关配置加入到application.yml配置文件中就可以解决