您的位置:首页 > 其它

Hdu 4433 locker【思维+Dp】

2017-10-07 16:16 417 查看


locker

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 2082    Accepted Submission(s): 949


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

题目大意:

我们每次操作可以选择连续的1~3的位子上的数字,进行加法或者减法操作,使得s串变成t串。

问最少操作次数。

思路:

我们设定Dp【i】【j】【k】表示我们【1~i】位子上的结果都已经正确了,并且i+1位现在是数字j,第i+2位现在是数字k的情况下的最小操作次数。

那么有:

Dp【i+1】【j+x】【k+y】=min(Dp【i+1】【j+x】【k+y】,Dp【i】【j】【k】+从j变成t【i+1】的步数);

这里x<=从j变成t【i+1】的步数;y<=x;

过程维护一下即可。

Ac代码:

#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
int dp[1500][12][12];
char a[1500];
char b[1500];
int main()
{
while(~scanf("%s%s",a+1,b+1))
{
memset(dp,0x7f,sizeof(dp));
int n=strlen(a+1);
a[n+1]=b[n+1]='0';a[n+2]=b[n+2]='0';a[n+3]=b[n+3]='0';
dp[0][a[1]-'0'][a[2]-'0']=0;
int output=0x7f;
for(int i=0;i<=n;i++)
{
for(int j=0;j<10;j++)
{
for(int k=0;k<10;k++)
{
if(dp[i][j][k]<n*100)
{
int temp=0;
int tmp=j;
while(tmp!=b[i+1]-'0')
{
tmp++;
temp++;
tmp%=10;
}
for(int x=0;x<=temp;x++)
{
for(int y=0;y<=x;y++)
{
dp[i+1][(k+x)%10][(y+a[i+3]-'0')%10]=min(dp[i+1][(k+x)%10][(y+a[i+3]-'0')%10],dp[i][j][k]+temp);
}
}
temp=0;
tmp=j;
while(tmp!=b[i+1]-'0')
{
tmp--;
temp++;
tmp=(tmp%10+10)%10;
}
for(int x=0;x<=temp;x++)
{
for(int y=0;y<=x;y++)
{
dp[i+1][((k-x)%10+10)%10][((-y+a[i+3]-'0')%10+10)%10]=min(dp[i+1][((k-x)%10+10)%10][((-y+a[i+3]-'0')%10+10)%10],dp[i][j][k]+temp);
}
}
}
if(i==n)output=min(output,dp[i][j][k]);
}
}
}
printf("%d\n",dp
[0][0]);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Hdu 4433