题意:有一个 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\n", dp[n][0][0]);
}
return 0;
}

UVa 1631 Locker (DP)的更多相关文章

  1. UVA - 1631 Locker 记忆化搜索

    题意:给定两个密码串,每次可以让1~3个相邻的密码向上或者向下滚动,每个密码是 ,问最少需要多少次滚动可以让原串成为目标串? 思路:假设当前要让第i位密码还原,我们可以同时转动,不同的转动方式会影响后 ...

  2. UVA - 1631 Locker(密码锁)(dp---记忆化搜索)

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

  3. uva 1631

    1631 Locker A password locker with N digits, each digit can be rotated to 0-9 circularly. You can ro ...

  4. UVA.10192 Vacation (DP LCS)

    UVA.10192 Vacation (DP LCS) 题意分析 某人要指定旅游路线,父母分别给出了一系列城市的旅游顺序,求满足父母建议的最大的城市数量是多少. 对于父母的建议分别作为2个子串,对其做 ...

  5. UVA.10130 SuperSale (DP 01背包)

    UVA.10130 SuperSale (DP 01背包) 题意分析 现在有一家人去超市购物.每个人都有所能携带的重量上限.超市中的每个商品有其相应的价值和重量,并且有规定,每人每种商品最多购买一个. ...

  6. 【Uva 1631】Locker

    [Link]: [Description] 有一个n(n≤1000)位密码锁,每位都是0-9,可以循环旋转.每次可以让1-3个相邻 数字同时往上或者往下转一格.例如,567890->567901 ...

  7. BZOJ 1260&UVa 4394 区间DP

    题意: 给一段字符串成段染色,问染成目标串最少次数. SOL: 区间DP... DP[i][j]表示从i染到j最小代价 转移:dp[i][j]=min(dp[i][j],dp[i+1][k]+dp[k ...

  8. UVa 10029 hash + dp

    题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  9. uva 10154 贪心+dp

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

随机推荐

  1. tomcat 代码集

    Tomcat类是整个tomcat的起点,负责加载所有的配置信息以及把配置信息解析转换成tomcat组件对象. Context addWebapp(String contextPath, String ...

  2. 用Cocoapods集成XMPPFramework 遇 Module 'KissXML' not found 问题

    用Coacopods集成XMPPFramework完成后Command + B,报Module 'KissXML' not found 一般来说,通过Coacopods集成集成第三方框架,不会再有依赖 ...

  3. SAM4E单片机之旅——3、LED闪烁之定时器中断

    让一个LED灯闪烁不过瘾,我们应该让这块开发板完成一点更高难度的任务:比如让两个LED灯闪烁. …… 当然了,以我们的现在使用的空循环技术,还是可以实现这点的.但是这样显得略为低端.所以我们使用一个高 ...

  4. AAC包增加ADTS头Without MediaCodec

    AAC原始码流无法直接播放,一般需要封装为ADTS格式才能再次使用,本博主在android中用MediaCodec编码得到的AAC就是raw格式,为了保存为.aac格式,需要增加adts头,这样就可以 ...

  5. 基于live555实现的跨平台高性能RTSPServer流媒体服务器EasyIPCamera

    本文转自EasyDarwin团队成员kim的博客:http://blog.csdn.net/jinlong0603/article/details/52366412 简介 EasyIPCamera是由 ...

  6. Spring mvc接受集合类型参数的方法

    public String xxxxx(String xxxx, String xxxxx, @RequestParam("parameterList[]") List<St ...

  7. 九度OJ 1095:2的幂次方 (递归)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:913 解决:626 题目描述: Every positive number can be presented by the exponent ...

  8. TTimer很特殊

    TTimer = class(TComponent) private FInterval: Cardinal; FWindowHandle: HWND; FOnTimer: TNotifyEvent; ...

  9. windows搭建FTP服务器实战

    第一步:创建用户名密码(ftp使用) 1.1.点击“开始”菜单,选择“控制面板”. 1.2.选择“管理工具”—>“计算机管理” 1.3. 选择“本地用户和组”下的用户,右键选择“新用户” 输入用 ...

  10. TabHost的坑

    问题1.  运行Activity的时候出现Your content must have a TabHost whose id attribute is ‘android.R.id.tabhost’ 问 ...