"8001" Enter password

Source: Internet
Author: User
Tags add numbers

Time Limit:3 Second
Memory Limit:2 MB

"Problem description"
    John is a department of staff, in order to improve the security level, the department's password keyboard is specially designed, there is no number keys on the keyboard, and only six keys: UP,DOWN,LEFT,RIGHT,SW0,SW1, the definition of the input area of the six positions of the number, from left to right in turn, 4,5,6. Below is a list of the functions of each key:  Sw0: Press Sw0, the position of the cursor is not changed, the cursor location of the number and the input area number 1th position on the exchange. If the cursor is already in the entry area of position 1th, then press Sw0 key, the number of the input area is unchanged; Sw1: Press SW1, the position of the cursor is not changed, the position of the cursor in the location of the number and the number 6th in the input area of the digital exchange. If the cursor is already in position 6th of the entry area, the number of the input area will not change after pressing the SW1 key, up: press up, the position of the cursor is unchanged, and the number on the position of the cursor is 1 (unless the number is 9). For example, if the number at the cursor position is 2, the number is changed to 3 after pressing up, and if the number is 9, the number does not change and the cursor position is not changed after pressing up, and down: press down, the position of the cursor is unchanged, and the number at the cursor position is reduced by 1 (unless the number is 0) , if the number is 0, then press down, the number does not change, the cursor position is not changed ; Left: The cursor will remain in position if the cursor is positioned at position 1th in the input area (the first position on the left), and the cursor will not move  .
Input
The input data is one line, two six digits, separated by a space. The first data refers to the initial number (6 bits) on the input area, followed by a space, and the second to the password value to be entered (6 is a number).
Output
Output one line, minimum number of keystrokes
"Input Sample" 123456 654321
"Output Example"

11


Exercises

Known starting point to find the minimum number of steps, wide search problem. This problem cannot be done with the string class, although the string class is very convenient, but after this time, I thoroughly felt that the string class at run time how slow ... Enter the two numbers first as a string class, and then convert the two string classes to an int array. It is much faster to operate with an int array.

Then the sentence weight, with the bool array bo[1000000][6] to the weight, the first dimension is 6 digits, the second dimension is the position of the cursor. Then use the loop queue when doing the wide search, otherwise it will be super memory.

See the code comment specifically.

Code

