LeetCode Algorithm 0063 - Unique Paths II (Medium)

解决LeetCode上的0063题——带障碍物的唯一路径。机器人从网格的左上角开始,只能向下或向右移动,目标是到达右下角。讨论了如何计算独特的路径数量,包括遇到障碍物的情况。题目链接、相关主题和C++解决方案都进行了概述。
摘要由CSDN通过智能技术生成

LeetCode Algorithm 0063 - Unique Paths II (Medium)

返回分类:全部文章 >> 基础知识

返回上级:LeetCode 算法目录


Problem Link: https://leetcode.com/problems/unique-paths-ii/

Related Topics: Array Dynamic Programming


Description

A robot is located at the top-left corner of a m x n grid (marked ‘Start’ in the diagram below).

The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked ‘Finish’ in the diagram below).

How many possible unique paths are there?

robot_maze

An obstacle and empty space is marked as 1 and 0 respectively in the grid.

Note: m and n will be at most 100.

Example 1:

Input:
[
  [0,0,0],
  [0,1,0],
  [0,0,0]
]
Output: 2
Explanation:
There is one obstacle in the middle of the 3x3 grid above.
There are two ways to reach the bottom-right corner:
1. Right -> Right -> Down -> Down
2. Down -> Down -> Right -> Right

Solution C++

// Author: https://blog.csdn.net/DarkRabbit
// Problem: https://leetcode.com/problems/unique-paths-ii/
// Difficulty: Medium
// Related Topics: `Array` `Dynamic Programming`

#pragma once

#include "pch.h"

namespace P63UniquePathsII
{
    class Solution
    {
        public:
        int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid)
        {
            int rows = obstacleGrid.size();
            if (rows == 0)
            {
                return 0;
            }

            int cols = obstacleGrid[0].size();
            if (cols == 0)
            {
                return 0;
            }

            if (obstacleGrid[0][0] == 1)
            {
                return 0;
            }

            // 初始化全0走法二维数组(路径数量)
            vector<vector<long long>> pathCount = vector<vector<long long>>(rows, vector<long long >(cols, 0));
            pathCount[0][0] = 1LL;

            // 第一列的行,都只有1种走法
            for (int r = 1; r < rows; r++)
            {
                if (obstacleGrid[r][0] == 1)
                {
                    break;
                }
                else
                {
                    pathCount[r][0] = 1;
                }
            }

            // 第一行的列,都只有1种走法
            for (int c = 1; c < cols; c++)
            {
                if (obstacleGrid[0][c] == 1)
                {
                    break;
                }
                else
                {
                    pathCount[0][c] = 1;
                }
            }

            // 中间网格,每个网格都可以由 “上”“左” 两个方向来,
            // 则走到这个网格的数量是,两个方向走法的和
            for (int r = 1; r < rows; r++)
            {
                for (int c = 1; c < cols; c++)
                {
                    if (obstacleGrid[r][c] != 1)
                    {
                        pathCount[r][c] = pathCount[r - 1][c] + pathCount[r][c - 1];
                    }
                }
            }
            return pathCount[rows - 1][cols - 1];
        }
    };
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值