Hdu 4433 locker【思维+Dp】

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
#include
#include
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]










你可能感兴趣的:(思维,dp)