Test instructions: In the 4*8 diagram, give you 4 types of cards, each card serial number 1-7, such as 42 for the 4th card of the second, now give you 4*7 total 28, while the leftmost column starts empty, the first step you have to do is to each type of 1th card from top to down in this column empty position, Then, in the new empty position, you have to pick the next card on the left side of the empty seat, and if not, you cannot manipulate it.
Solution: The status of a lot of questions, and how to indicate a state has been searched. Then the matrix to do a conversion, the current matrix in line expansion, to 2 as a decimal number (maximum 2^32, so long long), the next storage of these integers, with map,set should also be able, perhaps a little slower, I use a simple hash
#include <iostream>#include <cstdlib>#include <cmath>#include <algorithm>#include <cstring>#include <cstdio>#include <queue>#include <set>#include <stack>#define CL (A, B) memset (A,b,sizeof (a));#define LL Long Long#define P pair<int,int>#define X First#define Y Second#define PB Push_back#define OUT (x) cout<<x<<endl;using namespace STD;Const intmaxn= -;Const intinf=9999999;Const intMod=100007;structnode{intx[4],y[4];position of//four spaces inta[4][8];//The state of the diagram for each step intStep//Number of steps};intaim[][8]={//The status of the target{ One, A, -, -, the, -, -,0},{ +, A, at, -, -, -, -,0},{ to, +, -, the, *, $,Panax Notoginseng,0},{ A, the, +, -, $, $, -,0}}; vector<LL>HASH_[MOD];//hsahvoidInsert (LL key) {//hash Insert Function intPos= (key%mod+mod)%mod; HASH_[POS].PB (key);}BOOLFind (LL key) {//hash Lookup Function intPos= (key%mod+mod)%mod;intN=hash_[pos].size (); for(intI=0; i<n;i++) {if(Hash_[pos][i]==key)return true; }return false;} LL aimencode=98430874871LL/// This number is calculated using the Encode function for the aim arrayLL Encode (intx[][8]){/// The matrix is expanded in rows, followed by two as the base, into the decimal number, and then the hashLL num=0; for(intI=0;i<4; i++) { for(intj=0;j<8; j + +) {num+=x[i][j]* (1Ll<< (i*8+J)); } }returnNum;} Node St;intBFS () { queue<node>Q st.step=0; Q.push (ST); Insert (Encode (ST.A));//Access tag while(!q.empty ()) {node Tt=q.front (); Q.pop (); for(intI=0;i<4; i++) {//expansion of four spaces at a timeNode S=tt;intx=s.a[s.x[i]][s.y[i]-1];if(%Ten==7|| x==0)Continue; x + +; for(intj=0;j<4; j + +) for(intk=0;k<8; k++)if(s.a[j][k]==x) {swap (s.a[j][k],s.a[s.x[i]][s.y[i]); LL Tmp=encode (S.A.);if(Tmp==aimencode) {returns.step+1; }if(!find (TMP)) {Insert (TMP); s.step++; S.x[i]=j; S.y[i]=k; Q.push (s); }GotoA } A:; } }return-1;}intMain () {intTscanf("%d", &t); while(t--) {//cl (st.a,0); for(intI=0; i<mod;i++) hash_[i].clear (); st.a[0][0]=st.a[1][0]=0; st.a[2][0]=st.a[3][0]=0;intnum=0; for(intI=0;i<4; i++) { for(intj=1; j<=7; j + +) {intXscanf("%d", &st.a[i][j]); X=ST.A[I][J];if(x== One) {Swap (st.a[0][0],ST.A[I][J]); St.x[num]=i;st.y[num++]=j; }Else if(x== +) {Swap (st.a[1][0],ST.A[I][J]); St.x[num]=i;st.y[num++]=j; }Else if(x== to) {Swap (st.a[2][0],ST.A[I][J]); St.x[num]=i;st.y[num++]=j; }Else if(x== A) {Swap (st.a[3][0],ST.A[I][J]); St.x[num]=i;st.y[num++]=j; } } }///end of if(Encode (ST.A) ==aimencode) {puts("0");Continue; }printf("%d\n", BFS ()); }return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HDU1067 Gap (bfs+ HASH pruning, matrix represented by a number)