Ultraviolet A 1631-Locker (DP), uva1631-lockerdp

Source: Internet
Author: User

Ultraviolet A 1631-Locker (DP), uva1631-lockerdp

Give two strings with only numbers whose length is n (n <= 1000, each operation can add 10 plus 1 or 10 minus 1 (9 + 1 = 0-1 = 9) to the number of consecutive 1-3 orders ), find the minimum number of steps for the source string to become the target string.


First, assume that the last operand is used as the operand for any operation. For example, if the number of operations changes from 111 to 3rd, the number of operations is considered.

The cost array is pre-processed. cost [I] [j] indicates the minimum number of steps (I, j <1000 ). For example, if 111 is changed to 3, only the last number can be operated, so 220 --> 111-> 222 --> 221.


Use a [I] and B [I] to represent the I Number of the source and target numeric strings.

In the case that d [I] [j] represents the first I number, the first I-2 has become the destination number, and the I-1 and I constitute the decimal number j, the minimum number of required steps.

State d [I-1] [u] completion recursion based on all operations completed before the operands are I-1 and I-1.

If the previous state is d [I-1] [u] (at this time the number of I in the source string has not changed), then the transfer is to operate only the last number, change three-digit u * 10 + a [I] to three-digit B [I-2] * 100 + j (in State d [I] [j, the number of I-2 has become the target number), exactly the content of the cost array storage, so as to complete the recurrence.


State transition equation:

D [I] [j] = min {d [I-1] [u] + cost [q] [p]}

Where p = B [I-2] * 100 + j, q = u * 10 + a [I]


#include<stdio.h>#include<stdlib.h>#include<string.h>char a[1010];char b[1010];int d[2][110];int cost[1010][1010];int main(void){int i,j,u,p,q,i1,i2,i3,j1,j2,j3,u1,u2,u3,lo,cur,minp;for(i=0;i<1000;i++){for(j=0;j<1000;j++){if(i!=j){if(cost[j][i]>0){cost[i][j]=cost[j][i];}else{i1=i/100;i2=(i%100)/10;i3=i%10;j1=j/100;j2=(j%100)/10;j3=j%10;if(i1<j1){u1=j1-i1;i2=(i2+u1)%10;i3=(i3+u1)%10;}else{u1=i1-j1;i2=(i2-u1+10)%10;i3=(i3-u1+10)%10;}if(i2<j2){u2=j2-i2;i3=(i3+u2)%10;}else{u2=i2-j2;i3=(i3-u2+10)%10;}u3=(i3>j3)?i3-j3:j3-i3;u1=(u1>10-u1)?10-u1:u1;u2=(u2>10-u2)?10-u2:u2;u3=(u3>10-u3)?10-u3:u3;cost[i][j]=u1+u2+u3;}}}}while(scanf("%s%s",a+1,b+1)==2){lo=strlen(a+1);if(lo==1){u=(a[1]>b[1])?a[1]-b[1]:b[1]-a[1];printf("%d\n",(u>10-u)?10-u:u);}else{for(j=0;j<10;j++){u=(j>a[1]-'0')?j-a[1]+'0':a[1]-'0'-j;d[0][j]=(u>10-u)?10-u:u;}for(j=0;j<100;j++){minp=(1<<30);for(u=0;u<10;u++){q=u*10+a[2]-'0';minp=(d[0][u]+cost[q][j]<minp)?d[0][u]+cost[q][j]:minp;}d[1][j]=minp;}cur=1;for(i=3;i<=lo;i++){cur^=1;for(j=0;j<100;j++){minp=(1<<29);for(u=0;u<100;u++){p=(b[i-2]-'0')*100+j;q=u*10+a[i]-'0';minp=(d[cur^1][u]+cost[p][q]<minp)?d[cur^1][u]+cost[p][q]:minp;}d[cur][j]=minp;}}p=(b[lo-1]-'0')*10+b[lo]-'0';printf("%d\n",d[cur][p]);}}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.