Short Path for getting started with DP

Source: Internet
Author: User


Want to see more problem-solving reports: http://blog.csdn.net/wangjian8006/article/details/7870410
Reprinted please indicate the source: http://blog.csdn.net/wangjian8006

We use a two-dimensional array of map to save this table, and use the two-dimensional array DP to save the optimal path from 0 to 0.

At the beginning, I thought that the DP array of this table was initialized

2 10 15 16 26

7 0 0 0 0

13 0 0 0 0

22 0 0 0 0

31 0 0 0 0

The above is the initialization of the first row and the first column, which is equal to the addition of the previous

Then we can get DP [I] [J] = max (DP [I-1] [J] + map [I] [J], DP [I] [J-1] + map [I] [J]);

The Code is as follows:

#include <iostream>using namespace std;#define max(a,b) (a>b?a:b)int map[100][100],n,dp[100][100];void write(int a[][100]){int i,j;for (i=0;i<n;i++){for(j=0;j<n;j++)printf("%d ",a[i][j]);printf("\n");}printf("\n");}int main(){int i,j;scanf("%d",&n);for(i=0;i<n;i++)for(j=0;j<n;j++)scanf("%d",&map[i][j]);dp[0][0]=map[0][0];for(i=1;i<n;i++) {dp[0][i] = dp[0][i-1]+map[0][i];dp[i][0] = dp[i-1][0]+map[i][0];}for(i=1;i<n;i++)for(j=1;j<n;j++){dp[i][j] = max(dp[i-1][j]+map[i][j],dp[i][j-1]+map[i][j]);}write(dp);return 0;}

However, DP [I] [J] = max (DP [I-1] [J], DP [I] [J-1]) can be used directly from 0 to 0. + map [I] [J];
The Code is as follows:

#include <iostream>using namespace std;#define max(a,b) (a>b?a:b)int map[100][100],n,dp[100][100];void write(int a[][100]){int i,j;for (i=0;i<n;i++){for(j=0;j<n;j++)printf("%d ",a[i][j]);printf("\n");}printf("\n");}int main(){int i,j;scanf("%d",&n);for(i=0;i<n;i++)for(j=0;j<n;j++)scanf("%d",&map[i][j]);memset(dp,0,sizeof(dp));for(i=0;i<n;i++)for(j=0;j<n;j++)dp[i][j] = max(dp[i-1][j],dp[i][j-1])+map[i][j];write(dp);return 0;}

This kind of code can be introduced later than the previous one, which is called recursion. Of course there is recursion, but the efficiency of recursion is extremely low. It takes more than 1 second to calculate the amount of data as long as n = 14.

 

#include <iostream>using namespace std;#define max(a,b) (a>b?a:b)int map[100][100],n,dp[100][100];int dfs(int i,int j){if(i < 0 || j < 0) return 0;return max(dfs(i-1,j),dfs(i,j-1))+map[i][j];}void write(int a[][100]){int i,j;for (i=0;i<n;i++){for(j=0;j<n;j++)printf("%d ",a[i][j]);printf("\n");}printf("\n");}int main(){int i,j;scanf("%d",&n);for(i=0;i<n;i++)for(j=0;j<n;j++)scanf("%d",&map[i][j]);printf("%d",dfs(n-1,n-1));return 0;}
 
/* The efficiency of recursion is slow because of repeated steps and data redundancy. To avoid data redundancy, we add a number Group mark to check whether the mark has been traversed, so the efficiency is almost the same as that of recursion */
#include <iostream>using namespace std;#define max(a,b) (a>b?a:b)int map[100][100],n,dp[100][100];int dfs(int i,int j){if(i < 0 || j < 0) return 0;if(dp[i][j]) return dp[i][j];dp[i][j] = max(dfs(i-1,j),dfs(i,j-1))+map[i][j];return dp[i][j];}void write(int a[][100]){int i,j;for (i=0;i<n;i++){for(j=0;j<n;j++)printf("%d ",a[i][j]);printf("\n");}printf("\n");}int main(){int i,j;scanf("%d",&n);for(i=0;i<n;i++)for(j=0;j<n;j++)scanf("%d",&map[i][j]);memset(dp,0,sizeof(dp));printf("%d",dfs(n-1,n-1));return 0;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.