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: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
Source
我不得不說位運算是一個強大的東西,之前從來沒怎麼用過,沒想到這麼一道本來沒有頭緒的題,位運算一優化成了一道裸一維的BFS,說這個題目,大意是一個翻牌游戲,牌有黑白兩面,當翻到所有的牌都是黑或都是白時,滿足狀態,輸出最小操作步數。每個位置的牌的都可以翻,但如過翻了當前這張牌,它的相鄰的位置的牌都要翻過來。對於所有的狀態,一共有2^16種(4*4的圖)#include#include #include using namespace std; bool vis[70000]; char ma[20]; typedef struct node { int state,step; }; void bfs(int s) { queue Q; node t;t.state=s;t.step=0; vis[s]=1; Q.push(t); while(!Q.empty()) { node v=Q.front();Q.pop(); if(v.state==0||v.state==65535) { cout< =0) tem^=1<<(i-4);//上 if(i<12) tem^=1<<(i+4);//下 if(i%4!=0) tem^=1<<(i-1);//左 if((i+1)%4!=0) tem^=1<<(i+1);//右 if(!vis[tem]) { vis[tem]=1; t.state=tem; t.step=v.step+1; Q.push(t); } } } puts("Impossible"); } int main() { int i=0,s=0; memset(vis,0,sizeof(vis)); while(i<16) { cin>>ma[i]; if(ma[i]=='b') s+=1<