Question: on the 8*8 checkers board, you are given two States, each of which has four points to determine whether a status can reach another status within 8 steps.
Analysis: Given the start and end statuses, it is a typical bidirectional wide search.
From the beginning to the end, you can perform a wide search. Ideally, you can reduce the search volume by 1/2 to increase the search speed.
The key to the problem is to save the status. Because the four chess pieces are the same and the size of the Board is only 8x8, after sorting by coordinates, each three digits of a binary string are used to store one coordinate of a piece. A total of 24 digits are required.
Improvements to the bidirectional breadth search algorithm:
Slightly modify the control structure. Each while loop only expands one of the fewer nodes in both directions, which can maintain a certain balance between the two sides, this reduces the total number of extended nodes and speeds up search.
ThisCodeI wrote a little frustrated. I wrote it using two BFs, not the same one, but it also saves the trouble. The first double-width, debugging for one afternoon.
View code
# Include <iostream>
# Include <algorithm>
# Include <queue>
# Include <bitset>
# Define N 8
Using Namespace STD;
Bitset < 20000010 > Vis1, vis2;
Int Dir [ 4 ] [ 2 ] = {{ 0 , 1 },{ 0 ,- 1 },{ 1 , 0 },{- 1 , 0 }};
Bool Map [N] [N], flag;
Struct Point
{
Int X, Y;
};
Struct Node
{
Point P [ 4 ];
};
Node start, end1;
Queue <node> q1, Q2;
Bool CMP (point a, point B)
{
If (A. X! = B. X)
Return A. x <B. X;
Return A. Y <B. Y;
}
Void Get_map (point P [])
{
Memset (map, 0 , Sizeof (MAP ));
For ( Int I = 0 ; I < 4 ; I ++)
Map [p [I]. x] [p [I]. Y] = 1 ;
}
Int Get_hash (point P [])
{
Sort (p, p + 4 , CMP );
Int A = 0 , J = 7 ;
For ( Int I = 0 ; I < 4 ; I ++)
{
A | = (P [I]. x <(J * 3 ));
A | = (P [I]. Y <(j- 1 )* 3 );
J-= 2 ;
}
Return A;
}
Void Bfs1 ()
{
Int A;
Int Size = q1.size ();
While (Size --)
{
Node q = q1.front (), temp;
Q1.pop ();
Get_map (Q. P );
For ( Int I = 0 ; I < 4 ; I ++)
For ( Int K = 0 ; K < 4 ; K ++)
{
Temp = Q;
Int X = Q. P [I]. x + dir [k] [0 ];
Int Y = Q. P [I]. Y + dir [k] [ 1 ];
If (X < 0 | X> = n | Y < 0 | Y> = N)
Continue ;
Temp. P [I]. x = X;
Temp. P [I]. Y = y;
If (Map [x] [Y])
{
Int X1 = x + dir [k] [0 ];
Int Y1 = Y + dir [k] [ 1 ];
If (X1 < 0 | X1> = n | Y1 < 0 | Y1> = n | map [X1] [Y1])
Continue ;
Temp. P [I]. x = x1; temp. P [I]. Y = Y1;
}
A = get_hash (temp. P );
If (Vis1 [a]) Continue ;
If (Vis2 [a]) {
Flag = True ;
Return ;
}
Vis1 [a] = True ;
Q1.push (temp );
}
}
}
Void Bfs2 ()
{
Int A;
Int Size = q2.size ();
While (Size --)
{
Node q = q2.front (), temp;
Q2.pop ();
Get_map (Q. P );
For ( Int I = 0 ; I < 4 ; I ++)
For ( Int K = 0 ; K < 4 ; K ++)
{
Temp = Q;
Int X = temp. P [I]. x + dir [k] [ 0 ];
Int Y = temp. P [I]. Y + dir [k] [ 1 ];
If (X < 0 | X> = n | Y < 0 | Y> = N)
Continue ;
Temp. P [I]. x = X;
Temp. P [I]. Y = y;
If (Map [x] [Y])
{
Int X1 = x + dir [k] [ 0 ];
Int Y1 = Y + dir [k] [ 1 ];
If (X1 < 0 | X1> = n | Y1 < 0 | Y1> = n | map [X1] [Y1])
Continue ;
Temp. P [I]. x = x1; temp. P [I]. Y = Y1;
}
A = get_hash (temp. P );
If (Vis2 [a]) Continue ;
If (Vis1 [a]) {
Flag = True ; Return ;
}
Vis2 [a] = True ;
Q2.push (temp );
}
}
}
Int Main ()
{
While (Scanf ( " % D " , & Start. P [ 0 ]. X, & start. P [ 0 ]. Y) = 2 )
{
Start. P [ 0 ]. X --;
Start. P [ 0 ]. Y --;
For ( Int I = 1 ; I < 4 ; I ++)
{
Scanf ( " % D " , & Start. P [I]. X, & start. P [I]. y );
Start. P [I]. X --; start. P [I]. y --;
}
For ( Int I = 0 ; I < 4 ; I ++)
{
Scanf (" % D " , & End1.p [I]. X, & end1.p [I]. y );
End1.p [I]. X --;
End1.p [I]. y --;
}
While (! Q1.empty ())
Q1.pop ();
While (! Q2.empty ())
Q2.pop ();
Vis1.reset ();
Vis2.reset ();
Int A;
A = get_hash (start. P );
Vis1 [a] =True ;
Int B = get_hash (end1.p );
If (A = B ){
Printf ( " Yes \ n " );
Continue ;
}
Vis2 [B] = True ;
Q1.push (start); q2.push (end1 );
Int Step = 8 ;
Flag = False ;
While (Step --)
{
If (Q1.size () <= q2.size ())
Bfs1 ();
Else Bfs2 ();
If (FLAG) Break ;
}
If (FLAG) printf ( " Yes \ n " );
Else Printf ( " No \ n " );
}
Return 0 ;
}