Pku acm 1753 first BSF Algorithm

Source: Internet
Author: User
Tags rounds

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

bwwb bbwb bwwb bwww

Sample output

4

Solution:
This is a search question. It is important to compare the expression methods of each State in the search question design. Different States are encoded according to certain rules, and then switched
 
Under the same encoding.
If we think of 16 grids as a hexadecimal number, the first row is the highest bit, the last row is the lowest Bit, W indicates 0, B Indicates 1, in this case, all the statuses are 0-65535. We can use an array to save the minimum steps from the current status to any status. When you perform the next search, you can flip up, down, and left to right, and use the XOR of the corresponding bit to implement the operation, and then enter the next state.
The Code is as follows:

#include <iostream>#include <queue>using namespace std;#define MAXSTATUS 65535#define ALLSAME(X) X==0 || X == MAXSTATUSunsigned int oxrMatrix[] = {0xC800,0xE400,0x7200,0x3100,0x8C80,0x4E40,0x2720,0x1310,0x08c8,0x04E4,0x0272,0x0131,0x008c,0x004E,0x0027,0x0013,};unsigned int allStatus[MAXSTATUS];int bsf(unsigned int c){int i;unsigned int tmp;queue<unsigned int> statuses;statuses.push(c);while(!statuses.empty()){c = statuses.front();statuses.pop();for( i = 0; i < 16; i++){tmp = c ^ oxrMatrix[i];if(ALLSAME(tmp)){return allStatus[c] + 1;}if(!allStatus[tmp]) // current status is not visited before{allStatus[tmp] = allStatus[c] + 1;statuses.push(tmp);}}}return 0;}int main(){int i;unsigned int status = 0;char tmpC;int nStep;while(cin>>tmpC){if(tmpC == 'b'){status = 1;}for( i = 1; i < 16; i++){cin>>tmpC;status *= 2;if(tmpC == 'b'){status += 1;}}if(ALLSAME(status)){cout<<"0\n"<<endl;}else{memset(allStatus,0,sizeof(allStatus));nStep = bsf(status);if(!nStep){cout<<"Impossible\n"<<endl;}else{cout<<nStep<<endl;}}}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.