zoukankan      html  css  js  c++  java
  • UVa 1631 Locker (DP)

    题意:有一个 n 位密码锁,每位都是0-9,可以循环旋转。同时可以让1-3个相邻数字进行旋转一个,给定初始状态和目状态,问你最少要转多少次。

    析:很明显的一个DP题。dp[i][j][k] 表示前 i 位已经转好,并且第 i+1 位是 j ,第 i+2 位是 k,那么我们先把第 i 位转到指定位置,然后计算转多少次,

    然后再考虑 i+1位和 i+2位,要旋转小于等于第 i 位的次数,这就转移完了。比较简单的一个DP,只是没有遇见过。

    代码如下:

    #pragma comment(linker, "/STACK:1024000000,1024000000")
    #include <cstdio>
    #include <string>
    #include <cstdlib>
    #include <cmath>
    #include <iostream>
    #include <cstring>
    #include <set>
    #include <queue>
    #include <algorithm>
    #include <vector>
    #include <map>
    #include <cctype>
    #include <cmath>
    #include <stack>
    #define freopenr freopen("in.txt", "r", stdin)
    #define freopenw freopen("out.txt", "w", stdout)
    using namespace std;
    
    typedef long long LL;
    typedef pair<int, int> P;
    const int INF = 0x3f3f3f3f;
    const double inf = 0x3f3f3f3f3f3f;
    const LL LNF = 0x3f3f3f3f3f3f;
    const double PI = acos(-1.0);
    const double eps = 1e-8;
    const int maxn = 1e3 + 5;
    const int mod = 1e9 + 7;
    const int dr[] = {-1, 0, 1, 0};
    const int dc[] = {0, 1, 0, -1};
    const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
    int n, m;
    const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    inline int Min(int a, int b){ return a < b ? a : b; }
    inline int Max(int a, int b){ return a > b ? a : b; }
    inline LL Min(LL a, LL b){ return a < b ? a : b; }
    inline LL Max(LL a, LL b){ return a > b ? a : b; }
    inline bool is_in(int r, int c){
        return r >= 0 && r < n && c >= 0 && c < m;
    }
    char s1[maxn], s2[maxn];
    int a[maxn], b[maxn];
    int g[2][15][15];
    LL dp[maxn][15][15];
    
    int solve(int pre, int last, int cnt){
        return cnt ? (last+10-pre) % 10 : (pre+10-last) % 10;
    }
    
    int main(){
        for(int i = 0; i < 2; ++i)  for(int j = 0; j < 10; ++j)
            for(int k = 0; k < 10; ++k)  g[i][j][k] = i ? (j+k) % 10 : (j-k+10) % 10;
    
        while(scanf("%s %s", s1, s2) == 2){
            n = strlen(s1);
            for(int i = 1; i <= n; ++i) a[i] = s1[i-1] - '0', b[i] = s2[i-1] - '0';
            a[n+1] = a[n+2] = b[n+1] = b[n+2] = 0;
            for(int i = 0; i <= n; ++i) for(int j = 0; j < 10; ++j)
                for(int k = 0; k < 10; ++k) dp[i][j][k] = LNF;
    
            dp[0][a[1]][a[2]] = 0;
            for(int i = 1; i <= n; ++i){
                for(int r = 0; r < 2; ++r){
                    for(int j = 0; j < 10; ++j){
                        for(int k = 0; k < 10; ++k){
                            int x = solve(j, b[i], r);
                            for(int ii = 0; ii <= x; ++ii){
                                for(int jj = 0; jj <= ii; ++jj){
                                    dp[i][g[r][k][ii]][g[r][a[i+2]][jj]] = Min(dp[i][g[r][k][ii]][g[r][a[i+2]][jj]], dp[i-1][j][k] + x);
                                }
                            }
                        }
                    }
                }
            }
    
            printf("%lld
    ", dp[n][0][0]);
        }
        return 0;
    }
    
  • 相关阅读:
    Spring IOC容器的初始化-(二)BeanDefinition的载入和解析
    Spring IOC容器的初始化—(一)Resource定位
    SpringMVC的启动过程
    Spring IOC容器在Web容器中是怎样启动的
    Cannot retrieve metalink for repository: epel/x86_64. Please verify its path and try again 问题分析
    网络丢包分析
    yum安装jdk如何配置JAVA_HOME
    Linux网卡绑定
    Linux Buffer/Cache 的区别
    理解load averages
  • 原文地址:https://www.cnblogs.com/dwtfukgv/p/5836738.html
Copyright © 2011-2022 走看看