Bupt 202 Chocolate Machine Dynamic planning

Source: Internet
Author: User

The 2011 Bupt Multi-School joint game H question

Bupt 202 Chocolate Mashine

The old OJ of the school to hang the agent to go on so here is a link to a hust on the topic:

Http://acm.hust.edu.cn/vjudge/contest/view.action?cid=70168#problem/J

Are counted from 1 onwards

DP[I][J] Indicates the maximum value of money in the bottom-right corner of the chocolate line I, J Liecheg

Small cells with a value less than K "block"

if (I, J) itself is "blocking" then dp[i][j] = 0

if (i, j) both the upper and the left have "block" then dp[i][j] = A[i][j]

If only one of the above blocks is "block" then dp[i][j] = from the current grid to the left plus until you encounter a boundary or "block"

If only the left one block is "blocking" then dp[i][j] = starts with the current grid and adds to the top until it encounters a boundary or "block"

If both upper and left are not blocked then dp[i][j] = Dp[i-1][j] + dp[i][j-1]-dp[i-1][j-1] + a[i][j]

And then constantly update the maximum value in Dp[i][j]

It's not hard to follow my line of thought is more cumbersome WA 4 cannon tat

#include <cstdio>#include<cstdlib>#include<ctime>#include<iostream>#include<cmath>#include<cstring>#include<algorithm>#include<stack>#include<Set>#include<queue>#include<vector>using namespaceStd;typedefLong Longll;Const intMAXN =3010; ll DP[MAXN][MAXN];intA[MAXN][MAXN];intMain () {//freopen ("In.txt", "R", stdin);    intT; scanf ("%d", &T);  while(t--)    {        intN, M, K; scanf ("%d%d%d", &n, &m, &k);  for(inti =1; I <= N; i++)             for(intj =1; J <= M; J + +) scanf ("%d", &A[i][j]); ll ans=0;  for(inti =1; I <= N; i++)             for(intj =1; J <= M; J + +)            {                if(A[i][j] <k) Dp[i][j]=0; Else                {                    if(dp[i-1][J] = =0&& dp[i][j-1] ==0) Dp[i][j]=A[i][j]; Else if(I-1>=1&& dp[i-1][J] = =0) {Dp[i][j]=A[i][j]; intt =1;  while(J-t >=1&& DP[I][J-T]! =0) {Dp[i][j]+ = a[i][j-T]; T++; }                    }                    Else if(J-1>=1&& dp[i][j-1] ==0) {Dp[i][j]=A[i][j]; intt =1;  while(I-t >=1&& Dp[i-t][j]! =0) {Dp[i][j]+ = a[i-T]                            [j]; T++; }                    }                    Else{Dp[i][j]= dp[i-1][J] + dp[i][j-1]-dp[i-1][j-1] +A[i][j]; }                }                if(Dp[i][j] >ans) ans=Dp[i][j]; } printf ("%i64d\n", ans); }    return 0;}

Bupt 202 Chocolate Machine Dynamic planning

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.