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. Notepad++快捷键及使用技巧

    常用快捷键: CTRL+Q 注释/取消注释 用Notepad++写代码,要是有一些重复的代码想copy一下,还真不容易,又得动用鼠标,巨烦人....有木有简单的方法呢,确实还是有的不过也不算太好用.主 ...

  2. SmartBinding with kbmMW #4

    前言 在前面写过的文章中,详细介绍过如何将各种的控件与数据源进行绑定(Bind).在这篇文章中,将重点讨论如何绑定TImage和TListView.(同时支持VCL与Firemonkey). 将图形数 ...

  3. 【转载】interpolation(插值)和 extrapolation(外推)的区别

    根据已有数据以及模型(函数)预测未知区域的函数值,预测的点在已有数据范围内就是interpolation(插值), 范围外就是extrapolation(外推). The Difference Bet ...

  4. win10下 switchhost权限修改问题

    switchhost提示没有切换权限:C:\WINDOWS\system32\drivers\etc\host 文件无法修改   1.找到host文件 C:\Windows\System32\driv ...

  5. 一周死磕fastreport ----ASP.NET (一)

    https://blog.csdn.net/wuyuander/article/details/52692435 原文链接,点击跳转 首先是安装好FastReport .net: 然后在vs2012中 ...

  6. php函数copy和rename的区别

    copy ( string source, string dest )将文件从 source 拷贝到 dest.如果成功则返回 TRUE,失败则返回 FALSE. 如果要移动文件的话,请用 renam ...

  7. jquery.table2excel,将HTML的table标签数据导出成excel

    <!DOCTYPE> <html> <head> <meta http-equiv="Content-Type" content=&quo ...

  8. 实体类 @TableName&@TableField&@Version

    //指向表table_biao @TableName("table_biao)public class UserThirdLogin extends Model<UserThirdLo ...

  9. 新西达电调初始化代码,使用nodejs ffi技术调用wiringpi,代码使用typescript编写

    这是我设计的F450四轴飞行器飞控代码的一部分 运行在orangepi-zero上,操作系统是armbian,思路是使用node-ffi调用wiringpi的so库与GPIO通信,然后控制端逻辑代码使 ...

  10. 前端导出pdf

    html2canvas文档地址 http://html2canvas.hertzen.com/configuration 方式一:使用html2canvas和jspdf插件实现 该方式是通过html2 ...