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)Tooltip弹出式按钮插件
			百度云盘 传送门 密码:7eh5 弹出式按钮效果 <!DOCTYPE html> <html > <head> <meta charset="UTF ... 
- [CSP-S模拟测试]:多维网格(组合数学+容斥)
			题目传送门(内部题138) 输入格式 输入数据第一行为两个整数$d,n$. 第二行$d$个非负整数$a_1,a_2,...,a_d$. 接下来$n$行,每行$d$个整数,表示一个坏点的坐标.数 ... 
- javascript学习笔记之DOM
			DOM(文档对象模型),描述了一个层次化的节点树 一.DOM NODE相关公共属性与方法 DOM中所有节点都实现了NODE接口,该接口的公共属性和方法如下: 1.节点基本属性 1)NodeType 节 ... 
- ltelliJ IDEA 创建Maven web项目无src目录的解决方案
			https://blog.csdn.net/xiaoke815/article/details/72810976 一.缘由 这几天闲来无事,突然想试试IDEA这个编译器,之前一直都在用Eclipse ... 
- js手写笔记
			1.document.write(); 2.document.getElementById("").style.color="red";//sytle.font ... 
- hibernate entitymanager的理解
			hibernate EntityManager是围绕提供JPA编程接口的Hibernate Core的一个包装,支持JPA实体实例的生命周期,并允许你用标准的JavaPersistence查询语言编写 ... 
- SSM三大框架整合配置详解
			首先,导入框架所需要的全部jar包(此处省略...........) 第一步:先从mybatis框架开始 我们只需要在mybatis的核心配置文件sqlConfigXml里写上这么一段话,代表的是给p ... 
- vue项目内嵌入到app input type=file 坑(文件上传插件)
			w问题描述: 我用vue-cli完成的一个移动端项目,内嵌到app当中,用原生的input type=file 来完成文件上传.在安卓下没有问题但是在苹果手机 上传第二次手机就会发生白屏 并无缘无故跳 ... 
- 191107Django的Cookie和Session
			Cookie的使用 from django.shortcuts import render,redirect def login(request): print("1",reque ... 
- Oracle  数据自动备份 通过EXP备份
			先写个批处理文件(.bat),具体如下:@echo off@echo ================================================@echo windows环境下 ... 
