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

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

//
// Created by yuxi on 2019/1/21.
// #include <vector>
#include <iostream>
using namespace std; class Solution {
public:
int cntzero;
int ret;
vector<vector<int>> dirs = {{,},{,-},{-,},{,}};
int uniquePathsIII(vector<vector<int>>& grid) {
vector<vector<int>> records(, vector<int>(,));
ret = ;
cntzero = ;
for(int i=; i<grid.size(); i++) {
for(int j=; j < grid[].size(); j++) {
if(grid[i][j] == ) {
records[][] = i;
records[][] = j;
} else if(grid[i][j] == ){
records[][] = i;
records[][] = j;
} else if(grid[i][j] == ) cntzero++;
}
}
int cnt = ;
vector<bool> used(grid.size()*grid[].size(), false);
vector<vector<int>> path;
helper(grid, path, records[], records[], cnt, used);
//cout << ret << endl;
return ret;
}
void helper(vector<vector<int>>& grid, vector<vector<int>>& path, vector<int> s, vector<int>& e, int cnt, vector<bool>& used) {
// for(int i=0; i<path.size(); i++) {
// cout << "("<< path[i][0] << " " << path[i][1] << ")" << " ";
// }
//printgird(grid);
int N = grid.size(), M = grid[].size();
if(s[] == e[] && s[] == e[]) {
// cout << "(" << s[0] << " " << s[1] << ")" << " " << endl;
if(cnt == cntzero) ret++;
return;
}
// cout << endl;
// used[s[0]*N+s[1]] = true;
grid[s[]][s[]] = -;
path.push_back({s[],s[]});
for(auto& dir : dirs) {
int newx = dir[] + s[], newy = dir[] + s[];
if(newx >= && newx < N && newy >= && newy < M && grid[newx][newy] != - && grid[newx][newy] != && grid[newx][newy] != -) {
int newcnt = cnt;
if(grid[newx][newy] == ) newcnt++;
helper(grid, path, {newx, newy}, e, newcnt, used);
}
}
grid[s[]][s[]] = ;
// used[s[0]*N+s[1]] = false;
path.pop_back();
} void printgird(vector<vector<int>>& grid) {
int N = grid.size(), M = grid[].size();
for(int i=; i<N; i++) {
for(int j=; j<M; j++) {
cout << grid[i][j] << " ";
}
cout << endl;
}
}
};

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. Hadoop_30_MapReduce_多job串联

    一个稍复杂点的处理逻辑往往需要多个mapreduce程序串联处理,多job的串联可以借助mapreduce框架的JobControl实现 示例代码:  每个job装配完成才可以进行下面代码: Cont ...

  2. 地沟油jiance

    与地沟油较劲九年(我和我的祖国) 任  飞 2019年06月13日05:00  来源:人民网-人民日报 分享到:     地沟油中含有多种有毒有害物质,但地沟油的检测却是一大难题.单靠眼睛谁也无法判断 ...

  3. hibernate入门配置及第一个hibernate程序

    学习了hibernate后就想先给大家分享一下它的配置方法: jar包导入 一.数据库表的创建   二.开启hibernate配置 编译器:eclipse 数据库:mysql 1.创建第一个xml文件 ...

  4. PAT Basic 1095 解码PAT准考证 (25 分)

    PAT 准考证号由 4 部分组成: 第 1 位是级别,即 T 代表顶级:A 代表甲级:B 代表乙级: 第 2~4 位是考场编号,范围从 101 到 999: 第 5~10 位是考试日期,格式为年.月. ...

  5. JAI丢包掉帧处理

    问题 时间戳停止变化/图像停止更新 描述 本小白刚刚接触JAI,有很多不懂的地方.这次遇到问题是请教了YZ大哥(不知道年龄,暂时这么称呼),很感谢YZ大哥的耐心指导.因为我不仅不知道怎么调,连在哪里调 ...

  6. Linux之df磁盘信息

    df命令用于查看磁盘的分区,磁盘已使用的空间,剩余的空间 1.用法 df [选项] [文件..] 2.命令选项 -a,--all 全部文件系统-h,--human-readable 以以合适的单位来显 ...

  7. Jenkins构建从github上克隆时,报Host key verification failed.

    首先在本地通过CMD执行git clone xxxxx时,可以成功的通过免密(SSH_KEY)克隆下来代码,但是通过Jenkins克隆时,就报如下信息: Cloning into 'GitHub'.. ...

  8. 04—mybatis的关联映射

    mybatis的关联映射一对一一对多多对多 一.一对一(一个人只能有一个身份证号) 1.创建表创建表tb_card CREATE TABLE `tb_card` ( `id` int(11) NOT ...

  9. debug:The key "" is not recognized and ignored.

    今天写代码时候发现chrome浏览器有一个警告The key "" is not recognized and ignored.既然发现了就处理一下吧,于是就有了这篇小文章. 查阅 ...

  10. PHP实现省市区关键词搜索邮编

    前两天做了一个项目, 其中有一个需求是根据用户输入的关键词查询邮编. 最开始设计的数据库结构是省市区分为三个字段, province, city, area, 但是在写代码实现的过程中发现, 用户只输 ...