HDU 1195 (search)

Source: Internet
Author: User

Open the Lock

Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 5970 Accepted Submission (s): 2666


Problem Descriptionnow an emergent tasks for are to open a password lock. The password is consisted of four digits. Each digit are numbered from 1 to 9.
Each time, you can add or minus 1 to any digit. When add 1 to ' 9 ', the digit would change to IS ' 1 ' and when minus 1 to ' 1 ', the digit would change to be ' 9 '. You can also exchange the digit with its neighbor. Each action would take one step.

Now your task was to use minimal steps to open the lock.

Note:the leftmost digit is not the neighbor of the rightmost digit.

Inputthe input file begins with an integer T, indicating the number of the test cases.

Each test case is begins with a four digit N, indicating the initial state of the password lock. Then followed a line with Anotther four Dight M, indicating the password which can open the lock. There is a blank line after each test case.

Outputfor each test case, print the minimal steps on one line.

Sample Input21234214411119999

Sample Output24 This type of doing relatively little, or very rewarding. Mark each one, and then process each of the actions.
#include <cstdio>#include<cstring>#include<algorithm>#include<math.h>#include<queue>#include<iostream>using namespaceStd;typedefLong LongLL;BOOLvis[Ten][Ten][Ten][Ten];structnode{intv[4]; intstep;}; Node s,t;BOOL_equal (Node a,node b) { for(intI=0;i<4; i++){        if(A.v[i]!=b.v[i])return false; }    return true;} Node Operate (intX,node Now)    {Node Next;  for(intI=0;i<4; i++) {Next.v[i]=Now.v[i]; } next.step=now.step+1; if(x<4){///+        if(now.v[x]==9) next.v[x]=1; Elsenext.v[x]=now.v[x]+1; }Else if(x<8){///-        if(now.v[x%4]==1) next.v[x%4]=9; Elsenext.v[x%4]=now.v[x%4]-1; }Else{///Exchange        intA = now.v[x%4]; intb = now.v[x%4+1]; Next.v[x%4]=C; Next.v[x%4+1] =A; }    returnnext;}intBFS (Node s) {memset (Vis,false,sizeof(VIS)); Queue<Node>Q; vis[s.v[0]][s.v[1]][s.v[2]][s.v[3]]=true;    Q.push (s); S.step=0;  while(!Q.empty ()) {Node now=Q.front ();        Q.pop (); if(_equal (now,t)) {returnNow.step; }         for(intI=0;i< One; i++) {///a total of 11 operations, [1-4]+ [1-4]-exwchange[1,2][2,3][3,4]Node next=operate (I,now); if(vis[next.v[0]][next.v[1]][next.v[2]][next.v[3]]==false) {vis[next.v[0]][next.v[1]][next.v[2]][next.v[3]]=true;            Q.push (next); }        }    }    return-1;}intMain () {Chars1[5],s2[5]; inttcase; scanf ("%d",&tcase);  while(tcase--) {scanf ("%s", S1); scanf ("%s", S2);  for(intI=0;i<4; i++) {S.v[i]=s1[i]-'0'; T.v[i]=s2[i]-'0'; }        intres =BFS (s); printf ("%d\n", RES); }    return 0;}

HDU 1195 (search)

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.