44 sub-company chess
time limit: 1 sspace limit: 128000 KBtitle level: Golden GoldTitle Description
Description
On a 4*4 board placed 14 pieces, of which there are 7 white pieces, 7 black pieces, two blank areas, any one black and white pieces can be moved up and down four direction to the adjacent space, this is called a step, black and white sides alternately moves, either party can go first , If at some point a piece of any color is formed into four lines (including a slash), the position is the target.
Enter a description
Input Description
Read from the file into a 4*4 of the initial game, black pieces with B, white pieces with W, the space zone with O is indicated.
Output description
Output Description
Moves the number of steps to the target game with a minimum number of steps.
Sample input
Sample Input
Bwbo
Wbwb
Bwbw
Wbwo
Sample output
Sample Output
5
Data range and Tips
Data Size & Hint
Hi
#include <iostream>using namespacestd;structdata{intmp[5][5];//1 white chess, 2 black chess, 0 spaces}d[ the];intnext[ the]={1,2};//The next step is black or white, 1 white, 2 black .intstep[ the];BOOLhash[4000000];intxx[4]={0,0,1,-1},yy[4]={1,-1,0,0};intt=0, w=2, flag=0;BOOLEquintA1,intA2,intA3,intA4) {if(a1!=a2| | a2!=a3| | a3!=a4| | A4!=A1)return 0;return 1;}BOOLCheck () { for(intI=1; i<=4; i++) { if(Equ (d[w].mp[i][1],d[w].mp[i][2],d[w].mp[i][3],d[w].mp[i][4]))return 1; if(Equ (d[w].mp[1][i],d[w].mp[2][i],d[w].mp[3][i],d[w].mp[4][i]))return 1; } if(Equ (d[w].mp[1][1],d[w].mp[2][2],d[w].mp[3][3],d[w].mp[4][4]))return 1; if(Equ (d[w].mp[1][4],d[w].mp[2][3],d[w].mp[3][2],d[w].mp[4][1]))return 1; return 0;}intHash () {//Hash Weight ints=0, k=1; for(intI=1; i<=4; i++) for(intj=1; j<=4; j + +) {s+=d[w].mp[i][j]*k;k*=3;} S%=3733799; if(!hash[s]) {hash[s]=1;return 1;} return 0;}BOOLPdintXinty) { intk=Next[t]; if(x>4|| Y>4|| x==0|| y==0)return 0; Else if(d[t].mp[x][y]==k)return 1; return 0;}voidSpint&a,int&B) {intt=a;a=b;b=t;}voidMoveintXinty) { for(intI=0;i<4; i++) { intp=x+xx[i],q=y+Yy[i]; if(PD (P,Q)) { for(intj=1; j<=4; j + +) for(intk=1; k<=4; k++) D[w].mp[j][k]=D[t].mp[j][k]; SP (d[w].mp[x][y],d[w].mp[p][q]); STEP[W]=step[t]+1; if(check ()) {cout<<step[w];flag=1;return;} if(Hash ()) {if(next[t]==1) next[w++]=2; if(next[t]==2) next[w++]=1; } } }}voidsearch () { while(t<W) { for(intI=1; i<=4; i++) for(intj=1; j<=4; j + +) { if(d[t].mp[i][j]==0) Move (I,J); if(flag)return; } t++; }} intMain () {Charx; for(intI=1; i<=4; i++) for(intj=1; j<=4; j + +) {cin>>x; if(x=='W') d[0].mp[i][j]=d[1].mp[i][j]=1; Else if(x=='B') d[0].mp[i][j]=d[1].mp[i][j]=2; } search (); return 0;}
Codevs 10,044 Sub-even chess BFS, hash weight