matrix

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

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
 

题解:dp题,发现自己对于dp太弱了。。。

题意是一个n*m的矩阵,从(1,1)走到(n,m)的a1*a2+a3*a4+a5*a6。。。。其中a1,a2。。。是矩阵的当前值;

由于只能向右向下,从(1,1)开始,那么(x+1,y)或(x,y+1)由于是从1+1=2偶数开始,所以到x+y+1是奇数的时候再进行运算,

所以每次往前推两步即可,初始化dp为INF,因为求最小值,dp[1][1]=0;状态转移方程:

if((i+j)&1){
    dp[i][j]=min(dp[i-1][j]+num[i-1][j]*num[i][j],dp[i][j-1]+num[i][j-1]*num[i][j]);
   }
   else dp[i][j]=min(dp[i-1][j],dp[i][j-1]);

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define mem(x,y) memset(x,y,sizeof(x))
using namespace std;
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0);
const int MAXN=1010;
int dp[MAXN][MAXN],num[MAXN][MAXN];
int main(){
int n,m;
while(~scanf("%d%d",&n,&m)){
mem(dp,INF);
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++){
scanf("%d",&num[i][j]);
if(i==1&&j==1){
dp[i][j]=0;continue;
}
if((i+j)&1){
dp[i][j]=min(dp[i-1][j]+num[i-1][j]*num[i][j],dp[i][j-1]+num[i][j-1]*num[i][j]);
}
else dp[i][j]=min(dp[i-1][j],dp[i][j-1]);
}
printf("%d\n",dp[n][m]);
}
return 0;
}

  

matrix(dp)的更多相关文章

  1. [CSP-S模拟测试]:matrix(DP)

    题目描述 求出满足以下条件的$n\times m$的$01$矩阵个数:(1)第$i$行第$1~l_i$列恰好有$1$个$1$.(2)第$i$行第$r_i~m$列恰好有$1$个$1$.(3)每列至多有$ ...

  2. CSP模拟赛 Matrix(DP)

    题面 求出满足以下条件的 n*m 的 01 矩阵个数: (1)第 i 行第 1~li 列恰好有 1 个 1. (2)第 i 行第 ri~m 列恰好有 1 个 1. (3)每列至多有 1 个 1. n, ...

  3. hihocoder #1580 : Matrix (DP)

    #1580 : Matrix 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 Once upon a time, there was a little dog YK. On ...

  4. 牛客网多校训练第一场 B - Symmetric Matrix(dp)

    链接: https://www.nowcoder.com/acm/contest/139/B 题意: 求满足以下条件的n*n矩阵A的数量模m:A(i,j) ∈ {0,1,2}, 1≤i,j≤n.A(i ...

  5. 【POJ 3071】 Football(DP)

    [POJ 3071] Football(DP) Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4350   Accepted ...

  6. LightOJ 1033 Generating Palindromes(dp)

    LightOJ 1033  Generating Palindromes(dp) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid= ...

  7. lightOJ 1047 Neighbor House (DP)

    lightOJ 1047   Neighbor House (DP) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87730# ...

  8. UVA11125 - Arrange Some Marbles(dp)

    UVA11125 - Arrange Some Marbles(dp) option=com_onlinejudge&Itemid=8&category=24&page=sho ...

  9. Leetcode#867. Transpose Matrix(转置矩阵)

    题目描述 给定一个矩阵 A, 返回 A 的转置矩阵. 矩阵的转置是指将矩阵的主对角线翻转,交换矩阵的行索引与列索引. 示例 1: 输入:[[1,2,3],[4,5,6],[7,8,9]] 输出:[[1 ...

随机推荐

  1. Spring Boot Admin Reference Guide

    1. What is Spring Boot Admin? Spring Boot Admin is a simple application to manage and monitor your S ...

  2. elasearch 版本控制

    http://192.168.32.81:9200/library/books/8/ GET { "_index": "library", "_typ ...

  3. 由世纪互联运营的 Windows Azure 现已在中国正式发布

     我们非常高兴地公开发布由世纪互联运营的 Windows Azure,这标志着我们成为第一家在中国国内正式提供公共云平台技术的跨国公司.这一伟大成就的实现,得益于 Microsoft 与世纪互联的 ...

  4. VS2010/MFC:模态对话框及其弹出过程

    模态对话框及其弹出过程 加法计算器对话框程序大家照着做一遍后,相信对基于对话框的程序有些了解了,有个好的开始对于以后的学习大有裨益.趁热打铁,这一节讲讲什么是模态对话框和非模态对话框,以及模态对话框怎 ...

  5. redis研究笔记

    本文链接:http://blog.csdn.net/u012150179/article/details/38077851 一. redis Redis is an in-memory databas ...

  6. python下module、package导入

    #encoding=utf-8"""模块:1.import demo #导入demo.py下的所有的函数,调用方法为:demo.function()2.from demo ...

  7. C++模板编程

    如何处理函数模板中的函数体? 预备知识补充: 按照c++的语言系统,普通函数及类的声明应该放在一个头文件中(通常是.h. .hpp..hh为扩展名)里: 而将其实现放在一个主代码文件中(通常以.c . ...

  8. SharePoint需要开启的网站集功能

    1. 管理网站功能 2. 网站集功能

  9. 【QT相关】文件、目录基础操作

    判断目录是否存在: QString proFile(t_path); proFile.append("/dir"); QFileInfo proFileInfo(proFile); ...

  10. muduo简化(1):Reactor的关键结构

    说明:本文参照muduo代码,主要用意是简化muduo代码呈现其主要结构,并脱离muduo的文件依赖. 本节简化的是Reactor的关键结构部分:EventLoop.Poller.Channel.遵照 ...