POJ 2948 DP, poj2948dp

Source: Internet
Author: User

POJ 2948 DP, poj2948dp


A row * col matrix. Each grid has two kinds of mines, yeyenum and bloggium, and knows the number of them in each grid. The collection site with bloggium in the Northern Region and yeyenum in the western region. Now you need to install a conveyor belt to the North or to the west on these grids (each grid can be fitted with one type ). Ask the maximum number of mines that can be collected.


DP, the state transition equation is

Dp [I] [j] = Max (dp [I] [J-1] + suma [I] [j], dp [I-1] [j] + sumb [I] [j]);

Here, sumb [I] [j] is the value of the ore from Column 1 to column j of row I to the west, suma [I] [j] is the value of the ore from Column 1 to line I that needs to reach the Far North.


#include "stdio.h"#include "string.h"int dp[510][510],suma[510][510],sumb[510][510],a[510][510],b[510][510];int Max(int a,int b ){    if (a<b) return b;else return a;}int main(){    int n,m,i,j;    while (scanf("%d%d",&n,&m)!=EOF)    {        if (n+m==0)break;        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=1;j<=m;j++)            scanf("%d",&a[i][j]);        memset(suma,0,sizeof(suma));        memset(sumb,0,sizeof(sumb));        for (i=1;i<=n;i++)            for (j=1;j<=m;j++)            suma[i][j]=suma[i-1][j]+a[i][j];        for (j=1;j<=m;j++)            for (i=1;i<=n;i++)            sumb[i][j]=sumb[i][j-1]+b[i][j];        memset(dp,0,sizeof(dp));        for (i=1;i<=n;i++)            for (j=1;j<=m;j++)            {                dp[i][j]=Max(dp[i][j-1]+suma[i][j],dp[i-1][j]+sumb[i][j]);            }        printf("%d\n",dp[n][m]);    }    return 0;}



POJ 1004 helps you to see why answer wrong cannot be found.

Replace void main with int main, and delete the void in the brackets of the main function. You do not need to use any parameters, and add return 0. void main is prone to problems, the last printf should be %. 2f. You can change it. If not, replace all float with double and f with lf.

Let me see why POJ1458 is old WA?

Changed. Logic error.

/*
Peking University ACM-ICPC Online Judge, Pro.1458
LCS longest Public String Dynamic Programming Algorithm
*/
# Include <cstdio>
# Include <memory>
# Include <cstdlib>
# Define MAX (a, B) (a> B? A: B)
Using namespace std;
Const int maxLenth = 1000;
Char s1 [maxLenth], s2 [maxLenth];
Int DP [maxLenth] [maxLenth];
Int LCS (char *, char *);
Int main (int, char **)
{
// Freopen ("1458in.txt", "r", stdin );
While (scanf ("% s", s1, s2)> 0)
{
Printf ("% d \ n", LCS (s1, s2 ));
}
Return 0;
}
Int LCS (char * s1, char * s2)
{
Int I, j;
Int len1, len2;
Memset (DP, 0, sizeof (DP ));
For (I = 0; s1 [I]; I ++)
For (j = 0; s2 [j]; j ++)
If (s1 [I] = s2 [j])
DP [I] [j] = 1;
Len1 = I; len2 = j;
For (I = 0; I <len1; I ++)
For (j = 0; j <len2; j ++)
{
Int temp1, temp2, temp3;
Temp1 = j> 0? DP [I] [J-1]: 0;
Temp2 = I> 0? DP [I-1] [j]: 0;
Temp3 = (I> 0 & j> 0 )? DP [I-1] [J-1]: 0;
If (DP [I] [j])
DP [I] [j] = temp3 + 1;
Else
DP [I] [j] = MAX (temp1, temp2) + DP [I] [j];
}
Return DP [len1-1] [len2-1];
}

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.