UVa 1631 Locker (DP)
题意:有一个 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)的更多相关文章
- UVA - 1631 Locker 记忆化搜索
题意:给定两个密码串,每次可以让1~3个相邻的密码向上或者向下滚动,每个密码是 ,问最少需要多少次滚动可以让原串成为目标串? 思路:假设当前要让第i位密码还原,我们可以同时转动,不同的转动方式会影响后 ...
- UVA - 1631 Locker(密码锁)(dp---记忆化搜索)
题意:有一个n(n<=1000)位密码锁,每位都是0~9,可以循环旋转.每次可以让1~3个相邻数字同时往上或者往下转一格.输入初始状态和终止状态(长度不超过1000),问最少要转几次. 分析: ...
- uva 1631
1631 Locker A password locker with N digits, each digit can be rotated to 0-9 circularly. You can ro ...
- UVA.10192 Vacation (DP LCS)
UVA.10192 Vacation (DP LCS) 题意分析 某人要指定旅游路线,父母分别给出了一系列城市的旅游顺序,求满足父母建议的最大的城市数量是多少. 对于父母的建议分别作为2个子串,对其做 ...
- UVA.10130 SuperSale (DP 01背包)
UVA.10130 SuperSale (DP 01背包) 题意分析 现在有一家人去超市购物.每个人都有所能携带的重量上限.超市中的每个商品有其相应的价值和重量,并且有规定,每人每种商品最多购买一个. ...
- 【Uva 1631】Locker
[Link]: [Description] 有一个n(n≤1000)位密码锁,每位都是0-9,可以循环旋转.每次可以让1-3个相邻 数字同时往上或者往下转一格.例如,567890->567901 ...
- 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 ...
- UVa 10029 hash + dp
题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- uva 10154 贪心+dp
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
随机推荐
- 虚拟网卡TUN/TAP 驱动程序设计原理
昨天韦哥写了<Linux下Tun/Tap设备通信原理>一文,只提到了两个使用Tun的用户进程之间的通信路径,并没有说明Tun虚拟网卡驱动是如何实现的,而正好看到了这里的一篇讲解这方面的文章 ...
- ios开发动物园管理 继承多态的实现
// // main.m // 继承 // // #import <Foundation/Foundation.h> #import "Animal.h" #impor ...
- 为什么在 Java 中用 (low+high)>>>1 代替 (low+high)/2 或 (low+high)>>1 来计算平均值呢?好在哪里?
>>>与>>是位运算符,只对整型有效(不能用于浮点型).当是整型的时候(low+high)>>1可以代替(low+high)/2.>>>是无 ...
- LookAround开元之旅
http://blog.csdn.net/lancees/article/details/17696805
- left outer join preserving unmatched rows from the first table
https://docs.oracle.com/javadb/10.8.3.0/ref/rrefsqlj18922.html INNER JOIN operation Specifies a join ...
- Linux就该这么学--命令集合1(常用系统工作命令)
1.用echo命令查看SHELL变量的值(前面有$符号): echo $SHELL 2.查看本机主机名: echo $HOSTNAME 3.查看当前的系统时间: date 4.按照“年-月-日 时:分 ...
- Spring 配置JNDI数据源
1.Spring 提供的JNDI调用类. 2.使用weblogic进行部署项目,所以使用WebLogicNativeJdbcExtrator类进行配置. 3.配置完数据源后配置sessionFacto ...
- iOS 打开应用与系统功能的调用
[[UIApplication sharedApplication] openURL:url]; 通过给url不同的值,可以实现调用系统自带 电话/短信/邮箱/浏览器/... 1.调用 电话pho ...
- curl上传图片(同域上传)
先研究完curl同域上传图片然后再研究curl跨域上传,先把同域上传代码总结如下: index.php <?php //图片上传 $url = "http://192.168.1.23 ...
- ES6 对Number的扩展
Number.isFinite(), Number.isNaN() ES6 在Number对象上,新提供了Number.isFinite()和Number.isNaN()两个方法. Number.is ...