UVA 1366-martian minging (DP)

Source: Internet
Author: User

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)

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.