Advanced Dynamic Programming

Theoretical Analysis

Dynamic programming is actually remember the answer before the question, to analyze and to solve the current problem and then use the answers before the questions, and there are two very important step, is  dismantling  and  defined state .

This time to a specific class of dynamic programming, dynamic programming matrix class problem, take a look at the ideas and points for attention to this kind of problem.

Dynamic programming matrices, also called dynamic programming coordinate classes, such problems will usually give you a matrix, the matrix which share some information, then you need to solve the problem based on that information.

Generally speaking, when thinking of such a dynamic programming problem, we only need to think about the state of the current location, and then try to see the current position and its progressive relationship neighbors, we want to arrive recurrence equation, which a class of dynamic programming problem is relatively simple

 

Different paths

Description:  m  and  n  values of not more than 100.

Topic Analysis:

  • Dismantling problem: the title says, each move can only be to the right or down, dynamic programming matrix class need to focus on the relationship between the current position and its adjacent location, for a certain location, the only route through it it came from above, or it came from the left, so if you request a different path to reach the current position, we need to know the different paths to reach its upper position, and reaches its left position of the different paths
  • Status Definition: dynamic programming matrices state definition is relatively simple, you can just look at the current location, dismantling problem, we analyzed the relationship between the current location and its neighbors, each location mentioned fact can be counted is the end, the state is represented by " the number of different paths from the origin to the position "
  • Recurrence equation: dp [i] [j] = dp [i] [j-1] + dp [i-1] [j]
  • Realization: we have to consider the state of the array initialization problem, to a point on the border and the left edge, because they can only come from one direction, needs to be considered separately, such as points on the boundary of this can only come from a left direction, the left margin the point can only come from one direction on top of this, the number of different paths they in fact only one, like early treatment.
#include <iostream>
#include <cstdio>
#include <algorithm> 
using namespace std;
int main(){
	int m,n;//行,列
	cin >>m>>n;
	int dp[m][n];
	for(int i=0;i<n;i++){
		dp[0][i]=1;
	}
	for(int i=0;i<m;i++){
		dp[i][0]=1;
	}
	for(int i=1;i<m;i++){
		for(int j=1;j<n;j++){
			dp[i][j]=dp[i-1][j]+dp[i][j-1];
		}
	}
	cout <<dp[m-1][n-1];
	return 0;
} 

Different paths (obstacle)

Analytical title

在上面那道题的基础上,矩阵中增加了障碍物,这里只需要针对障碍物进行判断即可,如果当前位置是障碍物的话,状态数组中当前位置记录的答案就是 0,也就是没有任何一条路径可以到达当前位置,除了这一点外,其余的分析方法和解题思路和之前 一样 

 

#include <iostream>
#include <cstdio>
int dp[100][100];
using namespace std;
int main(){
	int m,n;
	cin >>m>>n;
	for(int i=0;i<m;i++){
		for(int j=0;j<n;j++){
			cin >>dp[i][j];
		}
	}
	for(int i=0;i<n;i++){
		dp[0][i]=(dp[0][i]==1)?0:1; 
	}
	for(int i=0;i<m;i++){
		dp[i][0]=(dp[i][0]==1)?0:1;
	}
	for(int i=1;i<m;i++){
		for(int j=1;j<n;j++){
			dp[i][j]=(dp[i][j]==1)?0:dp[i-1][j]+dp[i][j-1];
		}
	}
	cout <<dp[m-1][n-1];
	return 0;
} 

最小路径和

给定一个包含非负整数的 m x n 网格,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小。

说明:每次只能向下或者向右移动一步。

 

题目解析:

  • 问题拆解:到达终点pb[i][j]的数字最小和,及求到达pb[i][j-1],pb[i-1][j]最小和
  • 状态定义:状态表示是 “从起点到达该位置的最小和
  • 递推方程:dp[i][j]=min(dp[i][j-1],dp[i-1][j])
  • 实现:我们还要考虑状态数组的初始化问题,对于上边界和左边界的点,因为它们只能从一个方向过来,需要单独考虑,比如上边界的点只能从左边这一个方向过来,其路径最小和就是到达左边元素的最小和

 

发布了107 篇原创文章 · 获赞 18 · 访问量 6303

Guess you like

Origin blog.csdn.net/qq_43109978/article/details/104075879