位元運算-BFS-Flip Game-POJ 1753

來源:互聯網
上載者:User

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

給十六個格子標上序號

                                        0     1     2     3

                                        4     5     6     7

                                        8     9     10    11

                                        12   13   14    15

便可以採用二進位記錄不同的狀態

                      0    1    0    0    0    1    1    0    0    0    1    1    0    1
   1    1

                     15   14  13  12   11  10  9    8    7    6    5    4    3    2   
1    0

                                                  此時便記錄下了

                         14        10         9         5        4          2          1      0

最後採用十進位儲存此時的狀態 1表示‘b’,0表示‘w’,再做相應的翻轉,既0 -> 1,  1 -> 0

#include<iostream>#include<cstring>using namespace std;char ma[5][5];//用於輸入int states[65535]={0};//(核心部分)選項組因為最大為2^16-1=65535所以數組大小為65535int step[65535]={0};//記錄步數int rear=0,front=0;//記錄頭與尾bool vis[65535];//用於記錄是否搜過//輸入,並記錄初始狀態,放入隊列首位置void shuru(){    int i,state=0;    for(i=0;i<4;i++){        cin>>ma[i];        int j;        for(j=0;j<4;j++)            if(ma[i][j]=='b')                state|=1<<(i*4+j);    }    states[rear++]=state;//記錄初始狀態放入隊列首    vis[state]=true;//記錄已走過}//反轉一個,併產生影響int fanzhuan(int stat,int i){    int state=0;    state|=1<<i;    if(i%4!=0)state|=1<<(i-1);    if((i+1)%4!=0)state|=1<<(i+1);    if(i-4>=0)state|=1<<(i-4);    if(i+4<16)state|=1<<(i+4);    return (state^stat);}//BFS寬度遍曆,搜尋每種狀態,不斷放入隊列中,找到後立即返回真(true)bool bfs(){    while(front<rear)    {        int state=states[front++];//取出隊列頭,記錄此時狀態        if(0==state||65535==state)//一旦找到,立即返回        {            cout<<step[state]<<"\n";            return true;        }        int i;        for(i=0;i<16;i++)//遍曆每種狀態        {            int st;            st=fanzhuan(state,i);            if(!vis[st])//防止重複遍曆            {                states[rear++]=st;//不可以的加到隊列尾部                vis[st]=true;                step[st]=step[states[front-1]]+1;//步數加1            }        }    }    return false;}int main(){    //freopen("in.txt","r",stdin);    //freopen("out.txt","w",stdout);    shuru();    if(!bfs())cout<<"Impossible\n";    return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.