解题思路:
水题,直接DP。
#include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> #include <algorithm> #include <cmath> #include <vector> #include <queue> #include <stack> #include <set> #include <map> #define LL long long #define FOR(i,x,y) for(int i=x;i<=y;i++) using namespace std; const int INF = 0x3f3f3f3f; int A[25][15]; int dp[25][15]; int N, K; int main() { while(scanf("%d%d", &N, &K)!=EOF) { FOR(i,1,N) FOR(j,1,K) scanf("%d", &A[i][j]); memset(dp,INF,sizeof(dp)); FOR(j,1,K) dp[1][j] = 0; FOR(i,2,N) { FOR(j,1,K) { FOR(l,1,K) { dp[i][j] = min(dp[i][j], dp[i-1][l] + abs(A[i][j] - A[i-1][l])); } } } int ans = INF; FOR(i,1,K) ans = min(ans ,dp[N][i]); printf("%d\n", ans); } return 0; }
原文地址:http://blog.csdn.net/moguxiaozhe/article/details/43320929