Dp problem ----- This article was written two classic dp problem, the same ideas
If you have questions, please enlighten big brother ~ ~ Sahua Sahua
A. The number of tower problem :( topic below)
Ideas:
dp [i] [j] =======> storage
The array a [i] [j] ========> entered.
When we enter the number into the shape of the column of FIG we imagine him into an array, the row is set to i, as j, and down and right can be seen that only two directions, from the bottom up we .
List:
dp[i][j]=max(dp[i-1][j],dp[i-1][j-1])+a[i][j]
Code:
#include<cstdio>
#include<iostream>
using namespace std;
#include<cmath>
#include<cstring>
int main()
{
int C,N;
int dp[101][101];
int a[101][101];
memset(a,0,sizeof(a));
memset(dp,0,sizeof(dp));
scanf("%d",&C);
while(scanf("%d",&N)){
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= N; j++) {
scanf("%d",&a[i][j]);
}
}
}
dp[1][1]=a[1][1];
for (int i = 1; i <= N; i++) {
for (int j = 1; j <=N; j++) {
dp[i][j]=max(dp[i-1][j],dp[i-1][j-1])+a[i][j];
}
}
int ans = 0;
for (int i = 1; i <= N; i++) {
ans = max(dp[N][i], ans);
}
printf("%d",ans);
return 0;
}
II. Matrix :( access issues under the topic)
Thought the previous one, directly under the code posted
#include<cstdio>
#include<iostream>
using namespace std;
#include<cmath>
#include<cstring>
int main()
{
int dp[1001][1001];
int a[1001][1001];
memset(a,0,sizeof(a));
memset(dp,0,sizeof(dp));
int N;
while(scanf("%d",&N)!=EOF)
{
for(int i=1;i<=N;i++)
{
for(int j=1;j<=N;j++)
{ scanf("%d",&a[i][j]);
}
}
}
for(int i=1;i<=N;i++)
{
for(int j=1;j<=N;j++)
{
dp[i][j]=max(dp[i][j-1],dp[i-1][j])+a[i][j];
}
}
dp[1][1]=a[1][1];
int ans=0;
for(int i=1;i<=N;i++)
{
ans=max(ans,dp[N][i]);
}
printf("%d",ans);
}