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. ALV详解:OO SALV

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  2. SQL判断临时表是否存在

    IF EXISTS(select * from tempdb..sysobjects where id=object_id('tempdb..#tb')) BEGIN DROP TABLE #tb E ...

  3. Http协议简单学习笔记

    HTTP是hypertext transfer protocol(超文本传输协议)的简写,它是TCP/IP协议的一个应用层协议,用于定义WEB浏览器与WEB服务器之间交换数据的过程. 在HTTP1.0 ...

  4. SQL & PL/SQL 模块总结

    SQL 1. 各种function 2. merge 3. connect by PL/SQL 1. pl/sql 寄出 2. 游标 3. procedure 4. function 5. packa ...

  5. hdu4588Count The Carries

    链接 去年南京邀请赛的水题,当时找规律过的,看它长得很像数位dp,试了试用数位dp能不能过,d出每位上有多少个1,然后TLE了..然后用规律优化了前4位,勉强过了. 附数位dp代码及找规律代码. #i ...

  6. ASP.NET MVC HtmlHelper用法集锦

    ASP.NET MVC HtmlHelper用法集锦 在写一个编辑数据的页面时,我们通常会写如下代码 1:<inputtype="text"value='<%=View ...

  7. css处理浏览器兼容问题

    浏览器的兼容: _ ie6认识 格式:_width:100px;要写在最后面来覆盖前面的. * ie6和ie7都认识 格式:* width:100px;要写在最后面来覆盖前面的. \0 ie8认识  ...

  8. XMLHttpRequest对象进行Ajax操作

    XMLHttpRequest 对象的三个常用的属性: 1. onreadystatechange 属性  onreadystatechange 属性存有处理服务器响应的函数. 请求状态改变的事件触发器 ...

  9. python中的for循环

    打印出1到100的数,不包含100 for i in range(1,100): if i==23: print "great,you got your luncky number:&quo ...

  10. Android中view动画

    [1]透明 //点击按钮 实现iv 透明的效果 动画 public void click1(View v) { //1.0意味着着完全不透明 0.0意味着完全透明 AlphaAnimation aa ...