LC 980. Unique Paths III
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 <= 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的更多相关文章
- 原题链接在这里: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
On a 2-dimensional grid, there are 4 types of squares: 1 represents the starting square. There is e ...
- 【leetcode】980. Unique Paths III
题目如下: On a 2-dimensional grid, there are 4 types of squares: 1 represents the starting square. Ther ...
- 【LeetCode】980. Unique Paths III解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...
- 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 ...
- [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 ...
- [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 ...
- [LeetCode] Unique Paths II 不同的路径之二
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
随机推荐
- 2.3 使用 dom4j 对 xml文件进行 dom 解析
// 使用dom4j对XML文档进行解析 CRUD public class Demo1 { //读取XML文档中第二本书的书名 <书名>javaWEB</书名> @Test ...
- instanceof解析
https://www.zhihu.com/question/21574535/answer/18998914 Java instanceof 关键字是如何实现的? 基本理解 只是在同一个类加载器加载 ...
- Tomcat - Tomcat安装
Tomcat官网:http://tomcat.apache.org/ 准备:JAVA环境布置完成 一.Windows平台 1. 版本选择 1) 进入官网 2) 查看版本匹配 官网说明 https:// ...
- SqlMetaData异常 dbType xx 对于此构造函数无效。
今天在dapper中想扩展使用表值类型参数——tableValue.但是dapper不支持此类参数,于是扩展了一下.其中出现了一个问题. Microsoft.SqlServer.Server.SqlM ...
- C# 接口的作用浅谈举例(转)
转:http://blog.csdn.net/liuqinghui1990/article/details/77171051 我初次接触接口(Interface),对接口的作用有点迷茫,C#接口中包含 ...
- 关于WebMvcConfigurationSupport的大坑-静态资源访问不了
WebMvcConfigurationSupport是spring boot2.0以后用来替代WebMvcConfigurerAdapter,但是如果你直接用WebMvcConfigurationSu ...
- 用JS将毫秒转化成天时分秒的时间格式
function formatDuring(mss) { var days = parseInt(mss / (1000 * 60 * 60 * 24)); var hours = parseInt( ...
- Vue-项目重要配置
Vue配置axios ''' 1)安装插件(一定要在项目目录下): >: cnpm install axios 2)在main.js中配置: import axios from 'axios' ...
- [MySQL优化] -- 如何定位效率较低的SQL
一般通过以下两种方式定位执行效率较低的 SQL 语句. 通过慢查询日志定位那些执行效率较低的 SQL 语句,用 --log-slow-queries[=file_name] 选项启动时, mysqld ...
- MyBatis中#{}和${}的不同和${}的妙用(转)
突然意识到sql语句的独特语义要和代码分离,我们就不能够在代码中写sql语句!!比如我要用${}在MyBatis的sql中拼接排序类型的时候,我就不能够在Java代码中直接写参数字符串为Ord ...