HDU 5569 matrix(简单DP+递推)

matrix

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


 

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 a1∗a2+a3∗a4+...+a2k−1∗a2k. What is the minimum of the cost?

 

Input

Several test cases(about 5)

For each cases, first come 2 integers, n,m(1≤n≤1000,1≤m≤1000)

N+m is an odd number.

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

 

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

BestCoder Round #63 (div.2)

 

Recommend

hujie   |   We have carefully selected several similar problems for you:  6385 6384 6383 6382 6380 

#include<iostream>
#include<algorithm>
#include<string>
#include<map>//int dx[4]={0,0,-1,1};int dy[4]={-1,1,0,0};
#include<queue>//int gcd(int a,int b){return b?gcd(b,a%b):a;}
#include<vector>
#include<cmath>
#include<stack>
#include<string.h>
#include<stdlib.h>
#include<cstdio>
#define mod 1e9+7
#define ll unsigned long long
#define MAX 1000000000
#define ms memset
#define maxn 5005
#define INF 1e9
using namespace std;

int n,m;
int mp[maxn][maxn],dp[maxn][maxn];
/*
简单DP;
不要想记忆化歧途,
递推即可,按行数和列数的性质,
和为奇数则更新答案,和为偶数则延扩答案。
*/

int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        ///memset(d,0,sizeof(d));
        for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) scanf("%d",&mp[i][j]);
        dp[1][1]=0;
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
            {
                if(i==1&&j==1) continue;
                dp[i][j]=INF;
                if((i+j)&1)
                {
                    if(i-1>=1) dp[i][j]=min(dp[i][j],mp[i][j]*mp[i-1][j]+dp[i-1][j]);
                    if(j-1>=1) dp[i][j]=min(dp[i][j],mp[i][j-1]*mp[i][j]+dp[i][j-1]);
                }
                else
                {
                    if(i-1>=1) dp[i][j]=min(dp[i][j],dp[i-1][j]);
                    if(j-1>=1) dp[i][j]=min(dp[i][j-1],dp[i][j]);
                }
               /// cout<<dp[i][j]<<" ";
            }
            printf("%d\n",dp[n][m]);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_37451344/article/details/81612441