HDU 4433 locker(DP)(2012 Asia Tianjin Regional Contest)

Problem Description
A password locker with N digits, each digit can be rotated to 0-9 circularly.
You can rotate 1-3 consecutive digits up or down in one step.
For examples:
567890 -> 567901 (by rotating the last 3 digits up)
000000 -> 000900 (by rotating the 4th digit down)
Given the current state and the secret password, what is the minimum amount of steps you have to rotate the locker in order to get from current state to the secret password?
 
Input
Multiple (less than 50) cases, process to EOF.
For each case, two strings with equal length (≤ 1000) consists of only digits are given, representing the current state and the secret password, respectively.
 
Output
For each case, output one integer, the minimum amount of steps from the current state to the secret password.

 

题目大意:从一串数字转成另一串数字,每次操作可以把连续的1~3个数字都+1或者-1(9+1=0,0-1=9),问最少须要多少次操作。

思路:DP。dp[i][x][y]表示,把第一个字符串变为第 i 位是 x,第 i-1 位是 y, i-1前面的全部都与第二个字符串相同,最少须要多少步。

假设第一串数字是a[],第二串数字是b[]

那么状态转移就是,对于每一个dp[i][x][y],判断从a[i]须要转多少步操作才能到x(两种操作都要枚举),设为k,然后枚举 i-1 跟着转多少步才能到达 y,设为p(p≤k),再枚举 i-2 须要跟着转多少步才能到达b[i],设为q(q≤p)。详见代码。

最终答案为dp[n][b[n]][b[n-1]]

 

代码(203MS):

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <algorithm>
 4 #include <cstring>
 5 using namespace std;
 6 typedef long long LL;
 7 
 8 const int MAXN = 1010;
 9 
10 int a[MAXN], b[MAXN], n;
11 int dp[MAXN][11][11];
12 char s[MAXN];
13 
14 inline int make(int x) {
15     if(x < 0) x += 10;
16     if(x > 9) x -= 10;
17     return x;
18 }
19 
20 inline void update_min(int &a, const int &b) {
21     if(a > b) a = b;
22 }
23 
24 inline int calc(int x, int y) {
25     if(x > y) y += 10;
26     return y - x;
27 }
28 
29 int solve() {
30     memset(dp, 0x3f, sizeof(dp));
31     dp[1][a[1]][0] = 0;
32     for(int i = 1; i <= 9; ++i) {
33         update_min(dp[1][make(a[1] + i)][0], i);
34         update_min(dp[1][make(a[1] - i)][0], i);
35     }
36     for(int i = 2; i <= n; ++i) {
37         for(int x = 0; x <= 9; ++x) {
38             for(int y = 0; y <= 9; ++y) {
39                 int k = calc(a[i], x);
40                 for(int p = 0; p <= k; ++p) {
41                     for(int q = 0; q <= p; ++q)
42                         update_min(dp[i][x][y], dp[i - 1][make(y - p)][make(b[i - 2] - q)] + k);
43                 }
44 
45                 k = 10 - k;
46                 for(int p = 0; p <= k; ++p) {
47                     for(int q = 0; q <= p; ++q)
48                         update_min(dp[i][x][y], dp[i - 1][make(y + p)][make(b[i - 2] + q)] + k);
49                 }
50             }
51         }
52     }
53     return dp[n][b[n]][b[n - 1]];
54 }
55 
56 int main() {
57     while(scanf("%s", s) != EOF) {
58         n = strlen(s);
59         for(int i = 0; i < n; ++i) a[i + 1] = s[i] - '0';
60         scanf("%s", s);
61         for(int i = 0; i < n; ++i) b[i + 1] = s[i] - '0';
62         printf("%d\n", solve());
63     }
64 }
View Code

 

转载于:https://www.cnblogs.com/oyking/p/3350633.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值