470人阅读 评论(0)

# locker

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1271    Accepted Submission(s): 549

Problem Description
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

Source

Recommend
zhoujiaqi2010   |   We have carefully selected several similar problems for you:  4812 4811 4810 4809 4808

f[i,j,k] 表示第i格已经扳到j，i+1格已经扳到k。前i-1个格子都到指定位置。的最小转动次数。

（注意k和a的含义不同，j和t的含义也不同。因为j和k是之前被动地转，a是当前i+1被动地转，t是i主动在转）

1、无序有序化，有时就可以消除后效性（当然要利用一些手段比如整体看待）。
2、有多少元素参与转移，常常就会有多少元素在转移方程里，把它们当做一个整体来看待。

#include <cstdio>
#include <cstring>

int min(int a,int b)
{
return a<b?a:b;
}

int f[1010][10][10];
char s1[1010];
char s2[1010];

int main()
{
while (scanf("%s%s",s1,s2)!=EOF)
{
int len = strlen(s1);

memset(f,0x3f,sizeof f);
f[0][0][0] = 0;
for (int i=0;i<len;i++)
{
for (int j=0;j<10;j++)
{
for (int k=0;k<10;k++)
{
int t = (s2[i]-s1[i]-j+20)%10;
for (int a=0;a<=t;a++)
{
for (int b=0;b<=a;b++)
{
f[i+1][(k+a)%10][b] = min(f[i+1][(k+a)%10][b],f[i][j][k]+t);
}
}

t = 10-t;
for (int a=0;a<=t;a++)
{
for (int b=0;b<=a;b++)
{
f[i+1][(k-a+20)%10][(10-b)%10] = min(f[i+1][(k-a+20)%10][(10-b)%10],f[i][j][k]+t);
}
}
}
}
}
printf("%d\n",f[len][0][0]);

}
return 0;
}

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：335392次
• 积分：7282
• 等级：
• 排名：第3145名
• 原创：419篇
• 转载：14篇
• 译文：0篇
• 评论：32条
文章分类
最新评论