难题 枚举,位运算加遍历或深搜 或者纯深搜

Flip Game

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 51139   Accepted: 21590

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

#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<vector>
#include<map>
#include<string.h>

//#include <ionaip>
using namespace std;
typedef long long ll;
bool mp[4][4];
bool yuan[4][4];
ll check;
int ans=1000;
void f(int pos)   //将所在位置及相邻位置的棋子翻转
{
    int hang,lie;
    hang=pos/4;
    lie=pos%4;
    mp[hang][lie]=!mp[hang][lie];
    if(lie-1>=0)
        mp[hang][lie-1]=!mp[hang][lie-1];
    if(lie+1<4)
        mp[hang][lie+1]=!mp[hang][lie+1];
    if(hang+1<4)
        mp[hang+1][lie]=!mp[hang+1][lie];
    if(hang-1>=0)
        mp[hang-1][lie]=!mp[hang-1][lie];
}

bool q()    //检查是否符合要求结果
{
    bool flag=mp[0][0];
    for(int i=0; i<4; ++i)
    {
        for(int j=0; j<4; ++j)
        {
            if(mp[i][j]!=flag)
                return false;
        }
    }
    return true;
}
int main()
{
    char temp;
    memset(mp,false,sizeof(mp));
    memset(yuan,false,sizeof(yuan));
    for(int i=0; i<4; ++i)
    {
        for(int j=0; j<4; ++j)
        {
            temp=getchar();
            if(temp=='b')
                yuan[i][j]= mp[i][j]=true;

        }
        getchar();

    }



        for(check=0; check<= 1<<16; ++check)  //枚举,将所有有可能反转的情况都列举一遍,翻转多个的也有,选择翻转的棋子我用1表示,比如要选择第1个、第3个、第5个和第6个,那就是11 0101的状态。这样枚举就很方便了,枚举值范围0x0000~0xFFFF。
        {
            int pos=0;
            ll now=check;
            int count=0;

            while(now>0)
            {
                if(now&1)    //假如这一位应该翻转
                {
                    //反转操作///     
                    f(pos);   //把那一位及相邻的位翻转
                    count++;

                }
                now>>=1;   //找到下一个该翻转的位
                pos++;
            }
            //检查是否全部纯色/
            if(q())
            {
                ans=min(ans,count);
                for(int i=0; i<4; ++i)
                {
                    for(int j=0; j<4; ++j)
                    {

                        mp[i][j]=yuan[i][j];//还原8

                    }
                }

            }
            else
                for(int i=0; i<4; ++i)
                {
                    for(int j=0; j<4; ++j)
                    {

                        mp[i][j]=yuan[i][j];//还原8

                    }
                }
        }

    if(ans!=1000)    //假如没有全都是纯色的情况
        cout<<ans<<endl;
    else
        cout<<"Impossible"<<endl;
}

用位运算加深搜递归

#include <stdio.h>

int field;
int state[]={19,39,78,140,305,626,1252,2248,4880,10016,20032,35968,12544,29184,58368,51200};
   //打表,将位运算该算的数提前算出来,翻哪个地方,哪个地方的位变成一。
void read() {
    for(int i=0; i<4; i++) {
        for(int j=0; j<4; j++) {
            field<<=1;      //把前面的移走,field不断左移使字符能够输入
            if(getchar()=='b')
                field|=1;  //或运算可以无论这位是什么,都能让这位是1.
        }
        getchar();
    }
}

void flip(int i) {
    field^=state[i];   //该翻转的位置做异或运算,0变成1,1变成0
}

bool check() {
    return field==0x0000||field==0xFFFF;
}

bool find(int n, int i) {
    if(n==0) return check();
    //if(i>=16) return false;
    for(; i<16; i++) {
        flip(i);
        if(find(n-1,i+1))  //翻几次,find几遍,一次find翻一个,所有的这些find都要保证为true才行,i+1可以使翻得不会有重复的
            return true;
        flip(i);   再翻一次可以达到原来的情况。
    }
    return false;
}

void work() {
    for(int c=0; c<=16; c++)
        if(find(c,0)) {   //枚举,一共能翻1-16个,找所有的翻几个的情况,找到最小的直接输出
            printf("%d\n", c);
            return;
        }
    puts("Impossible");
}

int main() {
    read();
    work();
    return 0;
}

另一种和第一种方法差不多的

#include <stdio.h>

int field;
int state[]={19,39,78,140,305,626,1252,2248,4880,10016,20032,35968,12544,29184,58368,51200};
int bit[]={1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768}; 

void read() {
    for(int i=0; i<4; i++) {
        for(int j=0; j<4; j++) {
            field<<=1;
            if(getchar()=='b')
                field|=1;  //第一种方法用数组存的这里使用一个整数
        }
        getchar();
    }
}

bool check() {
    return field==0x0000||field==0xFFFF;
}

int minn=0xFF;
void work() {
    for(int flip=0; flip<=0xFFFF; flip++) {
        int temp=field, cnt=0;
        for(int i=0; i<16; i++)
            if(flip&bit[i]) {// flip&(1<<i)
                field^=state[i];       //第一种方法没用bit[],直接用的1,并且是让flip向后移
                ++cnt;
            }
        if(check()&&minn>cnt) minn=cnt;
        field=temp;   //初始化,再让他成为一开始的棋盘情况
    }
}

void print() {
    if(minn==0xFF) puts("Impossible");
    else printf("%d\n", minn);
}

int main() {
    read();
    work();
    print();
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_42165786/article/details/81084439