HDU 3561 DP

Source: Internet
Author: User
A simple problem

Time Limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 464 accepted submission (s): 189


Problem descriptionthere's one row of buttons lying on onmylove's laptop keyboard, which are used to input the numbers, just as shown in the picture:

Onmylove used to input the numbers with his two specifc fingers, one is on the left, the other is on the right. in one unit time, onmylove's two fingers can both operate. for each finger, each operation can be one of the following:
1. Press on the button just under the finger.
2. Move to the left button or the right button.

But there're still some points you shoshould pay attention:
1. At any time, the left nger shoshould at the left side of the right finger.
2. In one unit of time, only one of these two fingers can press the button under it. Of course, the other
Finger can move at this unit of time.

Now you are given a string which consists of only numbers, you are asked to calculate: If onmylove want to input all these numbers, how many time does he need at least? At the every beginning, the left finger is above the button "5" and the right finger is
Above the button "6 ".

 


Inputmultiple inputs. For each test case, there is one string at a line. It's promised there're only numbers in the string and the length of it is not more than 100.


Outputfor each test case, output one number at a line, which represents the least time you need.


Sample Input

434565747
 


Sample output

5223
 


Authoronmylove


Source2010 Asia Regional Chengdu site -- online contest


Recommendlcy

#include <iostream>#include <cstring>#include <cstdio>#define inf 0x3f3f3f3f#define bug printf("asfd")using namespace std;char word[105];int num[105];int dp[2][15][15];int pre,cur;inline int abs(int x){    if(x>=0)        return x;    else        return -x;}void lefthand(int pos,int x,int y){    if(pos==10)        return ;    int dis=abs(pos-x)+1;    int i;    for(i=0;i<=dis&&y+i<=10;i++)    {        if(y+i>pos)            dp[cur][pos][y+i]=min(dp[cur][pos][y+i],dp[pre][x][y]+dis);    }    for(i=0;i<=dis&&y-i>=2;i++)    {        if(y-i>pos)            dp[cur][pos][y-i]=min(dp[cur][pos][y-i],dp[pre][x][y]+dis);    }}void righthand(int pos,int x,int y){    if(pos==1)    {        return ;    }    int dis=abs(y-pos)+1;    int i;    for(i=0;i<=dis&&x+i<=9;i++)        if(x+i<pos)            dp[cur][x+i][pos]=min(dp[cur][x+i][pos],dp[pre][x][y]+dis);    for(i=0;i<=dis&&x-i>=1;i++)        if(x-i<pos)            dp[cur][x-i][pos]=min(dp[cur][x-i][pos],dp[pre][x][y]+dis);}int main(){    while(scanf("%s",word)!=EOF)    {        int l=strlen(word);        int i;        for(i=0;i<l;i++)        {            if(word[i]=='0')                num[i]=10;            else                num[i]=word[i]-'0';        }        pre=0;        memset(dp[pre],inf,sizeof(dp[pre]));        dp[0][5][6]=0;        int k,j,pos;        for(k=0;k<l;k++)        {            cur=pre^1;            pos=num[k];            memset(dp[cur],inf,sizeof(dp[cur]));            for(i=1;i<10;i++)            {                for(j=i+1;j<=10;j++)                {                    if(dp[pre][i][j]!=inf)                    {                        lefthand(pos,i,j);                        righthand(pos,i,j);                    }                }            }            pre=cur;        }        int minn=inf;        for(i=1;i<=10;i++)        {            minn=min(dp[cur][pos][i],minn);            minn=min(dp[cur][i][pos],minn);        }        printf("%d\n",minn);    }    return 0;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.