设为首页 加入收藏

TOP

POj 1753--Flip Game--位运算+BFS
2015-07-20 17:55:14 】 浏览:156
Tags:POj 1753--Flip Game-- 运算 BFS
Flip Game
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 30669 Accepted: 13345

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:
Choose any one of the 16 pieces.
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

Source

我不得不说位运算是一个强大的东西,之前从来没怎么用过,没想到这么一道本来没有头绪的题,位运算一优化成了一道裸一维的BFS,说这个题目,大意是一个翻牌游戏,牌有黑白两面,当翻到所有的牌都是黑或都是白时,满足状态,输出最小操作步数。每个位置的牌的都可以翻,但如过翻了当前这张牌,它的相邻的位置的牌都要翻过来。对于所有的状态,一共有2^16种(4*4的图)
而对于翻牌操作,假设当前状态为s 想要翻第i个位置的牌,只要s^=1<
#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<
            
             




】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇HDU 1158 Employment Planning (.. 下一篇uva 11330 - Andy's Shoes(置..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目