matrix---简单dp,边界边界-_-

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5569

简单dp,恶心的边界处理,无语;

if((i+j)%2==1)

dp[i][j]=a[i-1][j]*a[i][j]+min(dp[i-2][j], dp[i-1][j-1]);
dp[i][j]=min(dp[i][j], a[i][j-1]*a[i][j]+min(dp[i-1][j-1], dp[i][j-2]));

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<map>
#include<cmath>
#include<queue>
using namespace std;
#define N 1005
#define INF 0x3f3f3f3f

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

int main()
{
    while(scanf("%d %d", &m, &n)!=EOF)
    {
        for(int i=0; i<N; i++)
            for(int j=0; j<N; j++)
                dp[i][j]=INF;

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

        dp[1][1]=0;
        dp[1][2]=a[1][1]*a[1][2];
        dp[2][1]=a[1][1]*a[2][1];
        for(int i=3; i<=m; i++)
        {
            dp[i][1]=dp[i-2][1]+a[i][1]*a[i-1][1];
            dp[i][2]=min(a[i][1]*a[i][2]+dp[i-1][1],a[i-1][2]*a[i][2]+min(dp[i-1][1], dp[i-2][2]));
        }
        for(int i=3; i<=n; i++)
        {
            dp[1][i]=dp[1][i-2]+a[1][i]*a[1][i-1];
            dp[2][i]=min(a[1][i]*a[2][i]+dp[1][i-1],a[2][i-1]*a[2][i]+min(dp[1][i-1], dp[2][i-2]));
        }

        for(int i=3; i<=m; i++)
        {
            for(int j=3; j<=n; j++)
            {
                if((i+j)%2==1)
                {
                    dp[i][j]=a[i-1][j]*a[i][j]+min(dp[i-2][j], dp[i-1][j-1]);
                    dp[i][j]=min(dp[i][j], a[i][j-1]*a[i][j]+min(dp[i-1][j-1], dp[i][j-2]));
                }
            }
        }
        printf("%d\n", dp[m][n]);
    }
    return 0;
}
/*
3 4
1 2 3 7
4 5 6 8
10 11 12 9

92
*/

时间: 2024-08-08 01:29:30

matrix---简单dp,边界边界-_-的相关文章

hdu 5569 matrix(简单dp)

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

POJ 3250 Bad Hair Day 简单DP 好题

Description Some of Farmer John's N cows (1 ≤ N ≤ 80,000) are having a bad hair day! Since each cow is self-conscious about her messy hairstyle, FJ wants to count the number of other cows that can see the top of other cows' heads. Each cow i has a sp

hdu1207 汉诺塔II 简单dp

本文出自:http://blog.csdn.net/svitter 题意:汉诺塔,多了一根柱子,问你寻找最快的移动次数. dp [ n ] = dp [ n - j ] * 2 + pow( 2, j ) - 1; 就是把j个汉诺塔移到一根上dp[ n - j ],然后就是普通的汉诺塔问题,即2^n - 1次移动, 然后把剩下的移动过去dp [ n - j ]. 注意pow(2, j )可能超出long long int范围.写二的次方的时候也可用移位算法. #include <iostream

POJ1088:滑雪(简单dp)

题目链接:  http://poj.org/problem?id=1088 题目要求: 一个人可以从某个点滑向上下左右相邻四个点之一,当且仅当高度减小.求可以滑落的最长长度. 题目解析: 首先要先排一下序,因为只能高度递减才能滑行.之后就很简单了,就是简单DP. 即:要求的滑坡是一条节点递减并依次相邻的最长路径,可以先根据高度将所有的点进行排序,在i点的时候,遍历0~i-1个点(升序排序,i前面的点的高度一定小于等于i),取相邻点间的大的路径长度 代码如下: #include <iostream

hdu 1087 简单dp

思路和2391一样的.. <span style="font-size:24px;">#include<stdio.h> #include<string.h> #include<iostream> #include<algorithm> using namespace std; const int inf=(0x7f7f7f7f); int main() { int a; int s[10005]; int w[10005];

hdu1087 简单DP

I - 简单dp 例题扩展 Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Description Nowadays, a kind of chess game called “Super Jumping! Jumping! Jumping!” is very popular in HD

2014 HDU多校弟九场I题 不会DP也能水出来的简单DP题

听了ZWK大大的思路,就立马1A了 思路是这样的: 算最小GPA的时候,首先每个科目分配到69分(不足的话直接输出GPA 2),然后FOR循环下来使REMAIN POINT减少,每个科目的上限加到100即可 算最大GPA的时候,首先每个科目分配到60分,然后FOR循环下来使REMAIN POINT减少,每个科目的上限加到85即可,如果还有REMAIN POINT,就FOR循环下来加到100上限即可 不会DP 阿 QAQ 过段时间得好好看DP了  =  = 于是默默的把这题标记为<水题集> //

HDU 2119 Matrix 简单二分匹配

行做x集,列做y集,1就给该行该列连一条边,输出最大匹配边即可 #include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #include<vector> #include<set> using namespace std; #define N 105 int lef[N], pn;//lef[v]表示Y集的点v 当前连接的点 , pn为x点集的

Codeforces 41D Pawn 简单dp

题目链接:点击打开链接 给定n*m 的矩阵 常数k 下面一个n*m的矩阵,每个位置由 0-9的一个整数表示 问: 从最后一行开始向上走到第一行使得路径上的和 % (k+1) == 0 每个格子只能向或走一步 求:最大的路径和 最后一行的哪个位置作为起点 从下到上的路径 思路: 简单dp #include <cstdio> #include <algorithm> #include<iostream> #include<string.h> #include &