HDOJ 5569 matrix (DP)
2015-11-23 20:57
357 查看
matrix
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 340 Accepted Submission(s): 208
[align=left]Problem Description[/align]
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?
[align=left]Input[/align]
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)
[align=left]Output[/align]
For each cases, please output an integer in a line as the answer.
[align=left]Sample Input[/align]
2 3
1 2 3
2 2 1
2 3
2 2 1
1 2 4
[align=left]Sample Output[/align]
4
8
题意:应该都能看懂,就不再翻译了
思路:刚开始看,没想到dp,写完才发现,因为dp弱,所以推了好久,因为只能向右或者向下,所以下一步的状态是由上一步来的,dp数组记录了到达那个点的ans最小值,然后一步一步推,相当于记忆化,在起点的时候该点dp值为0,周围边界直接处理为INF。
在奇数步的时候:转移方程为:dp[i][j]=min(dp[i-1][j],dp[i][j-1]);
在偶数步的时候:转移方程为:dp[i][j]=min(dp[i-1][j]+map[i-1][j]*map[i][j],dp[i][j-1]+map[i][j-1]*map[i][j]);
ac代码:
#include<stdio.h> #include<string.h> #include<math.h> #include<iostream> #include<algorithm> #define MAXN 10000001 #define fab(a) (a)>0?(a):(-a) #define INF 0xfffffff #define LL long long #define mem(x) memset(x,0,sizeof(x)) using namespace std; int map[1010][1010]; LL dp[1010][1010]; int n,m; int main() { int i,j; while(~scanf("%d%d",&n,&m)) { for(i=1;i<=n;i++) { for(j=1;j<=m;j++) scanf("%d",&map[i][j]); } for(i=0;i<=n+1;i++) { for(j=0;j<=m+1;j++) dp[i][j]=INF; } dp[1][1]=0; for(i=1;i<=n;i++) { for(j=1;j<=m;j++) { if(i==1&&j==1) continue; if((i+j)%2==0) dp[i][j]=min(dp[i-1][j],dp[i][j-1]); else dp[i][j]=min(dp[i-1][j]+map[i-1][j]*map[i][j],dp[i][j-1]+map[i][j-1]*map[i][j]); } } printf("%lld\n",dp [m]); } return 0; }
相关文章推荐
- 互联网技术发展之路(5)- 开发层技术剖析
- objective-C 多态
- 将string转换成char* (转)
- 1013. 数素数 (20)
- 互联网技术发展之路(4)- 存储层技术剖析
- c++之栈的顺序表实现
- REDIS源码中一些值得学习的技术细节01
- Linux 技巧:让进程在后台可靠运行的几种方法
- hdu 1062 Text Reverse
- 10+年程序员总结的20+条经验教训
- Ubuntu安装DGE-530T网卡驱动
- hdoj 4786 Fibonacci Tree
- C语言初学者画图练习
- PHP关系型数据库介绍
- git 删除错误提交的commit
- 【AMPPZ2014】【BZOJ4144】Petrol
- android面试重点(一)ListView的优化
- 排序算法-快速排序
- 一种导致UITextView输入中文却先输入拼音的解决思路
- Java中的I/O流复习