Title Description:
In the n*m of an Apple Tanaka, not a unit area has a number of apples, starting from (the) coordinates to collect, each time can only move down or to the right, ask the maximum number of apples collected? :)
For example:
3 3
1 2 3
6 8 9
1 1 1
The obvious answer is: 25.
Go down 1 steps, then 2 steps to the right, then 1 steps down.
Ideas:
A DP array in the code that stores the current state maximum, and the Apple array is used to store the number of apples in the corresponding location.
This problem to use dynamic planning, to collect the largest number of apples, in fact, is to go to (n,m) the maximum number of coordinates.
Divided into a sub-problem, for the current coordinates (I,J), can only be from (I-1,J) or from (i,j-1) moved over.
So Dp[i][j]=max (Dp[i-1][j],dp[i][j-1]) +apple[i][j].
Code:
#include <stdio.h>#include<string.h>#defineMAX 100intMain () {intn,m; intdp[max+7][max+7],apple[max+7][max+7]; while(SCANF ("%d%d", &n,&m)! =EOF) {memset (DP,0,sizeof(DP)); for(intI=1; i<=n;i++) for(intj=1; j<=m;j++) scanf ("%d",&Apple[i][j]); for(intI=1; i<=n;i++) { for(intj=1; j<=m;j++) {Dp[i][j]=dp[i-1][j]>=dp[i][j-1]?dp[i-1][J]:d p[i][j-1]; DP[I][J]+=Apple[i][j]; }} printf ("%d\n", Dp[n][m]); } return 0;}
Go to the fields and collect the apples!