#define_crt_secure_no_warnings#include<iostream>#include<algorithm>#include<cstdio>#include<cstdlib>#include<queue>#include<cstring>#include<map>#include<Set>using namespacestd;Const intMAXN =4;//Movable Directionintdir[4][2] = { { -1,0}, {0,1}, {1,0}, {0, -1 } };structStatus {//last = 1: sunspot, last = 2: White//including the result hash of converting 16-bit 3 into a decimal number, moving steps Step inthash, step, last;//Hash: to describe the position of a pawn, but not to repeat intmap[maxn][maxn];} Map;/** h[1][x] Record the movement of the sunspot to arrive information * h[1][x]=false, indicating that the status has not been moved * h[1][x]=true, indicating that has been reached before, do not enter the team*/Map<int,BOOL> h[3];queue<Status> que;//BFS, QueuevoidInput ();//input DatavoidSolve ();//Start functionBOOLJudgeintRintc);//Judging out of boundsBOOLCheck (Status a);//determine if the target state is met//Move one step, create a new pawn position, K for the moving directionvoidMove (Status now,intRintCintk);voidFindspace (Conststatus& now,int&r,int&c,int&r2,int&c2);//where to find spaces//The state of the board at this time, all with 0,1,2 for each bit of the three, converted into a decimalintGethash (Status a);//set hashcode on the current chess boardvoidBFS ();//Wide Search BFSvoidinput () {Chars[Ten]; /** Change each piece to a space-0, sunspot-1, white-2 (three-in) * This is a 16-bit 3 binary number, corresponding to a decimal number, and then by hashing the * Chessboard's decimal number, You can find the corresponding checkerboard State*/ for(inti =0; I < maxn; i++) {scanf ("%s", s); for(intj =0; J < maxn; J + +) { if(s[j] = ='B') map.map[i][j] =1; if(s[j] = ='W') map.map[i][j] =2; } }}BOOLJudgeintRintC) { return(r >=0&& R < maxn) && (c >=0&& C <maxn);}BOOLCheck (Status a) {BOOLFlag =true; //Horizontal connection of 4 for(inti =0; I < maxn; i++) {flag=true; for(intj =0; J < maxn-1; J + +) { if(a.map[i][j]! = A.map[i][j +1]) {flag=false; } } if(flag)return true; } //vertically connected to 4 for(inti =0; I < maxn; i++) {flag=true; for(intj =0; J < maxn-1; J + +) { if(a.map[j][i]! = A.map[j +1][i]) {flag=false; } } if(flag)return true; } Flag=true; for(inti =0; I < maxn-1; i++) { if(a.map[i][i]! = A.map[i +1][i +1]) {flag=false; } } if(flag)return true; Flag=true; for(inti = maxn-1; i >0; i--) { if(a.map[i][i]! = a.map[i-1][i-1]) {flag=false; } } if(flag)return true; //It's not even a 4-son, false. return false;}//all using 0,1,2 to represent each bit of the three-in, converted into a decimal//use a hash to findintGethash (Status A) {intres =0; intK =1; for(inti =0; I <4; i++) { for(intj =0; J <4; J + +) {res+ = a.map[i][j] *k; K*=3; } } returnres;}voidFindspace (Conststatus& now,int&r1,int&c1,int&r2,int&C2) { for(inti =0; I < maxn; i++) { for(intj =0; J < maxn; J + +) { if(!Now.map[i][j]) { if(R1 = =-1&& C1 = =-1) {r1= i; C1 =j; } Else{R2= i; C2 =j; } } } }}/*each step of the move, you need to do it * 1. Cross check * 2. Move the piece, and mark the move, and set the next move of the pawn type * 3. Whether to complete the target check * 4. Set the hashcode* 5 for the new chessboard if it is not completed. check if the state of the pawn has been present, not queued, and marked To move one step, create a new pawn position, K for the moving direction*/voidMove (Status now,intRintCintk) {Status tmp=now ; intTMPX = r + dir[k][0]; intTmpy = C + dir[k][1]; //judge whether to cross the border | | Have a hand, you can't move your son twice//(e.g., The first move of the white child, the second, the white can move the white son, is the wrong behavior if(!judge (tmpx, Tmpy) | | tmp.map[tmpx][tmpy] = =Tmp.last)return; Tmp.last=3-tmp.last;//take the inverse, the last white (1), this time on the sunspots (2)Swap (tmp.map[tmpx][tmpy], tmp.map[r][c]);//swap pieces and blank positionsTmp.hash = Gethash (tmp);//Reset Hashcodetmp.step++;//Mobile success, Step number +1 if(check (tmp)) {printf ("%d\n", tmp.step); Exit (0);//End the entire program }//represents the current state of a single piece of Tmp.last-whether it has been moved//false-not moved, can queue if(!H[tmp.last][tmp.hash]) {h[tmp.last][tmp.hash]=true;//flag This state has already occurredQue.push (tmp);//Queue }}voidBFS () {map.hash= Gethash (Map);//Hashcode of the first state checkerboard//because whoever gets down first, two pieces should be on the Team.Map.last =1; Que.push (Map); Map.last=2;//BlackQue.push (Map); while(!Que.empty ()) {Status now; now=Que.front (); Que.pop (); intR1 =-1, C1 =-1, R2 =-1, C2 =-1; Findspace (now, r1, c1, r2, c2); //Find space Position//a chessboard has two spaces, so both of them search for four directions together. for(intK =0; K < maxn; k++{Move (now, r1, c1, k); Move (now, r2, c2, k); } }}voidSolve () {input (); BFS ();}intmain () {solve (); return 0;}
BFS Algorithm Good Practice ~
Refer to this blog: http://blog.csdn.net/re_cover/article/details/9034219
BFS Search Algorithm Application _codevs 1004