zoukankan      html  css  js  c++  java
  • 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
    ", solve());
    63     }
    64 }
    View Code
  • 相关阅读:
    ssh框架下 写简单的hql语句
    onclick事件 在使用模板填充情况下 向后台传递多值
    调用 sendResponseMsg 遇到的问题
    ERP项目有关时间的修改和查看的显示,去掉时分秒
    ERP中select的填充方法
    最简单的jQuery ajax请求
    ERP中默认申请人和申请部门
    list 按元素的某字段排序方法。作者:黄欣
    C# 对象、文件与二进制串(byte数组)之间的转换【转载】
    .net framework(4.6.2) 迁移 .net core(2.2) 总结
  • 原文地址:https://www.cnblogs.com/oyking/p/3350633.html
Copyright © 2011-2022 走看看