• UVA 1631 Locker(密码锁)(dp记忆化搜索)


    题意:有一个n(n<=1000)位密码锁,每位都是0~9,可以循环旋转。每次可以让1~3个相邻数字同时往上或者往下转一格。输入初始状态和终止状态(长度不超过1000),问最少要转几次。

    分析:

    1、从左往右依次使各个数字与终止状态相同。

    2、dp[cur][x1][x2][x3]表示当前研究数字为第cur位,x1为a[cur],x2为a[cur + 1],x3为a[cur + 2],在当前状态下,使所有数字变成终止状态的最小旋转次数。

    3、研究第cur位时,第cur+1位和第cur+2位也可以随之一起转,枚举当前所有的旋转情况,并分别讨论向上和向下旋转两种情况。

    #pragma comment(linker, "/STACK:102400000, 102400000")
    #include<cstdio>
    #include<cstring>
    #include<cstdlib>
    #include<cctype>
    #include<cmath>
    #include<iostream>
    #include<sstream>
    #include<iterator>
    #include<algorithm>
    #include<string>
    #include<vector>
    #include<set>
    #include<map>
    #include<stack>
    #include<deque>
    #include<queue>
    #include<list>
    #define Min(a, b) ((a < b) ? a : b)
    #define Max(a, b) ((a < b) ? b : a)
    const double eps = 1e-8;
    inline int dcmp(double a, double b){
        if(fabs(a - b) < eps) return 0;
        return a > b ? 1 : -1;
    }
    typedef long long LL;
    typedef unsigned long long ULL;
    const int INT_INF = 0x3f3f3f3f;
    const int INT_M_INF = 0x7f7f7f7f;
    const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
    const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
    const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
    const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
    const int MOD = 1e9 + 7;
    const double pi = acos(-1.0);
    const int MAXN = 1000 + 10;
    const int MAXT = 10000 + 10;
    using namespace std;
    char aa[MAXN], bb[MAXN];
    int a[MAXN], b[MAXN];
    int dp[MAXN][15][15][15];
    int len;
    int getUpStep(int st, int et){//向上翻转的步数,向上转数字变大
        return (et - st + 10) % 10;
    }
    int getDownStep(int st, int et){
        return (st - et + 10) % 10;
    }
    int upNowpos(int st, int length){
        return (st + length) % 10;
    }
    int downNowpos(int st, int length){//从st向下翻转length步变成的数字
        return (st - length + 10) % 10;
    }
    int dfs(int cur, int x1, int x2, int x3){
        if(dp[cur][x1][x2][x3] != -1) return dp[cur][x1][x2][x3];
        if(cur == len) return 0;
        int ans = INT_INF;
        int upstep = getUpStep(x1, b[cur]), downstep = getDownStep(x1, b[cur]);
        for(int i = 0; i <= upstep; ++i){//枚举第cur+1位可以跟着第cur位一起向上旋转的步数
            for(int j = 0; j <= i; ++j){//枚举第cur+2位可以跟着第cur位和第cur+1位一起向上旋转的步数
                ans = Min(ans, dfs(cur + 1, upNowpos(x2, i), upNowpos(x3, j), a[cur + 3]) + upstep);
            }
        }
        for(int i = 0; i <= downstep; ++i){//向下转
            for(int j = 0; j <= i; ++j){
                ans = Min(ans, dfs(cur + 1, downNowpos(x2, i), downNowpos(x3, j), a[cur + 3]) + downstep);
            }
        }
        return dp[cur][x1][x2][x3] = ans;
    }
    int main(){
        while(scanf("%s%s", aa, bb) == 2){
            memset(dp, -1, sizeof dp);
            len = strlen(aa);
            for(int i = 0; i < len; ++i) a[i] = aa[i] - '0';
            for(int i = 0; i < len; ++i) b[i] = bb[i] - '0';
            a[len] = a[len + 1] = a[len + 2] = 0;
            b[len] = b[len + 1] = b[len + 2] = 0;
            printf("%d\n", dfs(0, a[0], a[1], a[2]));
        }
        return 0;
    }
    

      

  • 相关阅读:
    1 let和const
    ECMAScript 6 扫盲
    回溯法
    13. Ajax技术
    12.JSTL标签
    11.EL(表达式语言)
    10.正则表达式(未完成)
    博客园自定义样式
    9.一次简单的Web作业
    8.JavaScript
  • 原文地址:https://www.cnblogs.com/tyty-Somnuspoppy/p/6431774.html
Copyright © 2020-2023  润新知