Flip Game

Flip Game

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 22   Accepted Submission(s) : 10
Problem 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
 

Source
PKU
 
#include<iostream>
#include <map>
#include <set>
#include <list>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <vector>
#include <bitset>
#include <cstdio>
#include <string>
#include <numeric>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <functional>

using namespace std;
typedef long long ll;
#define Exp 1e-8
#define inf 0x7fffffff
#define Read freopen("in.txt","r",stdin)
#define Write freopen("out.txt","w",stdout)
#define maxn 65536
#define mod 1000000007


int que[maxn];
int f=0,l=0;                 //front last
bool VIs[maxn]= {0};         //记录状态是否已经出现过
int step[maxn]= {0};         //到达某一状态所用的步数
int id=0,flag=0,ans=0;

void read()
{
    char c;
    for(int i=0; i<4; i++)
        for(int j=0; j<4; j++)
        {
            cin>>c;
            id<<=1;
            if(c=='b')
                id+=1;
        }
        //cout<<"A"<<endl;
        //开始时,我想的使用字符数组来储存状态的,但是,觉得开销太大~~肯定不行~~
        //看了大牛们的表示方法~~~
}

void BFS()      //DFS应该也行,但是递归次数太多,可能会超时
{
    que[l]=id;
    VIs[id]=1;
    step[id]=0;
    l++;

    while(f<l)              //BFS思想,将能够到达的所有状态一一枚举,然后入队;对队首状态处理了16次,该状态就要出队
    {
        int j,k,temp;
        for(j=0; j<4; j++)            //对于每一个点都要尝试翻一次
            for(k=0; k<4; k++)
            {
                temp=id;             //但是,每翻完一次,就需要将状态还原
                if(j==0)             //在边上的格子,纵向相邻的只能翻一个,按照上下位置对应翻面即可
                    temp^=1<<(11-4*j-k);
                else if(j==3)
                    temp^=1<<(19-4*j-k);
                else
                {
                    temp^=1<<(11-4*j-k);  //如果是在中间位置,纵向上下都要翻
                    temp^=1<<(19-4*j-k);
                }

                if(k==0)                 //横向的
                    temp^=3<<(14-4*j-k);
                else if(k==3)
                    temp^=3<<(15-4*j-k);
                else
                {
                    temp^=7<<(14-4*j-k); //位运算的技巧,7的二进制是111,正好可以减少一次运算
                }

                if(!VIs[temp])          //队列中出现了以前没有出现的状态时,就需要对该状态进行标记
                {
                    que[l]=temp;
                    l++;
                    VIs[temp]=1;
                    step[temp]=step[id]+1;
                }
                if(temp==0||temp==65535)  //如果出现能够满足全白或者全黑的状态,直接跳出,输出yes
                {
                    flag=1;
                    ans=step[temp];
                    return ;
                }
            }
        f++;
        id=que[f];         //队首状态出列,指针后遗
    }
    flag=0;//队列为空时,意味着,所有能达到的状态都没办法成为全黑或者全白,此时,就要输出no
}


int main()
{
    read();
    if(id==0||id==65535)
        cout<<ans<<endl;
    else
    {
        BFS();
        if(flag)
            cout<<ans<<endl;
        else
            cout<<"Impossible"<<endl;
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值