Wiki OI 10,044 Sub-company chess

Source: Internet
Author: User
Tags bool hash

Title Link: http://wikioi.com/problem/1004/

Algorithms and ideas: Wide search +hash+ State compression

See note.

#include <stdio.h> #include <stdlib.h> #include <queue> #include <map> using namespace std;                 const int Dx[]={1,-1, 0, 0};
Initializes the movable direction of the const int dy[]={0, 0, 1, 1};                                            Information about the struct status//checkerboard state, including converting the 16-bit 3 binary number into a decimal number result hash, moving the steps step, {
	Is by who moves the chess piece obtains at this time this chessboard state, Last=1 (He Zifang), last=2 (white son square) int hash, step, last;                        int map[6][6]; 
Stores state information for the board.
}first;                Queue<struct status> Q; With BFS, queue map<int, bool> h[3]; Hash lookup. H[1][X] records the movement of the sunspot side of the information, H[1][x]=0, said that has not moved to this state, H[1][x]=1, said before has arrived, do not have to enter the team int gethash (status a)//to the state of the board at this time 
(all with 0,1,2 means that none of the three binaries) is converted to a decimal number, which is used for hash lookups.
	{int res = 0;
	int k = 1;
	int I, J;
			for (i = 1; I <= 4, i++) for (j = 1; J <= 4; j + +) {res + = a.map[i][j] * k;
		K *= 3;
} return res; 
} bool Check (status a)//to determine if the checkerboard state is already a target state at this time.
	{int I, J; for (i = 1; I <= 4; i++)//Horizontal connection 4 {bool FLAG = TRUE;
		for (j = 2; J <= 4; j + +) if (a.map[i][j-1]! = a.map[i][j]) flag = false;
	if (flag) return true;
		} for (i = 1;i <= 4; i++)//vertically connected to 4 {bool flag = true;
		for (j = 2; J <= 4; j + +) if (a.map[j][i]! = a.map[j-1][i]) flag = false;
	if (flag) return true; if (a.map[1][1] = = a.map[2][2])//diagonal if (a.map[2][2] = = A.map[3][3]) if (a.map[3][3] = = A.map[4][4]) re
	Turn true; if (a.map[1][4] = = a.map[2][3])//diagonal if (a.map[2][3] = = a.map[3][2]) if (a.map[3][2] = = a.map[4][1]) retur
	n true;
return false; 
} void Move (status now, int x, int y, int k)//Move one step to generate a new checkerboard state, K for Move direction.
	{Status TMP = Now;                     int tmpx = x + dx[k];
	Move one step, assign a new coordinate int tmpy = y + dy[k];         if (tmpx < 1 | | tmpx > 4) return; 
		Judging the boundary, moving one step, and cross the boundary, this strategy is not possible.
			if (Tmpy < 1 | | tmpy > 4) return;        if (tmp.map[tmpx][tmpy] = = tmp.last)//have the upper side problem, if the moving pieces are the last person's (each other's), for example, Bai Fanggang moved, at this time to move one step back to their own;                      Moving is just the white son, then not, because they can not move the other's pieces.                Tmp.last = 3-tmp.last;    The above three kinds of mobile situation is no problem, the new state of the mobile TMP, re-copied to the last, marked as this state is its own (black) mobile swap (Tmp.map[tmpx][tmpy], tmp.map[x][y]); 
	Spaces and Tmp.map[tmpx][tmpy] (sunspots or whites) exchange positions.                Tmp.hash = Gethash (TMP); 
	The new state after the move, calculates the decimal number corresponding to the 16-bit three-digit binary number.                       	 	tmp.step++; 
	Mobile success, step number plus one. 
	if (check (TMP))//If the new state after the move is exactly the target state, you can output the result.
		{printf ("%d", tmp.step);
	Exit (0); } if (!h[tmp.last][tmp.hash])//If the new state has not been marked before, it can be queued. H[tmp.last][tmp.hash] For people who move this chess. 
	The new state of the build belongs to him. 
		{//For example, Tmp.last=1, indicates that the new state is moved by black, corresponding to find his corresponding hash state.       H[tmp.last][tmp.hash] = 1; Join the team and re-assign values.              Indicates that Q.push (TMP) has been present in this state; 
	Team.     }} void BFs () {First.hash = Gethash (first);                  The decimal number corresponding to the first state first.last = 1;  Because whoever gets down first is OK. So start to get into the team of two elements. 
	Note that the first.last is different.
	Q.push (first);
	First.last = 2;
	Q.push (first); while (!
		Q.empty ()) {status now; now = Q.Front ();
		Q.pop ();
		int x1 =-1, x2 =-1, y1 =-1, y2 = 1;
		int I, j, K;  for (i = 1; I <= 4, i++) for (j = 1; J <= 4; j + +)//search for two spaces coordinates if (!now.map[i][j]) {if  (x1 = =-1 && y1 = =-1)//x1,y1;        
						X2,y2 is the coordinate of two spaces {x1 = i;
					Y1 = j;
						} else {x2 = i;
					y2 = j; 
		}} for (k = 0; k < 4; k++)//A chessboard has two spaces, so both search for four directions together.
		 	{Move (now, X1, Y1, k);
		Move (now, X2, Y2, k);
  	}}} int main () {int i, J;
		for (i = 1; I <= 4; i++) {char s[10];
		scanf ("%s", s);                          for (j = 0; J < 4; j + +)//convert each piece to 0 (space), 1 (sunspots), 2 (white) (three-in), at which time each lattice of the chessboard is 0,1,2. This is a 16-bit 3 binary number, corresponding to a decimal number. 
			The corresponding checkerboard state can then be found by hashing the decimal number of the chessboard.
			if (s[j] = = ' B ') first.map[i][j+1] = 1;
		if (s[j] = = ' W ') first.map[i][j+1] = 2;
	}} BFS ();
return 0; }



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.