Main topic: There is a grid of n (1<=n<=500) rows m (1<=m<=500), each grid has two kinds of mines, Yeyenum and Bloggium, in the west of the grid is Yeyenum refinery, the north side is Bloggium refinery , each grid has a certain number of two kinds of mines, now to arrange a conveyor system, the conveyor belt can only be from south to north or from east to west, conveyor belt in the same direction can be continuous transmission, only to the corresponding refinery is effective, ask many can get how many mines.
When using d[i][j] to indicate to row J of line I, the maximum number of mines that can be produced by the first row J column, using A[I][J] to indicate that the Yeyenum mine is added from column 1th to column J in row I, using b[i][j] to indicate that the Bloggium mine is added from line 1th to line i in column J, To the left or upward recursion according to the grid in column J of row I, either left, line I all left, or upward, column J all upward.
State transition equation: D[i][j]=max {d[i-1][j]+a[i][j],d[i][j-1]+b[i][j]}
#include <stdio.h> #include <stdlib.h>int a[510][510];int b[510][510];int d[510][510];int Main (void) {int i , j,n,m;scanf ("%d%d", &n,&m); while ((n!=0) | | (m!=0)) {for (i=1;i<=n;i++) {for (j=1;j<=m;j++) {scanf ("%d", &a[i][j]);}} for (i=1;i<=n;i++) {for (j=1;j<=m;j++) {scanf ("%d", &b[i][j]);}} for (i=1;i<=n;i++) {for (j=2;j<=m;j++) {a[i][j]=a[i][j-1]+a[i][j];}} for (i=2;i<=n;i++) {for (j=1;j<=m;j++) {b[i][j]=b[i-1][j]+b[i][j];}} for (i=1;i<=n;i++) {for (j=1;j<=m;j++) {if (D[i-1][j]+a[i][j]>d[i][j-1]+b[i][j]) {D[i][j]=d[i-1][j]+a[i][j] ;} ELSE{D[I][J]=D[I][J-1]+B[I][J];}}} printf ("%d\n", D[n][m]); scanf ("%d%d", &n,&m);} return 0;}
UVA 1366-martian minging (DP)