作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/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. 1 <= grid.length * grid[0].length <= 20

题目大意

给了一个二维矩阵,1代表起点,2代表终点,0代表可以走的格子,-1代表障碍物。求从起点到终点,把所有的可以走的格子都遍历一遍,所有可能的不同路径数。

解题方法

回溯法

周赛最后一题却是一个很简单的题目,因为题目给定了格子的大小总共才20个!也就是说可以使用O(2^N)的解法来做,即可以使用回溯法暴力求解所有可能路径,然后判断每个路径是否符合要求。(注:2的20次方 = 1048576.)

本身很简单哈,题目其实只有两个限制:第一,所有空白格子必须走一遍;第二,不能走障碍物上。

因此,我先统计了一下总的有多少个空白格子,然后每次经过一个空白格子都累加一下,如果遍历到终点并且走过的空白格子数等于grid中初始的zerocount,那么说明走过了所有空白格子,符合要求。

至于不能走障碍物,直接判断一下就好了,这个没啥说的。总之题目很简单,暴力求解不用怕。

下面做一下回溯法的思考:

第一,我在第一遍的时候保存了经历的路径,然后使用set去重,我以为只有这样才能保证结果里面不会出现重复的路径,但事实上是不需要的。回溯法不出现重复的路径,因为我们向后退了一步之后,下一轮的时候不会再沿着刚才已经尝试过的方向走了,这也就是对方向进行遍历的意义所在。只要回到上一步的位置,然后沿着另外一个方向继续寻找,那么找到的新的路径一定是不一样的。这也是回溯法的时间复杂度是O(2^N)的原因:找到了所有可能的路径,而这些路径是不会重复的。

第二,在dfs的时候,如果当前位置是0的话,我就对找到的0的个数pathcount+1,而之后是没有pathcount-1操作的。为什么?其实可以看出这个变量是统计在已经路过的路径上1的个数,而不同的路径的1的个数一定是不一样的,所以dfs()函数定义的时候对该变量做的是传值而不是传引用。所以,该变量在完成新的路径上0的个数统计之后已经没有意义了,不同的路径是不能共享该变量的,所以不用再对这个变量进行回溯操作。他会在完成自己的历史使命之后,在该dfs()函数结束的时候,退出历史舞台。

c++代码如下:

class Solution {
public:
int uniquePathsIII(vector<vector<int>>& grid) {
const int M = grid.size();
const int N = grid[0].size();
int zerocount = 0;
int res = 0;
for (int i = 0; i < M; ++i) {
for (int j = 0; j < N; ++j) {
if (grid[i][j] == 0) {
++zerocount;
}
}
}
for (int i = 0; i < M; ++i) {
for (int j = 0; j < N; ++j) {
if (grid[i][j] == 1) {
dfs(grid, i, j, 0, zerocount, res);
}
}
}
return res;
} void dfs(vector<vector<int>>& grid, int x, int y, int pathcount, int zerocount, int& res) {
if (grid[x][y] == 2 && pathcount == zerocount)
++res;
const int M = grid.size();
const int N = grid[0].size();
int pre = grid[x][y];
if (pre == 0)
++pathcount;
grid[x][y] = -1;
for (auto d : dirs) {
int nx = x + d.first;
int ny = y + d.second;
if (nx < 0 || nx >= M || ny < 0 || ny >= N || grid[nx][ny] == -1)
continue;
dfs(grid, nx, ny, pathcount, zerocount, res);
}
grid[x][y] = pre;
}
private:
vector<pair<int, int>> dirs = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
};

日期

2019 年 1 月 20 日 —— 这次周赛有点简单

【LeetCode】980. Unique Paths III解题报告(C++)的更多相关文章

  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 ...

  2. LC 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: Unique Paths II 解题报告

    Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution  Fol ...

  4. 【LeetCode】63. Unique Paths II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/unique-pa ...

  5. 原题链接在这里:980. Unique Paths III

    原题链接在这里:https://leetcode.com/problems/unique-paths-iii/ 题目: On a 2-dimensional grid, there are 4 typ ...

  6. 【leetcode】980. Unique Paths III

    题目如下: On a 2-dimensional grid, there are 4 types of squares: 1 represents the starting square.  Ther ...

  7. LeetCode 929 Unique Email Addresses 解题报告

    题目要求 Every email consists of a local name and a domain name, separated by the @ sign. For example, i ...

  8. 980. Unique Paths III

    题目来源: https://leetcode.com/problems/unique-paths-iii/ 自我感觉难度/真实难度: 题意: 分析: 回溯法,直接DFS就可以了 自己的代码: clas ...

  9. Leetcode之深度优先搜索&回溯专题-980. 不同路径 III(Unique Paths III)

    Leetcode之深度优先搜索&回溯专题-980. 不同路径 III(Unique Paths III) 深度优先搜索的解题详细介绍,点击 在二维网格 grid 上,有 4 种类型的方格: 1 ...

随机推荐

  1. C#序号

    OnRowCreated="gridViewCorrection_RowCreated" <asp:BoundField HeaderText="序号" ...

  2. python 多态、组合、反射

    目录 多态.多态性 多态 多态性 鸭子类型 父类限制子类的行为 组合 面向对象的内置函数 反射 多态.多态性 多态 多态通俗理解起来,就像迪迦奥特曼有三种形态一样,怎么变还是迪迦奥特曼 定义:多态指的 ...

  3. Scala【json字符串和json对象互相转换】

    一.fastjson工具 pom依赖 <dependency> <groupId>com.alibaba</groupId> <artifactId>f ...

  4. 零基础学习java------day27-28---------电影评分数据案例,. RPC案例

    一.  电影评分数据案例 movie:电影id rate:用户评分 timeStamp:评分时间 uid:用户id 简化数据: 需求: (1)每个用户评分最高的3部电影 (2)每个用户评分的平均值 ( ...

  5. 【leetocde】922. Sort Array By Parity II

    Given an array of integers nums, half of the integers in nums are odd, and the other half are even.  ...

  6. Dockers启动Kafka

    首先安装 Confluent Platform Quick Start for Confluent Platform (Local install) Use this quick start to g ...

  7. 错误: 找不到或无法加载主类(IDEA中启动spring boot项目)

    版权声明:本文为博主原创文章,如果转载请给出原文链接:http://www.jufanshare.com/content/142.html 提示:需要对IDEA编辑工具使用熟悉 出现一个问题,就是sp ...

  8. c3p0的使用步骤

    //1.导入c3p0的连个包,和mysql的驱动包//2.配置c3p0.xml的配置文件 <c3p0-config> <!-- 使用默认的配置读取连接池对象 --> <d ...

  9. Spring AOP通过注解的方式设置切面和切入点

    切面相当于一个功能的某一个类,切入点是这个类的某部分和需要额外执行的其他代码块,这两者是多对多的关系,在代码块处指定执行的条件. Aspect1.java package com.yh.aop.sch ...

  10. 【力扣】有序矩阵中第K小的元素

    给定一个 n x n 矩阵,其中每行和每列元素均按升序排序,找到矩阵中第 k 小的元素.请注意,它是排序后的第 k 小元素,而不是第 k 个不同的元素. 示例: matrix = [ [ 1, 5, ...