#include <cstdio> #include <string> #include <stdlib.h> #include <iostream>using namespace std; const int QQ = 4500000;const int QQQ = 4500000+20; These two constants are used for the loop queue and this variable can become 250W, which makes up less memory. int a,b,goal;string ts1,ts2;bool bo[1000000][6];int dl1[qqq][6];short dl2[qqq],dl3[qqq];int s1[6],s2[6];int get_num (    int ss[6])//convert int array to a single shaping variable {int L1 = 6,t = 0;    int i = 0;            while (i <= l1-1) {t = t*10 + ss[i];        i++; } return t;}    void Input_data () {cin >> ts1;//Convert string class to int array cin >> TS2;    for (int i = 0; i<= 5;i++) s1[i] = ts1[i]-' 0 ';    for (int i = 0;i <= 5;i++) s2[i] = ts2[i]-' 0 '; Goal = Get_num (S2);  Get the target number for (int i = 100000;i <= 999999;i++)//start with a weight array initialization for (int j = 0;j <= 5;j++) bo[i][j] = false;}    void Change (int & a,int & B)//Exchange numbers A and b{int t = A;    A = b; b = t;}    void Get_ans ()//start wide search {int head = 0,tail = 1; for (int i = 0;i <= 5;i++) Dl1[1][i] = S1[i]; DL2[1] = 0; Number of steps dl3[1] = 0;    cursor position int t = get_num (S1);    Bo[t][0] = true;            while (head! = tail)//loop queue cannot be written as head <= tail {head++;            int temp[6];            for (int i = 0;i <= 5;i++)//Remove the head node of the queue temp[i] = Dl1[head][i];            int step = Dl2[head],p = Dl3[head]; Head = head% QQ; Loop queue int NUM0 = Get_num (temp);                    Convert the int array to an integer//and No. 0 position on the number Exchange if (P! = 0)//If the cursor is not in the first position and the first position is swapped {                    Change (temp[p],temp[0]); int NUM1 = Get_num (temp); Convert to Digital if (!bo[num1][p])//Weight {if (NUM1 = = goal)//                                    If the target is obtained, output {printf ("%d\n", step+1);                                Exit (0); } Bo[num1][p] = true;                   Sentence weight         tail++;                            Put this element into the queue together with the cursor position also to save tail = tail%qq;                            for (int i = 0;i <= 5;i++) dl1[tail][i] = Temp[i]; Dl2[tail] = step+1;                            Also the number of steps to be deposited.                        Dl3[tail] = p; } change (temp[0],temp[p]); Return this element to the original}//and the 5th position on the number Exchange if (P! = 5)//The following steps are the same as the No. 0 position of the interchange.                    Refer to the above comment {change (temp[p],temp[5]);                    int numx = Get_num (temp);                                    if (!bo[numx][p]) {if (numx = = goal) {                                    printf ("%d\n", step+1);                                Exit (0);                            } Bo[numx][p] = true;                            tail++;                    tail = tail% QQ;        for (int i = 0;i <= 5;i++) dl1[tail][i] = Temp[i];                            Dl2[tail] = step+1;                        Dl3[tail] = p;                } change (temp[5],temp[p]);            }//The number +1 at the cursor position; int temp1[6];            If the number at the cursor position is 8 or 9, then it will be a bit cumbersome to revert to the original, so just manipulate the temp1 instead of the previous number.            for (int i = 0;i <= 5;i++) temp1[i] = Temp[i];                    if (temp1[p]! = 9)//If the number of the cursor is not 9 then increment this number by {Temp1[p] + = 1; int num2 = Get_num (TEMP1); Turn into a single shaping if (!bo[num2][p])//Weight {if (num2 = = goal)//                                If equal to the target, the output.                                    {printf ("%d\n", step+1);                                Exit (0); } Bo[num2][p] = true; Sentenced to heavy tail++; Put the cursor and the number and number of stepsInformation in queue tail = tail%qq;                            for (int i = 0;i <= 5;i++) dl1[tail][i] = Temp1[i];                            Dl2[tail] = step+1;                        Dl3[tail] = p; }}//The number at the cursor position-1;            Similar to the case of +1, refer to the comment above for (int i = 0;i <= 5;i++) temp1[i] = Temp[i];                    if (temp1[p]! = 0) {Temp1[p]-= 1;                    int num3 = Get_num (TEMP1);                                    if (!bo[num3][p]) {if (num3 = = goal) {                                    printf ("%d\n", step+1);                                Exit (0);                            } Bo[num3][p] = true;                            tail++;                            tail = tail%qq;  for (int i = 0;i <= 5;i++)                              Dl1[tail][i] = Temp1[i];                            Dl2[tail] = step+1;                        Dl3[tail] = p;            }}//cursor to the left one position,//because there is a cursor in the position of the 0,1, after the left shift is 1 not good to restore so or the PP cursor operation int pp = p; if (pp! = 0)//If it is not the leftmost operation (this step is meaningless if it is the leftmost operation) {pp--;//The cursor moves left if (!bo [NUM0] [PP])                            Sentence weight {bo[num0][pp] = true; tail++;                            Don't judge whether the number is a target this time, because moving the cursor number will not change. tail = tail% QQ;                            Add numbers and cursor information to the queue.                            for (int i = 0;i <= 5;i++) dl1[tail][i] = Temp[i];                            Dl2[tail] = step +1;                        Dl3[tail] = pp;  }}//The cursor moves right one position pp = p;            The same as the cursor left, note the reference above.   if (pp! = 5) {pp++;                 if (!bo[num0][pp]) {bo[num0][pp] = true;                            tail++;                            tail = tail%qq;                            for (int i = 0;i <= 5;i++) dl1[tail][i] = Temp[i];                            Dl2[tail] = step + 1;                        Dl3[tail] = pp;    }}}}int Main () {//freopen ("F:\\rush.txt", "R", stdin);    Input_data ();    Get_ans (); return 0;}


"8001" Enter password

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.