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. django 文件上传样例以及遇到的一些问题

    使用django上传文件 主流有两种方法 from表单以及ajax,为了自由度高一点,选择了ajax来实现文件的上传 前端部分代码如下: 主要关注 一 有一个文件上传(type='file')的按钮, ...

  2. LintCode上的一道算法面试题: 数字的统计

    说到数字的统计,小时候的数学课大家都应该有学过,但数字太多太复杂的,手动肯定耗时间不说还很容易出错.所以今天分享一下如何用程序来完成. Have you met this question in a ...

  3. linux入门常用指令4.挂载数据盘

    挂载硬盘 #查看当前分区情况 [root@localhost ~]# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sdb 8:16 0 5G 0 dis ...

  4. 桌面Ubuntu卡死解决方案

    通常情况下,我们用桌面Ubuntu会遇到卡住的的情况,我们一般会进行强制关机处理,但其实还有另一种操作,不用强制关机. 切换到tty模式,执行命令pkill X;start X;就能重新进入桌面,不用 ...

  5. 【Linux常用命令】Linux kill, killall, kill -9,

    参考链接 https://blog.csdn.net/zong596568821xp/article/details/77899454 kill + PID kill -9 + PID  加上-9 是 ...

  6. java线程基础巩固---策略模式在Thread和Runnable中的应用分析

    在上篇[http://www.cnblogs.com/webor2006/p/7709647.html]中已经学习了Runnable出现的好处,其实这种设计是采用的一种策略模式,所以为了进一步理解Ru ...

  7. flyio 的请求封装

    1.安装flyio.js npm install flyio --save-dev 2.在util创建一个fly.js用于封装 import Vue from 'vue' var Fly=requir ...

  8. [唐胡璐]Selenium技巧- ReportNG替换TestNG默认结果报告

    TestNG默认的报告虽然内容挺全,但是展现效果却不太理想,不易阅读。因此我们想利用ReportNG来替代TestNG默认的report。 什么是ReportNG呢?这里不多说,请直接参见:http: ...

  9. docker化canal与canal-adapter

    今日公司需求,需要将mysql更新实时同步到kafka中,后来又要将数据库中的一张表的变化实时同步到另一台mysql中,于是乎canal与canal-adapter紧急解决,其中踩了不少坑,下面为总结 ...

  10. Spring Cloud Gateway整合Eureka

    Spring Cloud Gateway features: Built on Spring Framework 5, Project Reactor and Spring Boot 2.0 Able ...