10,044 Sub-company chess
time limit: 1 sspace limit: 128000 KBtitle level: Golden Gold SolvingTitle 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
Category labels
Tags Click here to expandBreadth-First search depth-first search iterative search search
#include <iostream>#include<cstdio>#include<queue>using namespacestd;Chara[6][6];intb[6][6];structszlq{inta[6][6]; intb; ints; voidprint () { for(inti =1; I <=4; I ++,puts ("")) for(intj =1; J <=4; j + +) printf ("%d", A[i][j]); }};queue<szlq>Q;intans,inf=21474836;BOOLjudge (Szlq a) {inte; for(intI=1; i<=4; i++) {e=a.a[i][1]; if(a.a[i][2]==e&&e==a.a[i][3]&&e==a.a[i][4]) return 1; E=a.a[1][i]; if(e==a.a[2][i]&&e==a.a[3][i]&&e==a.a[4][i])return 1; } e=a.a[1][1]; if(e==a.a[2][2]&&e==a.a[3][3]&&e==a.a[4][4]) return 1; E=a.a[4][1]; if(e==a.a[3][2]&&e==a.a[2][3]&&e==a.a[1][4]) return 1; return 0;}intHuan (inta) { returna==1?2:1;}voidBFS (Szlq a) {if(judge (a) = =1) {ans=A.S; return; } SZLQ W; for(intI=1; i<=4; i++) for(intj=1; j<=4; j + +) { if(!A.a[i][j]) { if(a.a[i][j-1]==a.b) {W=A; W.B=Huan (A.B); W.S+=1; Swap (W.a[i][j],w.a[i][j-1]); Q.push (w); } if(a.a[i][j+1]==a.b) {W=A; W.B=Huan (A.B); W.S+=1; Swap (W.a[i][j],w.a[i][j+1]); Q.push (w); } if(a.a[i+1][j]==a.b) {W=A; W.B=Huan (A.B); W.S+=1; Swap (W.a[i][j],w.a[i+1][j]); Q.push (w); } if(a.a[i-1][j]==a.b) {W=A; W.B=Huan (A.B); W.S+=1; Swap (W.a[i][j],w.a[i-1][j]); Q.push (w); } } }}intMain () { for(intI=1; i<=4; i++) scanf ("%s", a[i]+1); for(intI=1; i<=4; i++) for(intj=1; j<=4; j + +) { if(a[i][j]=='B') B[i][j]=1; if(a[i][j]=='W') B[i][j]=2; } SZLQ KS,DQ; for(intI=1; i<=4; i++) for(intj=1; j<=4; j + +) Ks.a[i][j]=B[i][j]; ks.b=1; ks.s=0; Q.push (KS); ks.b=2; ks.s=0; Q.push (KS); Ans=inf; while(ans==inf) {DQ=Q.front (); BFS (DQ); Q.pop (); } printf ("%d", ans);}
10,044 Sub-company chess