5408. 保险箱 - AcWing题库
#include <iostream>
#include <string>
#include <algorithm>
#include <cstring>
using namespace std;const int N = 1e5 + 10;
string x, y;
int f[N][3], n;
int main() {cin >> n >> x >> y;memset(f, 0x3f, sizeof f);f[n][1] = 0;for (int i = n - 1; i >= 0; i--) {for (int j = 0; j < 3; j++) {for (int t = 0; t < 3; t++) {f[i][j] = min(f[i][j], f[i + 1][t] + abs(x[i] - 10 * (j - 1) + t - 1 - y[i]));}}}cout << min({ f[0][0], f[0][1], f[0][2] });return 0;
}