ACM/ICPC BFS (offline) + Kang expansion open (hdu1430-Magic Board)

Source: Internet
Author: User

Magic plate problem, a classic kang expansion open +bfs problem, for the sake of convenience, I use String class to represent the string, which was seldom used in the string class (because it is not efficient, and relative to the char array related functions are relatively familiar), So there are a lot of things that are easy to overlook.

For Kang expansion to open not very familiar with the department can first see a blog: http://blog.csdn.net/zhongkeli/article/details/6966805

  

As for the Sting class, it is important to note that when assigning a value, the assignment position cannot have an unassigned part with the first position .

The topic needs to change the idea of the place is: we need to convert the original magic plate and the target Magic plate to the form of a new target Magic plate, so that offline (to play the table) is enough.

The specific code is as follows:

1 //Magic Board Problem-BFS (offline) + Kang expansion open2 //time:38ms memory:6484k3#include <iostream>4#include <string>5#include <queue>6#include <algorithm>7 using namespacestd;8 9 #defineMAX 40321Ten  One intfac[8] = {1,1,2,6, -, -,720,5040};//Order Multiplier Group A  - intV[max];//access token - stringAns[max];//Offline Table the  - structboard{ -     intVal//Hash Value -     stringstr; + }; -  + //Kang Expansion Open (Hash) A intContor (stringstr) at { -     intnum =0;//Hash Value -      for(inti =0; I <8; i++) -     { -         intTMP =0;//The position in the subsequent sequence (ascending) -          for(intj = i +1; J <8; J + +) in             if(Str[j] < str[i]) tmp++; -num + = tmp*fac[7-i]; to     } +     returnnum; - } the  * //Offline play table (BFS) $ voidInit ()Panax Notoginseng { -Queue<board>Q; the Board T, tmp; +T.str = Tmp.str ="12345678";//Initial Array AT.val =Contor (T.STR); theV[t.val] =1; + Q.push (t); -      while(!Q.empty ()) { $t =Q.front (); $ Q.pop (); -  -         //A: Exchange lines the          for(inti =0; I <8; i++) -tmp.str[(i +4) %8] =T.str[i];Wuyi  theTmp.val =Contor (TMP.STR); -         if(!V[tmp.val]) { WuV[tmp.val] =1; -Ans[tmp.val] = Ans[t.val] +'A'; About Q.push (TMP); $         } -  -         //B: Loop right Shift -          for(inti =0; I <4; i++) Atmp.str[(i +1) %4] =T.str[i]; +          for(inti =4; I <8; i++) thetmp.str[(i +1) %4+4] =T.str[i]; -  $Tmp.val =Contor (TMP.STR); the         if(!V[tmp.val]) { theV[tmp.val] =1; theAns[tmp.val] = Ans[t.val] +'B'; the Q.push (TMP); -         } in  the         //C: Center clockwise rotation theTmp.str =T.str; Abouttmp.str[1] = t.str[5]; tmp.str[2] = t.str[1]; thetmp.str[6] = t.str[2]; tmp.str[5] = t.str[6]; the  theTmp.val =Contor (TMP.STR); +         if(!V[tmp.val]) { -V[tmp.val] =1; theAns[tmp.val] = Ans[t.val] +'C';Bayi Q.push (TMP); the         } the     } - } -  the intMain () the { theInit ();//BFS the     stringts, TE; -      while(Cin >> TS >>te) the     { the         /*Convert to order Magic plate*/ theSwap (ts[5], ts[6]);94Swap (ts[4], ts[7]); theSwap (te[5], te[6]); theSwap (te[4], te[7]); the 98         /*converts the starting magic plate to a standard magic plate, and the corresponding target magic plate is also changed*/ About         Chartmp[9]; -          for(inti =0; I <8; i++)101Tmp[ts[i]-'0'] = i +'1';102          for(inti =0; I <8; i++)103Te[i] = tmp[te[i]-'0'];104  thecout << ans[contor (TE)] <<Endl;106     }107 108     return 0;109}

ACM/ICPC BFS (offline) + Kang expansion open (hdu1430-Magic Board)

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.