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][][];
int n; int up(int s, int t)
{
if(s <= t) return t - s;
return ( + t - s);
} int down(int s, int t)
{
if(s >= t) return s - t;
return ( + s - t);
} int dp(int d1, int d2, int d3)
{
if(d1 >= n) return ;
if(d[d1][d2][d3] != -) return d[d1][d2][d3];
int t, minn = INF;
t = up(d2, b[d1]);
repu(i, , t + )
repu(j, i, t + )
minn = min(minn, dp(d1 + , (d3 + j) % , (a[d1 + ] + i) % ) + t);
t = down(d2, b[d1]);
repu(i, , t + )
repu(j, i, t + )
minn = min(minn, dp(d1 + , (d3 - j + ) % , (a[d1 + ] - i + ) % ) + t);
return d[d1][d2][d3] = minn;
} int main()
{
while(~scanf("%s%s", sa, sb))
{
n = strlen(sa);
repu(i, , n) a[i] = sa[i] - '';
repu(i, , n) b[i] = sb[i] - '';
a[n] = a[n + ] = b[n] = b[n + ] = ; _cle(d, -);
printf("%d\n", dp(, a[], a[]));
}
return ;
}

uva 1631的更多相关文章

  1. UVA - 1631 Locker 记忆化搜索

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

  2. UVa 1631 密码锁

    https://vjudge.net/problem/UVA-1631 题意: 有一个n位密码锁,每位都是0~9,可以循环旋转.每次可以让1~3个相邻数字同时往上或者往下转一格.输入初始状态和终止状态 ...

  3. UVa 1631 Locker (DP)

    题意:有一个 n 位密码锁,每位都是0-9,可以循环旋转.同时可以让1-3个相邻数字进行旋转一个,给定初始状态和目状态,问你最少要转多少次. 析:很明显的一个DP题.dp[i][j][k] 表示前 i ...

  4. 【Uva 1631】Locker

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

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

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

  6. uva 1354 Mobile Computing ——yhx

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABGcAAANuCAYAAAC7f2QuAAAgAElEQVR4nOy9XUhjWbo3vu72RRgkF5

  7. UVA 10564 Paths through the Hourglass[DP 打印]

    UVA - 10564 Paths through the Hourglass 题意: 要求从第一层走到最下面一层,只能往左下或右下走 问有多少条路径之和刚好等于S? 如果有的话,输出字典序最小的路径 ...

  8. UVA 11404 Palindromic Subsequence[DP LCS 打印]

    UVA - 11404 Palindromic Subsequence 题意:一个字符串,删去0个或多个字符,输出字典序最小且最长的回文字符串 不要求路径区间DP都可以做 然而要字典序最小 倒过来求L ...

  9. UVA&&POJ离散概率与数学期望入门练习[4]

    POJ3869 Headshot 题意:给出左轮手枪的子弹序列,打了一枪没子弹,要使下一枪也没子弹概率最大应该rotate还是shoot 条件概率,|00|/(|00|+|01|)和|0|/n谁大的问 ...

随机推荐

  1. git学习笔记12-标签管理-版本

    发布一个版本时,我们通常先在版本库中打一个标签(tag),这样,就唯一确定了打标签时刻的版本.将来无论什么时候,取某个标签的版本,就是把那个打标签的时刻的历史版本取出来.所以,标签也是版本库的一个快照 ...

  2. shift移动变量

    1.移动变量 脚本 sh05.sh #!/bin/bash # Program # Program shows the effect of shift function # History: # // ...

  3. iOS - Swift NSNumber 数字

    前言 public class NSNumber : NSValue public class NSDecimalNumber : NSNumber NSNumber 可以被赋值为各种数值类型.我们可 ...

  4. amd64_or_ia64?

    amd64 网上资料: 1. IA64是intel推出的架构,AMD64是AMD推出的.IA64不兼容原有的32位x86架构指令集,后来被证明这种做法是不成功的,于是Intel发展处IA64e架构,对 ...

  5. C/C++中static关键字作用总结

    来来来,来看这篇文章: http://www.cnblogs.com/biyeymyhjob/archive/2012/07/19/2598815.html 总结一下: 1.先来介绍它的第一条也是最重 ...

  6. JS---------->数组练习!

      var arr = [4, 0, 7, 9, 0, 0, 2, 6, 0, 3, 1, 0]; 要求将数组中的0项去掉,将不为0的值存入一个新的数组,生成新的数组 <!doctype htm ...

  7. Scala的Actor模式 & Akka框架

    今天学Spark的时候,看到Scala的actor模式是一个加分点.所以搜了一下,看了.主要参考下面两篇文章,还没有实验,有些地方领会的不深刻: http://nxlhero.blog.51cto.c ...

  8. C++ 多线程中的一个抛出异常

    试了一下,和Java完全不同. 注意Java和C++对于多线程里面的一个线程抛出异常的影响,完全不同. Java里面,对于主线程和其他线程完全不受影响: C++里面,整个程序会退出,所有线程都会受影响 ...

  9. GIT过滤

    git 创建 .gitignore 文件 建立项目过滤规则 创建 .gitignore 随意设置想跟踪哪些文件 和不跟踪哪些文件. 1.在项目根目录下建立 .gitignore 文件 2.   .gi ...

  10. 货币金额的计算 - Java中的BigDecimal

    在<Effective Java>这本书中也提到这个原则,float和double只能用来做科学计算或者是工程计算,在商业计算中我们要用 java.math.BigDecimal.,而且使 ...