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:

  1. Input: [[1,0,0,0],[0,0,0,0],[0,0,2,-1]]
  2. Output: 2
  3. Explanation: We have the following two paths:
  4. 1. (0,0),(0,1),(0,2),(0,3),(1,3),(1,2),(1,1),(1,0),(2,0),(2,1),(2,2)
  5. 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:

  1. Input: [[1,0,0,0],[0,0,0,0],[0,0,0,2]]
  2. Output: 4
  3. Explanation: We have the following four paths:
  4. 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)
  5. 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)
  6. 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)
  7. 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:

  1. Input: [[0,1],[2,0]]
  2. Output: 0
  3. Explanation:
  4. There is no path that walks over every empty square exactly once.
  5. Note that the starting and ending square can be anywhere in the grid.

Note:

  1. 1 <= grid.length * grid[0].length <= 20

Runtime: 0 ms, faster than 100.00% of C++ online submissions for Unique Paths III.

  1. //
  2. // Created by yuxi on 2019/1/21.
  3. //
  4.  
  5. #include <vector>
  6. #include <iostream>
  7. using namespace std;
  8.  
  9. class Solution {
  10. public:
  11. int cntzero;
  12. int ret;
  13. vector<vector<int>> dirs = {{,},{,-},{-,},{,}};
  14. int uniquePathsIII(vector<vector<int>>& grid) {
  15. vector<vector<int>> records(, vector<int>(,));
  16. ret = ;
  17. cntzero = ;
  18. for(int i=; i<grid.size(); i++) {
  19. for(int j=; j < grid[].size(); j++) {
  20. if(grid[i][j] == ) {
  21. records[][] = i;
  22. records[][] = j;
  23. } else if(grid[i][j] == ){
  24. records[][] = i;
  25. records[][] = j;
  26. } else if(grid[i][j] == ) cntzero++;
  27. }
  28. }
  29. int cnt = ;
  30. vector<bool> used(grid.size()*grid[].size(), false);
  31. vector<vector<int>> path;
  32. helper(grid, path, records[], records[], cnt, used);
  33. //cout << ret << endl;
  34. return ret;
  35. }
  36. void helper(vector<vector<int>>& grid, vector<vector<int>>& path, vector<int> s, vector<int>& e, int cnt, vector<bool>& used) {
  37. // for(int i=0; i<path.size(); i++) {
  38. // cout << "("<< path[i][0] << " " << path[i][1] << ")" << " ";
  39. // }
  40. //printgird(grid);
  41. int N = grid.size(), M = grid[].size();
  42. if(s[] == e[] && s[] == e[]) {
  43. // cout << "(" << s[0] << " " << s[1] << ")" << " " << endl;
  44. if(cnt == cntzero) ret++;
  45. return;
  46. }
  47. // cout << endl;
  48. // used[s[0]*N+s[1]] = true;
  49. grid[s[]][s[]] = -;
  50. path.push_back({s[],s[]});
  51. for(auto& dir : dirs) {
  52. int newx = dir[] + s[], newy = dir[] + s[];
  53. if(newx >= && newx < N && newy >= && newy < M && grid[newx][newy] != - && grid[newx][newy] != && grid[newx][newy] != -) {
  54. int newcnt = cnt;
  55. if(grid[newx][newy] == ) newcnt++;
  56. helper(grid, path, {newx, newy}, e, newcnt, used);
  57. }
  58. }
  59. grid[s[]][s[]] = ;
  60. // used[s[0]*N+s[1]] = false;
  61. path.pop_back();
  62. }
  63.  
  64. void printgird(vector<vector<int>>& grid) {
  65. int N = grid.size(), M = grid[].size();
  66. for(int i=; i<N; i++) {
  67. for(int j=; j<M; j++) {
  68. cout << grid[i][j] << " ";
  69. }
  70. cout << endl;
  71. }
  72. }
  73. };

LC 980. Unique Paths III的更多相关文章

  1. 原题链接在这里:980. Unique Paths III

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

  2. leetcode 980. Unique Paths III

    On a 2-dimensional grid, there are 4 types of squares: 1 represents the starting square.  There is e ...

  3. 【leetcode】980. Unique Paths III

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

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

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

  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. [LC] 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 ...

  8. [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 ...

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

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

随机推荐

  1. 2.3 使用 dom4j 对 xml文件进行 dom 解析

    // 使用dom4j对XML文档进行解析 CRUD public class Demo1 { //读取XML文档中第二本书的书名 <书名>javaWEB</书名> @Test ...

  2. instanceof解析

    https://www.zhihu.com/question/21574535/answer/18998914 Java instanceof 关键字是如何实现的? 基本理解 只是在同一个类加载器加载 ...

  3. Tomcat - Tomcat安装

    Tomcat官网:http://tomcat.apache.org/ 准备:JAVA环境布置完成 一.Windows平台 1. 版本选择 1) 进入官网 2) 查看版本匹配 官网说明 https:// ...

  4. SqlMetaData异常 dbType xx 对于此构造函数无效。

    今天在dapper中想扩展使用表值类型参数——tableValue.但是dapper不支持此类参数,于是扩展了一下.其中出现了一个问题. Microsoft.SqlServer.Server.SqlM ...

  5. C# 接口的作用浅谈举例(转)

    转:http://blog.csdn.net/liuqinghui1990/article/details/77171051 我初次接触接口(Interface),对接口的作用有点迷茫,C#接口中包含 ...

  6. 关于WebMvcConfigurationSupport的大坑-静态资源访问不了

    WebMvcConfigurationSupport是spring boot2.0以后用来替代WebMvcConfigurerAdapter,但是如果你直接用WebMvcConfigurationSu ...

  7. 用JS将毫秒转化成天时分秒的时间格式

    function formatDuring(mss) { var days = parseInt(mss / (1000 * 60 * 60 * 24)); var hours = parseInt( ...

  8. Vue-项目重要配置

    Vue配置axios ''' 1)安装插件(一定要在项目目录下): >: cnpm install axios 2)在main.js中配置: import axios from 'axios' ...

  9. [MySQL优化] -- 如何定位效率较低的SQL

    一般通过以下两种方式定位执行效率较低的 SQL 语句. 通过慢查询日志定位那些执行效率较低的 SQL 语句,用 --log-slow-queries[=file_name] 选项启动时, mysqld ...

  10. MyBatis中#{}和${}的不同和${}的妙用(转)

        突然意识到sql语句的独特语义要和代码分离,我们就不能够在代码中写sql语句!!比如我要用${}在MyBatis的sql中拼接排序类型的时候,我就不能够在Java代码中直接写参数字符串为Ord ...