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. TCP的流量控制和拥塞控制

    1.TCP的流量控制 一般说来,我们总是希望数据传输的更快一些.但如果发送方吧数据发送的过快,接收方就可能来不及接收,就会造成数据的丢失.所谓的流量控制就是让发送方的发送速率不要太快,要让接收方来的及 ...

  2. CUBRID学习笔记 19 sql语句1

    创建 欢迎转载 ,转载时请保留作者信息.本文版权归本人所有,如有任何问题,请与我联系wang2650@sohu.com . 过错 create table tableName (字段名 字段类型 pr ...

  3. POJ 3069 Saruman's Army(萨鲁曼军)

    POJ 3069 Saruman's Army(萨鲁曼军) Time Limit: 1000MS   Memory Limit: 65536K [Description] [题目描述] Saruman ...

  4. QQ音乐项目(OC版) - 实现细节

    QQ 音乐看似简单,但自己手动实现起来,才发现没有那么简单,有好多细节,需要注意. github : https://github.com/keenleung/QQMusic-OC 一.业务逻辑 首先 ...

  5. iOS - OC NSSize 尺寸

    前言 结构体,这个结构体用来表示事物的宽度和高度. typedef CGSize NSSize; struct CGSize { CGFloat width; CGFloat height; }; t ...

  6. ORACLE 查询日志

    SQL>select * from v$sql (#查看最近所作的操作) SQL>select * fromv $sqlarea(#查看最近所作的操作)

  7. 06 SQL执行计划

    解释计划 与 执行计划的 区别 随着可以得到解释计划输出的开发工具, 比如 toad 的普遍使用, 生成解释计划就变的相当简单. 而不简单的是得到执行计划. 解释计划 EXPLAIN PLAN 用来显 ...

  8. js输出26个字母两种方法(js fromCharCode的使用)

    方法一 var character = new Array("A","B","C","D","E", ...

  9. Javascript设计模式之创建构造函数和方法

    构造函数和方法 var Book = function (isbn,title,author) { this.setIsbn(isbn); this.setTitle(title); this.set ...

  10. GCC编译器和GDB调试器常用选项

    http://blog.csdn.net/u014328976/article/details/46745349 GCC编译器 gcc hello.c -o hello                 ...