思路:dp[i][j]表示到这里的最小和。然后就是分奇偶讨论了。
// #pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <sstream>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <vector>
#include <map>
#include <set>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <limits.h>
// #define DEBUG
#ifdef DEBUG
#define debug(...) printf( __VA_ARGS__ )
#else
#define debug(...)
#endif
#define MEM(x,y) memset(x, y,sizeof x)
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> ii;
const LL inf = 1LL << 60;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
int n, m;
int A[1010][1010];
int step[110][110];
LL dp[1010][1010];
int main()
{
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
while(scanf("%d%d",&n,&m) != EOF){
for (int i = 1;i <= n;++i)
for (int j = 1;j <= m;++j)
scanf("%d",&A[i][j]);
for (int i = 0;i <= n + 1;++i)
for (int j = 0;j <= m + 1;++j)
dp[i][j] = inf;
dp[0][1] = dp[1][0] = 0;
dp[1][1] = 0;
for (int i = 1;i <= n;++i){
for (int j = 1;j <= m;++j){
if ((i+j)%2){
dp[i][j] = min(dp[i-1][j] + 1LL*A[i-1][j]*A[i][j], dp[i][j]);
dp[i][j] = min(dp[i][j-1] + 1LL*A[i][j-1]*A[i][j], dp[i][j]);
}else dp[i][j] = min(dp[i][j-1], dp[i-1][j]);
}
}
printf("%lld\n", dp[n][m]);
}
return 0;
}