LC 980. Unique Paths III
2024-09-05 09:07:05
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;
}
}
};
最新文章
- Android笔记——提升ListView的运行效率
- [No000086]C#foreach集合被改变,报错处理方案
- 20145212 《Java程序设计》第6周学习总结
- linux crontab介绍
- j2ee Servlet、Filter、Listener
- css分离思想
- gc 辅助打印信息
- Solum入门知识(编辑中)
- bzoj1532
- [PeterDLax著泛函分析习题参考解答]第5章 赋范线性空间
- Pyhon之常用操作符 - 零基础入门学习Python006
- KB2533623 下载
- cygwin + git + nat123 30元搭建公网可访问的git服务器
- flex属性介绍
- html文件上传保存-(.html and string translate into .html )
- django创建一个简单的web站点
- csv 文件读取(input)和截分(split)方法
- java十年,需要学会的Java开发体系
- android学习-Toast的延迟时间
- 使用Thunderbird时你可能会用到的技巧