Unique Paths II (dp题)
Follow up for "Unique Paths":
Now consider if some obstacles are added to the grids. How many unique paths would there be?
An obstacle and empty space is marked as 1
and 0
respectively in the grid.
For example,
There is one obstacle in the middle of a 3x3 grid as illustrated below.
- [
- [0,0,0],
- [0,1,0],
- [0,0,0]
- ]
The total number of unique paths is 2
.
Note: m and n will be at most 100.
代码:
- class Solution {
- public:
- int uniquePathsWithObstacles(vector<vector<int> > &obstacleGrid) {
- if(obstacleGrid.size()==) return ;
- int row=obstacleGrid.size();
- int col=obstacleGrid[].size();
- int dp[][];
- memset(dp,,sizeof(dp));
- bool rblockTag=false;
- bool cblockTag=false;
- if(obstacleGrid[][]==||obstacleGrid[row-][col-]==)
- return ;
- for(int i=;i<col;++i){
- if(obstacleGrid[][i]==) rblockTag=true;
- if(!rblockTag) dp[][i]=;
- else dp[][i]=;
- }
- for(int j=;j<row;++j){
- if(obstacleGrid[j][]==) cblockTag=true;
- if(!cblockTag) dp[j][]=;
- else dp[j][]=;
- }
- for(int i=;i<row;++i){
- for(int j=;j<col;++j){
- if(obstacleGrid[i][j]==) dp[i][j]=;
- else dp[i][j]=dp[i][j-]+dp[i-][j];//i和j是从1开始
- }
- }
- return dp[row-][col-];
- }
- };
Unique Paths II (dp题)的更多相关文章
- leetcode-63. Unique Paths II · DP + vector
题面 A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). ...
- [LeetCode] Unique Paths && Unique Paths II && Minimum Path Sum (动态规划之 Matrix DP )
Unique Paths https://oj.leetcode.com/problems/unique-paths/ A robot is located at the top-left corne ...
- Leetcode之动态规划(DP)专题-63. 不同路径 II(Unique Paths II)
Leetcode之动态规划(DP)专题-63. 不同路径 II(Unique Paths II) 初级题目:Leetcode之动态规划(DP)专题-62. 不同路径(Unique Paths) 一个机 ...
- 动态规划小结 - 二维动态规划 - 时间复杂度 O(n*n)的棋盘型,题 [LeetCode] Minimum Path Sum,Unique Paths II,Edit Distance
引言 二维动态规划中最常见的是棋盘型二维动态规划. 即 func(i, j) 往往只和 func(i-1, j-1), func(i-1, j) 以及 func(i, j-1) 有关 这种情况下,时间 ...
- LeetCode之“动态规划”:Minimum Path Sum && Unique Paths && Unique Paths II
之所以将这三道题放在一起,是因为这三道题非常类似. 1. Minimum Path Sum 题目链接 题目要求: Given a m x n grid filled with non-negative ...
- 62. Unique Paths && 63 Unique Paths II
https://leetcode.com/problems/unique-paths/ 这道题,不利用动态规划基本上规模变大会运行超时,下面自己写得这段代码,直接暴力破解,只能应付小规模的情形,当23 ...
- 【leetcode】Unique Paths II
Unique Paths II Total Accepted: 22828 Total Submissions: 81414My Submissions Follow up for "Uni ...
- 【LeetCode练习题】Unique Paths II
Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are added to ...
- 【LeetCode】63. Unique Paths II
Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are added to ...
- [Leetcode Week12]Unique Paths II
Unique Paths II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/unique-paths-ii/description/ Descrip ...
随机推荐
- HttpServletResponse 的状态码
public static final int SC_ACCEPTED 202 public static final int SC_BAD_GATEWAY 502 public static ...
- 关于bin和obj文件夹。debug 和release的区别(转)
关于bin和obj文件夹. 楼主hcaihao(影子男孩)2002-05-29 20:04:24 在 .NET技术 / C# 提问 VS.Net会生成bin和obj文件夹以及它们下面的Debug和Re ...
- Java JDK装配置
1- 介绍 本文章介绍JAVA开发环境安装是基于: Java8(JDK8) 2- 下载JDK http://www.oracle.com/technetwork/java/javase/dow ...
- COGS 942. [東方S3] 比那名居天子
Problem 1 比那名居天子(tenshi.cpp/c/pas) 题目描述 在幻想乡,比那名居天子是管理着『要石』的天人.『要石』是能够引发和镇压地震的存在,当然也可以用来改变地形.因为在幻想乡引 ...
- 朴素贝叶斯法(naive Bayes)
<统计学习方法>(第二版)第4章 4 朴素贝叶斯法 生成模型 4.1 学习与分类 基于特征条件独立假设学习输入输出的联合概率分布 基于联合概率分布,利用贝叶斯定理求出后验概率最大的输出 条 ...
- ROI(Region of Interesting)
ROI(region of interest),感兴趣区域.机器视觉.图像处理中,从被处理的图像以方框.圆.椭圆.不规则多边形等方式勾勒出需要处理的区域,称为感兴趣区域,ROI.在Halcon.Ope ...
- <c:forEach>实例演示
<c:forEach>实例演示 <%@ page language="java" contentType="text/html; charset=UTF ...
- linux 安装nginx 集成emq
1:下载nginx-1.12.2.tar.gz http://nginx.org/en/download.html 2:解压 tar -zxvf nginx-1.12.2.tar.gz 3:进行co ...
- 前段开发 react native tab功能
import React, { Component } from 'react'; import { StyleSheet, Text, View, Image, } from 'react-nati ...
- 字典(trie)树--从入门到入土
今天再来认识一个强大的数据结构. 字典树又称单词查找树,Trie树,是一种树形结构,是一种哈希树的变种.典型应用是用于统计,排序和保存大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词 ...