matrix
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 304 Accepted Submission(s): 187
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)
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[i][j]表示第i, j位置的贡献, 当时不会处理dp数组和矩阵. (i + j)为奇数时, 进行状态转移即可.
AC代码:
#include "iostream"
#include "cstdio"
#include "cstring"
#include "algorithm"
#include "cmath"
#include "utility"
#include "map"
#include "set"
#include "vector"
#include "list"
using namespace std;
typedef long long ll;
const int MOD = 1e9 + 7;
const int MAXN = 1005;
int n, m;
ll maze[MAXN][MAXN], dp[MAXN][MAXN];
int main(int argc, char const *argv[])
{
while(scanf("%d%d", &n, &m) != EOF) {
memset(dp, 0, sizeof(dp));
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= m; ++j)
scanf("%lld", &maze[i][j]);
dp[1][2] = maze[1][1] * maze[1][2], dp[2][1] = maze[1][1] * maze[2][1];
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= m; ++j) {
if((i + j) & 1) {
if(j > 2 && dp[i][j - 2]) {
if(dp[i][j]) dp[i][j] = min(dp[i][j], dp[i][j - 2] + maze[i][j] * maze[i][j - 1]);
else dp[i][j] = dp[i][j - 2] + maze[i][j] * maze[i][j - 1];
}
if(i > 2 && dp[i - 2][j]) {
if(dp[i][j]) dp[i][j] = min(dp[i][j], dp[i - 2][j] + maze[i][j] * maze[i - 1][j]);
else dp[i][j] = dp[i - 2][j] + maze[i][j] * maze[i - 1][j];
}
if(i > 1 && j > 1 && dp[i - 1][j - 1]) {
if(dp[i][j]) dp[i][j] = min(dp[i][j], dp[i - 1][j - 1] + min(maze[i][j - 1] * maze[i][j], maze[i - 1][j] * maze[i][j]));
else dp[i][j] = dp[i - 1][j - 1] + min(maze[i][j - 1] * maze[i][j], maze[i - 1][j] * maze[i][j]);
}
}
}
printf("%lld\n", dp[n][m]);
}
return 0;
}