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 ...
随机推荐
- Ionic + AngularJS angular-translate 国际化本地化解决方案
欢迎访问我们的网站,网站上有更多关于技术性的交流:http://www.ncloud.hk/技术分享/ionic-plus-angularjs-angular-translate-国际化本地化解决方案 ...
- ubuntu环境eclipse配置
ubuntu环境eclipse配置 首先下载Eclipse和JDK: 然后将上边两个压缩包解压到安装文件夹(如;/home/linux/softwares/java).然后配置/etc/profile ...
- [转]JavaScript
javascript 1. oncontextmenu="window.event.returnValue=false" 将彻底屏蔽鼠标右键 <table border on ...
- Java类加载器( 死磕8)
[正文]Java类加载器( CLassLoader ) 死磕 8: 使用ASM,和类加载器实现AOP 本小节目录 8.1. ASM字节码操作框架简介 8.2. ASM和访问者模式 8.3. 用于增 ...
- bc - An arbitrary precision calculator language
bc(1) General Commands Manual bc(1) NAME bc - An arbitrary precision calculator language SYNTAX bc [ ...
- cenos 6.5 安装apache 2.4.28出现种问题
编译出错 configure: error: APR-util not found. Please read the documentation 解决办法 wget http://apache.fre ...
- 我对Swift的几点疑问
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/caowenbin/article/details/31418773 Swift自问世 ...
- PAT天梯赛 L2-026. 小字辈 【BFS】
题目链接 https://www.patest.cn/contests/gplt/L2-026 思路 用一个二维vector 来保存 每个人的子女 然后用BFS 广搜下去,当目前的状态 是搜完的时候 ...
- centos 配置
安装 node 源地址: http://my.oschina.net/blogshi/blog/260953 (一) 编译好的文件 简单说就是解压后,在bin文件夹中已经存在node以及npm,如果你 ...
- 有哪些逾渗模型percolation?
自wiki 1.第一个研究的模型是伯努利逾渗, 这个模型中所有键bond独立,物理学家称为键逾渗. 2.随机聚类模型,同Ising模型和Potts模型有很多联系 3.在完备图上的键逾渗,属于在随机图上 ...