(动态规划)matrix -- hdu -- 5569

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

 

matrix

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 325    Accepted Submission(s): 196


Problem Description
Given a matrix with n rows and m columns ( n+m is an odd number ), at first , you begin with the number at top-left corner (1,1) and you want to go to the number at bottom-right corner (n,m). And you must go right or go down every steps. Let the numbers you go through become an array a1,a2,...,a2k. The cost is a1a2+a3a4+...+a2k1a2k. What is the minimum of the cost?
 

 

Input
Several test cases(about 5)

For each cases, first come 2 integers, n,m(1n1000,1m1000)

N+m is an odd number.

Then follows n lines with m numbers ai,j(1ai100)
 

 

Output
For each cases, please output an integer in a line as the answer.
 

 

Sample Input
2 3
1 2 3
2 2 1
2 3
2 2 1
1 2 4
 

 

Sample Output
4
8
 

 

Source

 

 写完这题, 我意识到了动态规划最重要的便是状态转移, 虽然以前听别人说, 但是到底没有自己真正体会到的来的贴切

 

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<queue>
using namespace std;

const int N = 1100;

int a[N][N], dp[N][N];

int main()
{
    int n, m, i, j;

    while(scanf("%d%d", &n, &m)!=EOF)
    {
        for(i=1; i<=n; i++)
        for(j=1; j<=m; j++)
            scanf("%d", &a[i][j]);

        memset(dp, 0x3f3f3f3f, sizeof(dp));

        for(i=1; i<=n; i++)
        for(j=1; j<=m; j++)
        {
            if(i==1 && j==1)
                dp[i][j] = 0;
            else if((i+j)&1)
            {
                dp[i][j] = min(dp[i-1][j]+a[i-1][j]*a[i][j], dp[i][j-1]+a[i][j-1]*a[i][j]);
            }
            else
                dp[i][j] = min(dp[i-1][j], dp[i][j-1]);
        }

        printf("%d
", dp[n][m]);
    }
    return 0;
}

 

勿忘初心
原文地址:https://www.cnblogs.com/YY56/p/4988538.html