hdoj matrix 5569 (奇偶DP) 好题 向右下走取使方程值最小
2015-11-21 22:33
218 查看
matrix
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 107 Accepted Submission(s): 73
[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
#include<stdio.h> #include<string.h> int min(int x,int y) { return x>y?y:x; } int a[1010][1010]; int dp[1010][1010]; 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,0x3f3f3f,sizeof(dp)); for(i=1;i<=n;i++) { for(j=1;j<=m;j++) { 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]);//偶数步时,取向右走,与向下走的最小值。 if(i==1&&j==1) dp[i][j]=0; } } printf("%d\n",dp [m]); } return 0; }
相关文章推荐
- 计算机网络---基础题目汇总一
- linux中 find命令的总结
- Cython: 快速入门
- 文件上传一之form提交
- Chrome postman插件用来测试http 接口
- 做一个有价值的软件开发者
- App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Te
- 服务不支持chkconfig的解决
- Bootstrap 网格系统(Grid System)
- avrisp mkii问题
- Webkit之JS/C++互调关系
- HDU-5568 sequence2(DP+高精度)
- nginx 练习配置
- 简介Linux软件安装卸载工具
- java assert 使用
- 字符串查找-1
- 编译速度
- Intent页面跳转
- HDU5569/BestCoder Round #63 (div.2) C.matrix DP
- IO模型