uva 1631
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的更多相关文章
- UVA - 1631 Locker 记忆化搜索
题意:给定两个密码串,每次可以让1~3个相邻的密码向上或者向下滚动,每个密码是 ,问最少需要多少次滚动可以让原串成为目标串? 思路:假设当前要让第i位密码还原,我们可以同时转动,不同的转动方式会影响后 ...
- UVa 1631 密码锁
https://vjudge.net/problem/UVA-1631 题意: 有一个n位密码锁,每位都是0~9,可以循环旋转.每次可以让1~3个相邻数字同时往上或者往下转一格.输入初始状态和终止状态 ...
- UVa 1631 Locker (DP)
题意:有一个 n 位密码锁,每位都是0-9,可以循环旋转.同时可以让1-3个相邻数字进行旋转一个,给定初始状态和目状态,问你最少要转多少次. 析:很明显的一个DP题.dp[i][j][k] 表示前 i ...
- 【Uva 1631】Locker
[Link]: [Description] 有一个n(n≤1000)位密码锁,每位都是0-9,可以循环旋转.每次可以让1-3个相邻 数字同时往上或者往下转一格.例如,567890->567901 ...
- UVA - 1631 Locker(密码锁)(dp---记忆化搜索)
题意:有一个n(n<=1000)位密码锁,每位都是0~9,可以循环旋转.每次可以让1~3个相邻数字同时往上或者往下转一格.输入初始状态和终止状态(长度不超过1000),问最少要转几次. 分析: ...
- uva 1354 Mobile Computing ——yhx
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABGcAAANuCAYAAAC7f2QuAAAgAElEQVR4nOy9XUhjWbo3vu72RRgkF5
- UVA 10564 Paths through the Hourglass[DP 打印]
UVA - 10564 Paths through the Hourglass 题意: 要求从第一层走到最下面一层,只能往左下或右下走 问有多少条路径之和刚好等于S? 如果有的话,输出字典序最小的路径 ...
- UVA 11404 Palindromic Subsequence[DP LCS 打印]
UVA - 11404 Palindromic Subsequence 题意:一个字符串,删去0个或多个字符,输出字典序最小且最长的回文字符串 不要求路径区间DP都可以做 然而要字典序最小 倒过来求L ...
- UVA&&POJ离散概率与数学期望入门练习[4]
POJ3869 Headshot 题意:给出左轮手枪的子弹序列,打了一枪没子弹,要使下一枪也没子弹概率最大应该rotate还是shoot 条件概率,|00|/(|00|+|01|)和|0|/n谁大的问 ...
随机推荐
- Field+offset(len)
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- [Java解惑]类
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- ironpython 2.75 在c#中的使用
ironpython的介绍请自行搜索. 一句话,python是一个类似lua js的动态预言.ironpython是在net环境执行python的类库. 效果:在网站中调用一个python文件test ...
- Ubuntu下手动安装VMware Tools步骤
To mount the CD image and extract the contents: Power on the virtual machine. Log in to the virtual ...
- 移动端 移动web屏幕适配方案 随不同宽度的屏幕而改变
链接地址1:http://www.cnblogs.com/zjzhome/p/4802157.html 链接地址2:http://www.html-js.com/article/Mobile-term ...
- SpringMVC后缀
<!-- 在这里,使用*.html为后缀的URL都能被baobaotao Servlet截获,进而转由SpringMVC框架进行处理.在 Struts框架中,一般将URL后缀配置为*.do:在w ...
- 不含类解决最后一个li边距问题
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- iOS - UIStepper
前言 NS_CLASS_AVAILABLE_IOS(5_0) __TVOS_PROHIBITED @interface UIStepper : UIControl @available(iOS 5.0 ...
- 从POI到O2O 看百度地图如何走出未来之路
近期O2O的烧钱融资大战如火如荼,有人已经把O2O大战,用乌合之众的群体心理失控来形容.其实厂商都不傻,O2O烧钱大家都知道,但是大家还知道O2O背后这块大蛋糕价值"万亿级". 有 ...
- poj2451Uyuw's Concert(半平面交)
链接 逆时针给出线段,如果模板是顺时针的修改下系数的符号进行平面交即可. #include <iostream> #include<cstdio> #include<cs ...