怎么今天写啥题都不会写啊, 我是傻了吗。。
把电梯里面四个人的目标点当作状态, 然后暴力转移。
#include<bits/stdc++.h> #define LL long long #define fi first #define se second #define mk make_pair #define PLL pair<LL, LL> #define PLI pair<LL, int> #define PII pair<int, int> #define SZ(x) ((int)x.size()) #define ull unsigned long long using namespace std; const int N = 2000 + 7; const int inf = 0x3f3f3f3f; const LL INF = 0x3f3f3f3f3f3f3f3f; const int mod = 1e9 + 7; const double eps = 1e-8; const double PI = acos(-1); int dp[2][10][10][10][10][10]; int n, a[N], b[N]; int cur = 0, lst = 1; inline bool chkmin(int &a, int b) { return a > b ? a = b, true : false; } int main() { scanf("%d", &n); for(int i = 1; i <= n; i++) scanf("%d%d", &a[i], &b[i]); memset(dp[cur], inf, sizeof(dp[cur])); dp[cur][1][0][0][0][0] = 2 * n; for(int i = 0; i <= n; i++) { swap(cur, lst); memset(dp[cur], inf, sizeof(dp[cur])); for(int c1 = 9; c1 >= 0; c1--) { for(int c2 = 9; c2 >= 0; c2--) { for(int c3 = 9; c3 >= 0; c3--) { for(int c4 = 9; c4 >= 0; c4--) { for(int f = 1; f <= 9; f++) { int& ret = dp[lst][f][c1][c2][c3][c4]; if(ret >= inf) continue; if(!c1 && i < n) { chkmin(dp[cur][a[i + 1]][b[i + 1]][c2][c3][c4], ret + abs(f - a[i + 1])); } else if(c1) { chkmin(dp[lst][c1][0][c2][c3][c4], ret + abs(f - c1)); } if(!c2 && i < n) { chkmin(dp[cur][a[i + 1]][c1][b[i + 1]][c3][c4], ret + abs(f - a[i + 1])); } else if(c2) { chkmin(dp[lst][c2][c1][0][c3][c4], ret + abs(f - c2)); } if(!c3 && i < n) { chkmin(dp[cur][a[i + 1]][c1][c2][b[i + 1]][c4], ret + abs(f - a[i + 1])); } else if(c3) { chkmin(dp[lst][c3][c1][c2][0][c4], ret + abs(f - c3)); } if(!c4 && i < n) { chkmin(dp[cur][a[i + 1]][c1][c2][c3][b[i + 1]], ret + abs(f - a[i + 1])); } else if(c4) { chkmin(dp[lst][c4][c1][c2][c3][0], ret + abs(f - c4)); } } } } } } } int ans = inf; for(int i = 1; i <= 9; i++) ans = min(ans, dp[lst][i][0][0][0][0]); printf("%d\n", ans); return 0; } /* */