zoukankan      html  css  js  c++  java
  • uva 1631

    1631 Locker

    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.

    Sample Input

    111111 222222

    896521 183995

    Sample Output

    2

    12

    记忆化搜索

    从第一个开始,枚举向上或向下转1个,2个,3个的情况,然后记忆化搜索,注意最后一个字符在处理时要使第n + 1,n + 2个的初始和末状态相同。

    #include <cstdio>
    #include <iostream>
    #include <sstream>
    #include <cmath>
    #include <cstring>
    #include <cstdlib>
    #include <string>
    #include <vector>
    #include <map>
    #include <set>
    #include <queue>
    #include <stack>
    #include <algorithm>
    using namespace std;
    #define INF 100000
    #define ll long long
    #define _cle(m, a) memset(m, a, sizeof(m))
    #define repu(i, a, b) for(int i = a; i < b; i++)
    #define MAXN 1005
    char sa[MAXN], sb[MAXN];
    int a[MAXN], b[MAXN];
    int d[MAXN][10][10];
    int n;
    
    int up(int s, int t)
    {
        if(s <= t) return t - s;
        return (10 + t - s);
    }
    
    int down(int s, int t)
    {
        if(s >= t) return s - t;
        return (10 + s - t);
    }
    
    int dp(int d1, int d2, int d3)
    {
        if(d1 >= n) return 0;
        if(d[d1][d2][d3] != -1) return d[d1][d2][d3];
        int t, minn = INF;
        t = up(d2, b[d1]);
        repu(i, 0, t + 1)
          repu(j, i, t + 1)
           minn = min(minn, dp(d1 + 1, (d3 + j) % 10, (a[d1 + 2] + i) % 10) + t);
        t = down(d2, b[d1]);
        repu(i, 0, t + 1)
          repu(j, i, t + 1)
           minn = min(minn, dp(d1 + 1, (d3 - j + 10) % 10, (a[d1 + 2] - i + 10) % 10) + t);
        return d[d1][d2][d3] = minn;
    }
    
    int main()
    {
        while(~scanf("%s%s", sa, sb))
        {
            n = strlen(sa);
            repu(i, 0, n) a[i] = sa[i] - '0';
            repu(i, 0, n) b[i] = sb[i] - '0';
            a[n] = a[n + 1] = b[n] = b[n + 1] = 0;
    
            _cle(d, -1);
            printf("%d
    ", dp(0, a[0], a[1]));
        }
        return 0;
    }
    View Code
  • 相关阅读:
    batch build OSG
    COM技术内幕第八章笔记组件复用
    在Fireworks CS5中使用PS滤镜
    不能载入ive场景
    转:lib,dll和h
    想不到的.ive
    显卡始终只支持OPENGL1.1
    初识3D JavaScript接口
    32位CPU寄存器简介以及TSS和TR
    3dsMax的Intervals
  • 原文地址:https://www.cnblogs.com/sunus/p/4507362.html
Copyright © 2011-2022 走看看