leetcode 980. 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
思路:深搜, 终止条件到达目标位置,以及可到达的位置全部走了一遍,算一条路径。
 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的更多相关文章
- LC 980. Unique Paths III
		
On a 2-dimensional grid, there are 4 types of squares: 1 represents the starting square. There is e ...
 - 原题链接在这里:980. Unique Paths III
		
原题链接在这里:https://leetcode.com/problems/unique-paths-iii/ 题目: On a 2-dimensional grid, there are 4 typ ...
 - 【LeetCode】980. Unique Paths III解题报告(C++)
		
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...
 - 【leetcode】980. Unique Paths III
		
题目如下: On a 2-dimensional grid, there are 4 types of squares: 1 represents the starting square. Ther ...
 - 980. Unique Paths III
		
题目来源: https://leetcode.com/problems/unique-paths-iii/ 自我感觉难度/真实难度: 题意: 分析: 回溯法,直接DFS就可以了 自己的代码: clas ...
 - Leetcode之深度优先搜索&回溯专题-980. 不同路径 III(Unique Paths III)
		
Leetcode之深度优先搜索&回溯专题-980. 不同路径 III(Unique Paths III) 深度优先搜索的解题详细介绍,点击 在二维网格 grid 上,有 4 种类型的方格: 1 ...
 - [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 ...
 - 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). ...
 - [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 ...
 
随机推荐
- JS框架_(JQuery.js)带阴影贴纸标签按钮
			
百度云盘 传送门 密码:azo6 纯CSS带阴影贴纸标签按钮效果: <!doctype html> <html> <head> <meta charset=& ...
 - 完美解决前端跨域之 easyXDM 的使用和解析
			
前端跨域问题在大型网站中是比较常见的问题.本文详细介绍了利用 easyXDM 解决前端跨域的原理细节和使用细节,具体使用时可以在文中代码实例的基础上扩展完成. 0.背景 因个别网络运营商存在 HTTP ...
 - 为EasyUI的dataGrid单元格增加鼠标移入移出事件
			
onLoadSuccess: function (data) { $(".datagrid-row").mouseover(function (e) { var text = $( ...
 - 类组件(Class component)和函数式组件(Functional component)之间有何不同
			
类组件不仅允许你使用更多额外的功能,如组件自身的状态和生命周期钩子,也能使组件直接访问 store 并维持状态当组件仅是接收 props,并将组件自身渲染到页面时,该组件就是一个 ‘无状态组件(sta ...
 - 一、基础篇--1.1Java基础-Session和Cookie的区别【转】
			
https://www.cnblogs.com/zlw-xf/p/8001383.html 1:cookie数据存放在客户的浏览器上(客户端),session数据放 @1:cookie不是很安全,别人 ...
 - DB2基础维护手册
			
诊断DB2系统性能:db2top -d DEMODB db2top详解:http://blog.sina.com.cn/s/blog_636d62310102v7lm.html
 - P5436 【XR-2】缘分
			
P5436 [XR-2]缘分 题解 很显然给出一个n,要想使缘分最大,一定要选 n 和 n-1 对吧 但是这里有一个特盘,当 n=1 时,缘分应该为1 而不是0 代码 #include<bits ...
 - Python之标示符和关键字
			
<1>标示符 开发人员在程序中自定义的一些符号和名称 标示符是自己定义的,如变量名 .函数名等 <2>标示符的规则 标示符由字母.下划线和数字组成,且数字不能开头 python ...
 - BCNF/3NF 数据库设计范式简介
			
数据库设计有1NF.2NF.3NF.BCNF.4NF.5NF.从左往右,越后面的数据库设计范式冗余度越低. 满足后一个设计范式也必定满足前一个设计范式. 1NF只要求每个属性是不可再分的,基本每个数据 ...
 - iOS 图表工具charts之LineChartView
			
关于charts的系列视图介绍传送门: iOS 图表工具charts介绍 iOS 图表工具charts之LineChartView iOS 图表工具charts之BarChartView iOS 图表 ...