HDU 5569 matrix
来源:互联网 发布:sqlite数据库访问接口 编辑:程序博客网 时间:2024/05/16 05:30
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 isa1∗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 followsn lines with m numbers ai,j(1≤ai≤100)
For each cases, first come 2 integers,
N+m is an odd number.
Then follows
Output
For each cases, please output an integer in a line as the answer.
Sample Input
2 31 2 32 2 12 32 2 11 2 4
Sample Output
48
给一个n*m矩阵(n+m为奇数) 从(1,1)走到(n,m) 每次只能走右边和下边
走过的路会得到值 a1,a2,a3,...an
求 a1*a2 + a3*a4 + ... ak-1*ak 最小值
我用的dp+记忆化搜索
在走第奇数步的时候 dp[i][j] = min(dp[i][j+1]+mp[i][j]*mp[i][j+1], dp[i+1][j]+mp[i][j]*mp[i+1][j])
在走第偶数步的时候 dp[i][j] = min(dp[i][j+1], dp[i+1][j])
#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>#include <queue>#include <vector>#include <cmath>#include <stack>#include <string>#include <map>#include <set>#define pi acos(-1)#define LL long long#define ULL unsigned long long#define inf 0x3f3f3f3f#define INF 1e18#define lson l,mid,rt<<1#define rson mid+1,r,rt<<1|1using namespace std;typedef pair<int, int> P;const int maxn = 1e3 + 5;const int mod = 1e8;int mp[maxn][maxn];int dp[maxn][maxn];int dir[2][2] = {{1,0}, {0,1}};int n, m;int dfs(int x, int y, int cnt){ if (dp[x][y]) return dp[x][y]; if (x == n && y == m){ return 0; } int tmp, mni = inf; for (int i = 0; i < 2; i++){ int nx = x + dir[i][0]; int ny = y + dir[i][1]; if (nx > n || ny > m) continue; if (cnt % 2){ tmp = dfs(nx, ny, cnt+1); tmp = tmp + mp[x][y]*mp[nx][ny]; mni = min(mni, tmp); } else { tmp = dfs(nx, ny, cnt+1); mni = min(mni, tmp); } } dp[x][y] = mni; return dp[x][y];}int main(void){//freopen("C:\\Users\\wave\\Desktop\\NULL.exe\\NULL\\in.txt","r", stdin); int i, j, ans; while (cin >> n >> m){ memset(dp, 0, sizeof(dp)); for (i = 1; i <= n; i++) for (j = 1; j <= m; j++) scanf("%d", &mp[i][j]); ans = dfs(1, 1, 1); printf("%d\n", ans); }return 0;}
0 0
- HDU 5569 matrix
- HDU 5569 matrix(DP)
- hdu 5569 ***matrix
- hdu 5569 matrix(dp)
- hdu-5569-matrix-dp
- hdu 5569 matrix【dp】
- HDU 5569 matrix
- HDU-5569 matrix(DP)
- hdu 5569 matrix (dp)
- hdu 5569 matrix(dp)
- Matrix HDU
- hdu 3376 Matrix again
- hdu 4313 Matrix
- HDU 2119 Matrix
- HDU 2686 Matrix
- hdu 4313 Matrix
- hdu 2686 && 3376 Matrix
- HDU 4313 Matrix
- java学习之路 之 枚举类&注解-注解(Annotation)及练习题
- STM32 uart
- Android开发之实现最简单最酷炫的3D图片浏览效果(二)
- uva10048-Audiophobia-Floyd
- C++中静态成员变量与静态成员函数
- HDU 5569 matrix
- 今天写个关于应用程序界面隐藏标题栏,问题虽然很小,但很重要。
- 在 Cent OS 上使用YUM安装 MySQL 5.7 的正确姿势
- 使用C#开发管理信息系统
- [noip2001pj] 数的计算
- HDU 5461 Largest Point
- cookie 和session 的区别详解
- 【SQLite数据库小结】sql语句的两种形式以及你不能不知的事务
- 安卓控件之文本框(TextView)