Leetcode 980. 不同路径 III
- 用户通过次数42
- 用户尝试次数43
- 通过次数46
- 提交次数60
- 题目难度Hard
在二维网格 grid 上,有 4 种类型的方格:
1表示起始方格。且只有一个起始方格。2表示结束方格,且只有一个结束方格。0表示我们可以走过的空方格。-1表示我们无法跨越的障碍。
返回在四个方向(上、下、左、右)上行走时,从起始方格到结束方格的不同路径的数目,每一个无障碍方格都要通过一次。
示例 1:
输入:[[1,0,0,0],[0,0,0,0],[0,0,2,-1]]
输出:2
解释:我们有以下两条路径:
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)
示例 2:
输入:[[1,0,0,0],[0,0,0,0],[0,0,0,2]]
输出:4
解释:我们有以下四条路径:
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)
示例 3:
输入:[[0,1],[2,0]]
输出:0
解释:
没有一条路能完全穿过每一个空的方格一次。
请注意,起始和结束方格可以位于网格中的任意位置。
提示:
1 <= grid.length * grid[0].length <= 20
class Solution {
public:
int uniquePathsIII(vector<vector<int>>& grid) {
int n = grid.size();
int m = grid[].size();
int a = ;
int b = ;
vector<vector<int>> vis(n,vector<int>(m));
int cnt = ;
for(int i=;i < n;i++){
for(int j=;j < m;j++){
if(grid[i][j] == ){
cnt++;
vis[i][j] = ;
}
else if(grid[i][j] == ){
vis[i][j] = ;
}
else if(grid[i][j] == -){
vis[i][j] = ;
}
else if(grid[i][j] == ){
vis[i][j] = ;
a = i;b = j;
}
}
}
int res = ;
dfs(grid,vis,a,b,res,,cnt);
return res;
}
void dfs(vector<vector<int>> grid,vector<vector<int>> vis,int n,int m,int& num,int cnt,int maxnum){
if(grid[n][m] == ){
if(cnt- == maxnum) num++;
else return;
}
if((n->=&&n-<grid.size())&&(m>=&&m<grid[].size())&&vis[n-][m] == ){
vis[n-][m] = ;
dfs(grid,vis,n-,m,num,cnt+,maxnum);
vis[n-][m] = ;
}
if((n+>=&&n+<grid.size())&&(m>=&&m<grid[].size())&&vis[n+][m] == ){
vis[n+][m] = ;
dfs(grid,vis,n+,m,num,cnt+,maxnum);
vis[n+][m] = ;
}
if((n>=&&n<grid.size())&&(m+>=&&m+<grid[].size())&&vis[n][m+] == ){
vis[n][m+] = ;
dfs(grid,vis,n,m+,num,cnt+,maxnum);
vis[n][m+] = ;
}
if((n>=&&n<grid.size())&&(m->=&&m-<grid[].size())&&vis[n][m-] == ){
vis[n][m-] = ;
dfs(grid,vis,n,m-,num,cnt+,maxnum);
vis[n][m-] = ;
}
}
};
牛逼哦AC了
Leetcode 980. 不同路径 III的更多相关文章
- Leetcode之深度优先搜索&回溯专题-980. 不同路径 III(Unique Paths III)
Leetcode之深度优先搜索&回溯专题-980. 不同路径 III(Unique Paths III) 深度优先搜索的解题详细介绍,点击 在二维网格 grid 上,有 4 种类型的方格: 1 ...
- leetcode #980 不同路径||| (java)
在二维网格 grid 上,有 4 种类型的方格: 1 表示起始方格.且只有一个起始方格.2 表示结束方格,且只有一个结束方格.0 表示我们可以走过的空方格.-1 表示我们无法跨越的障碍.返回在四个方向 ...
- 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 260. Single Number III(只出现一次的数字 III)
LeetCode 260. Single Number III(只出现一次的数字 III)
- LeetCode:组合总数III【216】
LeetCode:组合总数III[216] 题目描述 找出所有相加之和为 n 的 k 个数的组合.组合中只允许含有 1 - 9 的正整数,并且每种组合中不存在重复的数字. 说明: 所有数字都是正整数. ...
- LeetCode:简化路径【71】
LeetCode:简化路径[71] 题解参考天码营:https://www.tianmaying.com/tutorial/LC71 题目描述 给定一个文档 (Unix-style) 的完全路径,请进 ...
- LeetCode 71.简化路径
LeetCode 71.简化路径 题目描述: 以 Unix 风格给出一个文件的绝对路径,你需要简化它.或者换句话说,将其转换为规范路径.在 Unix 风格的文件系统中,一个点(.)表示当前目录本身:此 ...
- [LeetCode] 216. Combination Sum III 组合之和 III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- leetcode 64. 最小路径和 动态规划系列
目录 1. leetcode 64. 最小路径和 1.1. 暴力 1.2. 二维动态规划 2. 完整代码及执行结果 2.1. 执行结果 1. leetcode 64. 最小路径和 给定一个包含非负整数 ...
随机推荐
- 洛谷p1732 活蹦乱跳的香穗子 二维DP
今天不BB了,直接帖原题吧 地址>>https://www.luogu.org/problem/show?pid=1732<< 题目描述 香穗子在田野上调蘑菇!她跳啊跳,发现 ...
- mysql表分区存储过程
本文为博主原创,未经允许不得转载: 由于数据库一张表数据量有几千万条,而且在不断增长,看见公司前辈写了一个创建表分区的存储过程,感觉 甚是牛逼,在此供自己保留学习. /*PROCEDURE creat ...
- 【译】第37节---EF6-异步查询和保存
原文:http://www.entityframeworktutorial.net/entityframework6/async-query-and-save.aspx 你可以在.NET4.5下使用 ...
- UVA12558 埃及分数
#include<iostream> #include<cstdio> #include<set> #include<memory.h> using n ...
- progressBar显示百分比
this.lab_AllFiles.Text = progressBarAllFile.Value * 100 / progressBarAllFile.Maximum + "%" ...
- vue运行报错--preventDefault
[Intervention] Unable to preventDefault inside passive event listener due to target being treated as ...
- Codeforces 781B. Innokenty and a Football League
题目链接:http://codeforces.com/contest/781/problem/B 去tmd 2-SAT 直接贪心就可以过去了,优先选择第二种情况. 然而....可以被叉掉(数据水了) ...
- go 接口以及对象的使用
// Sample program to show how to declare methods and how the Go // compiler supports them. package m ...
- 【C#】采用OleDB读取Excel文件转DataTable
using System; using System.Data; using System.Data.OleDb; using System.IO; using System.Linq; using ...
- Android Studio waiting for debugger 卡死
当你使用AS进行调试时,发现卡在进入调试模式的 waiting for debugger 不动了,出现这种问题是adb端口被抢占了.解决方法就是: 在命令行: netstat -ano|findstr ...