Hangzhou Electric 2391 Filthy Rich && Hangzhou 2571 Fate (moving rules)

Source: Internet
Author: User

Since these two topics have a progressive relationship, so write them in a blog, I hope to help you.

http://acm.hdu.edu.cn/showproblem.php?pid=2391

Input:

1 3 4 1 8 8 0 0 1 8 0 0 Output:

Scenario #1:42 The main idea is to go from the upper left corner of a matrix to its lower right corner, each element of the matrix has a non-0 value, only three directions can move to right, down, to the right. The maximum value to reach the lower right corner.

at the beginning of the time to see the search direction to think, have been trying to get through AH. Later went to the internet to see a bit, the original use of the rules AH Ah

AC Code:

<span style= "FONT-SIZE:24PX;"

> #include <iostream> #include <cstring> #include <cstdio> using namespace std;
int a[1001][1001];

int Dp[1001][1001];//dp[i][j] is used to denote the maximum value of this position value in column J of row I.
    int max (int x,int y,int z) {if (x<y) swap (x, y);
    if (x<z) swap (x,z);
return x;
    } int main () {int t,i,j,m,n,r,c;
    scanf ("%d", &t);
        for (j=1; j<=t; J + +) {scanf ("%d%d", &r,&c);
            for (m=1; m<=r; m++) for (n=1; n<=c; n++) {scanf ("%d", &a[m][n]);
        } for (i=0;i<=r;i++) dp[i][0]=0;
        for (i=0;i<=c;i++) dp[0][i]=0; for (m=1;m<=r;m++) for (n=1;n<=c;n++) Dp[m][n]=max (dp[m][n-1],dp[m-1][n],dp[m-1][n-1]) +a[m][n]
        ;
    printf ("Scenario #%d:\n%d\n\n", j,dp[r][c]);//The topic says that each back should have an empty line, read the question to be careful, die here really is not worth ah.
} return 0; }</span><span style= "FONT-SIZE:18PX;" > </span> 

http://acm.hdu.edu.cn/showproblem.php?pid=2571

The main topic: Also gives a matrix, and the value of each element, and the difference is that the value here can be negative (for DP initialization to pay attention to), the direction of walking is down, to the right (the right can be a step or the position of the column K times).

This topic seems to be not much different from the above question, in fact, it is not very different, but there is a place to pay attention to (K).

This topic with the memory of the search words will be simple and convenient, but because I will not use ... Here is not posted on the memory of the search code, after learning to be sure to fill one.

AC Code:

<span style= "FONT-SIZE:24PX;" > #include <iostream> #include <cstring> #include <cstdio> #include <algorithm> using
namespace Std;
#define M-999999 int a[21][1005];

int dp[21][1005];

int max (int x,int y) {return x>y?x:y;}
    int main () {int t,r,c,i,j,k;
    scanf ("%d", &t);
        while (t--) {scanf ("%d%d", &r,&c);
        for (i=1;i<=r;i++) for (j=1;j<=c;j++) scanf ("%d", &a[i][j]);
        for (i=0;i<=r;i++) dp[i][0]=m;
        for (i=0;i<=c;i++) dp[0][i]=m;
        dp[0][1]=0;
        dp[1][0]=0; for (i=1;i<=r;i++) {for (j=1;j<=c;j++) {Dp[i][j]=max (dp[i-1][j
                ],DP[I][J-1]) +a[i][j];//a first-in-a-way comparison.
                k=2;
                   while (j/k>0)//Here is the point where you can jump from to that point.
               {if (j%k==0)//If you can jump Dp[i][j]=max (dp[i][j],dp[i][j/k]+a[i][j]);//Compare jump.     K+=1;
    }}} printf ("%d\n", Dp[r][c]);
} return 0; } </span>



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.