ACM poj 1753 flip game

Source: Internet
Author: User
Tags rounds
Flip game
Time limit:1000 ms Memory limit:65536 K
Total submissions:14735 Accepted:6321

Description

Flip game is played on a rectangular 4x4 field with two-sided pieces placed on each of its 16 squares. one side of each piece is white and the other one is black and each piece is lying either it's black or white side up. each round you flip 3 to 5 pieces, thus changing the color of their upper side from black to white and vice versa. the pieces to be flipped are chosen every round according to the following rules:
    1. Choose any one of the 16 pieces.
    2. Flip the chosen piece and also all adjacent pieces to the left, to the right, to the top, and to the bottom of the chosen piece (if there are any ).

Consider the following position as an example:

Bwbw
Wwww
Bbwb
Bwwb
Here "B" denotes pieces lying their black side up and "W" denotes pieces lying their white side up. if we choose to flip the 1st piece from the 3rd row (this choice is shown at the picture), then the field will become:

Bwbw
Bwww
Wwwb
Wwwb
The goal of the game is to flip either all pieces white side up or all pieces black side up. you are to write a program that will search for the minimum number of rounds needed to achieve this goal.

Input

The input consists of 4 lines with 4 characters "W" or "B" each that denote game field position.

Output

Write to the output file a single integer number-the minimum number of rounds needed to achieve the goal of the game from the given position. if the goal is initially achieved, then write 0. if it's impossible to achieve the goal, then write the word "impossible" (without quotes ).

Sample Input

 
Bwwbbbwbbwwbbwww

Sample output

 
4

Source

Northeastern Europe 2000.

Question:There is a 4*4 square, with a piece of chess in each square. One side of the chess piece is white and the other side is black. The rule of the game is to select one of the 16 pieces each time, and turn the selected piece along with the pieces around it. When all the pieces are in the same color up, the game is complete. Given an initial state, you must output the minimum number of times the game is flipped to complete. If the initial state has reached the expected value, 0 is output. If the game cannot be completed, output impossible.

Main ideas:

1. If a 4*4 array is used to store each state, it is not only a large storage space, but also inconvenient to record when the State is in poor state. Because each piece has only two States, binary 0 and 1 can be used to represent the status of each piece. Then, the status of the Board can be uniquely identified by a 16-bit integer. The flip operation can also be completed through bit operations. Obviously, when the checker status ID is 0 (full White) or 65535 (full black), the game ends.

2. there are sixteen operations for each status of the checker. First, you must determine whether the operations are completed after these sixteen operations. If not, then perform the preceding operations on the results of these 16 Operations respectively. It is obvious that the queue is used for storage. In addition, during the flip process, it may return to a previous state, and such a duplicate state should not be requeued, therefore, the isvis [I] array is maintained to determine whether the status of ID = I has exists before. If not, the isvis [I] array is added to the queue. If the game cannot be completed, the status will inevitably form a loop. Because the repeat status will not be re-queued, the final queue will be an empty queue.

3. Because 0 ^ 1 = ^ 1 = 0, the flip operation can be completed through an exclusive or operation, and the flip position can be determined by shift.

 

# Include  <  Stdio. h  >  
# Include < Stdio. h >
# Include < String . H >
# Include < Iostream >
Using Namespace STD;
# Define Maxn 65536
Int Que [maxn * 2 ];
Int Front, rear;
Bool Used [maxn];
Int Step [maxn];
Bool Find0;
Void BFS ( Int P)
{
Find0 = False ;
Step [p] = 0 ;
If (P = 0 | P = 65535 )
{
Cout < " 0 " < Endl;
Find0 = True ;
Return ;
}
Memset (used, False , Sizeof (Used ));
Rear = Front = 0 ;
Que [rear ++ ] = P;
Used [p] = True ;
While (Front < Rear)
{
Int TMP = Que [Front ++ ];
Int T = TMP;
// Step ++;
For ( Int I = 0 ; I < 16 ; I ++ )
{
TMP = T;
TMP ^ = 1 < ( 15 - I );
If (I % 4 = 0 ) TMP ^ = 1 < ( 14 - I );
Else If (I % 4 = 3 ) TMP ^ = 1 < ( 16 - I );
Else TMP ^ = 5 < ( 14 - I );

If (I <= 3 ) TMP ^ = 1 < ( 15 - I - 4 );
Else If (I > = 12 ) TMP ^ = 1 < ( 15 - I + 4 );
Else
{
TMP ^ = 1 < ( 15 - I - 4 ); TMP ^ = 1 < ( 15 - I + 4 );
}
If (TMP = 0 | TMP = 65535 )
{
Cout < Step [T] + 1 < Endl;
Find0 = True ;
Return ;
}
If (Used [TMP] = False )
{
Que [rear ++ ] = TMP;
Used [TMP] = True ;
Step [TMP] = Step [T] + 1 ;
}
}
}
}
Int Main ()
{
Int I, J;
Char Color;
Int ID = 0 ;
For (I = 0 ; I < 4 ; I ++ )
For (J = 0 ; J < 4 ; J ++ )
{
CIN > Color;
ID <= 1 ;
If (Color = ' B ' ) ID + = 1 ;

}
BFS (ID );
If (Find0 = False )
{
Cout < " Impossible " < Endl;
}
// System ("pause ");
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.