zoukankan      html  css  js  c++  java
  • Flip Game 分类: POJ 2015-06-15 14:59 22人阅读 评论(0) 收藏

    Flip Game
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 33519   Accepted: 14642

    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 <cstdio>
    #include <cmath>
    #include <cstring>
    #include <cstdlib>
    #include <cctype>
    #include <queue>
    #include <stack>
    #include <algorithm>
    using namespace std;
    int a[5][5],b[5][5];
    int Arr[5];
    char s[5][5];
    
    void Trans()//转化成数字
    {
        for(int i=0; i<4; i++)
        {
            for(int j=0; j<4; j++)
            {
                a[i][j]=s[i][j]=='b'?1:0;
            }
        }
    }
    void tran(int n)//二进制转化
    {
        memset(Arr,0,sizeof(Arr));
        int top=3;
        while(n)
        {
            Arr[top--]=n&1;
            n=n>>1;
        }
    }
    void Creat()
    {
        for(int i=0; i<4; i++)
        {
            for(int j=0; j<4; j++)
            {
                b[i][j]=a[i][j];
            }
        }
    }
    void change(int i,int j)//反转
    {
        b[i][j]=b[i][j]==0?1:0;
        if(j>0)
        {
            b[i][j-1]=b[i][j-1]==0?1:0;
        }
        if(j<3)
        {
            b[i][j+1]=b[i][j+1]==0?1:0;
        }
        if(i<3)
        {
            b[i+1][j]=b[i+1][j]==0?1:0;
        }
        if(i>0)
        {
            b[i-1][j]=b[i-1][j]==0?1:0;
        }
    }
    int main()
    {
        int sum;
        int Max=1000;
        for(int i=0; i<4; i++)
        {
            scanf("%s",s[i]);
        }
        Trans();
        for(int i=0; i<=15; i++)
        {
            tran(i);
            for(int j=0; j<2; j++)
            {
                Creat();
                sum=0;
                for(int k=0; k<4; k++)
                {
                    if(Arr[k])
                    {
                        change(0,k);
                        sum++;
                    }
                }
                for(int k=1; k<4; k++)
                {
                    for(int kk=0; kk<4; kk++)
                    {
                        if(b[k-1][kk]!=j)
                        {
                            change(k,kk);
                            sum++;
                        }
                    }
                }
                bool flag=true;
                for(int k=0; k<4; k++)
                {
                    if(b[3][k]!=j)
                    {
                        flag=false;
                        break;
                    }
                }
                if(flag)
                {
                    if(Max>sum)
                    {
                        Max=sum;
                    }
                    if(Max==0)
                    {
                        break;
                    }
                }
                else//如果不能反转成则就不可能反转成功
                {
                    break;
                }
            }
        }
        if(Max!=1000)
        {
            printf("%d
    ",Max);
        }
        else
        {
            printf("Impossible
    ");
        }
        return 0;
    }
    

    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    2012年互联网教育行业观察
    SharePoint 2013的简单介绍
    让Node.js在Azure上运行3
    让Node.js在Azure上运行2
    有一个字符串 "I am a good man",设计一个函数,返回 "man good a am I"。
    json序列化与反序列化
    golang连接mysql数据库进行查询
    简单的WCF服务
    百钱买百鸡问题
    大叔程序员的第九天 @广播启动Activity
  • 原文地址:https://www.cnblogs.com/juechen/p/4722030.html
Copyright © 2011-2022 走看看