hdu 4433 locker(动态规划)

locker

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


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
 

题目:

给出两个串,每次可以选择连续的1-3个数字,进行同时加1或者同时减1,问最少经过多少次操作,将一个串转变为另外一个串。

思路:

dp[i][j][k]表示 前i个已经完全匹配,而这时候,第i+1个已经加了j,第i+2位已经加了k

转移分为两步,枚举加,枚举减

怎么由dp[i]转移到dp[i+1]呢?

dp[i]表示i位置已经调整好了。我们考虑调整i+1位置的情况。能够改变i位置的状态的方法有三种。

1.将i+1位置单独改变。

2.将i+1,i+2位置同时改变。

3.将i+1,i+2,i+3位置同时改变。

所以对于每一次改变。如果第i+1位改变了a,第i+2位改变了b,第i+3位改变了c,那么必定存在a>=b>=c。

因为三种改变方式有三个能改变i+1。两个能改变i+2。一个改变i+3。

详细见代码:

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxn=1010;
char s1[maxn],s2[maxn];
int dp[maxn][10][10];

int main()
{
    int i,j,k,x,y,t,len,up,dw;

    while(~scanf("%s%s",s1,s2))
    {
        len=strlen(s1);
        memset(dp,0x3f,sizeof dp);
        dp[0][0][0]=0;
        for(i=0; i<len; i++)
        {
            for(j=0; j<10; j++)
            {
                up=(s2[i]-s1[i]-j+20)%10;//调整第i+1位需往上加的步数
                dw=(10-up)%10;//需往下减往下减
                for(k=0; k<10; k++)
                {
                    for(x=0; x<=up; x++)//枚举i+2位可以加的值
                    {
                        t=(k+x)%10;//i+1位调整好后i+2位已经加的值
                        for(y=0; y<=x; y++)//枚举i+3位可以加的值
                            dp[i+1][t][y]=min(dp[i+1][t][y],dp[i][j][k]+up);
                    }
                    for(x=0; x<=dw; x++)//枚举i+2位可以减的值
                    {
                        t=(k-x+10)%10;//i+1位调整好后i+2位已经加的值
                        for(y=0; y<=x; y++)//枚举i+3位可以减的值
                            dp[i+1][t][(10-y)%10]=min(dp[i+1][t][(10-y)%10],dp[i][j][k]+dw);
                    }
                }
            }
        }
        printf("%d\n",dp[len][0][0]);
    }
}


  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值