• POJ 1753 Flip Game


    Flip Game
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 25328   Accepted: 10930

    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<stdio.h>
    using namespace std;
    
    typedef struct
    {
        int sum;
        int step;
    }Qu;
    Qu Queue[70000];
    int Step;
    int Sum;
    int n=0;
    int front,rear;
    int sum[6][6]={0,0,0,0,0,0,0,1,2,4,8,0,0,16,32,64,128,0,0,256,512,1024,2048,0,0,4096,8192,16384,32768,0,0,0,0,0,0,0};
    int visited[65536]={0};
    
    int BFS()
    {
        int i,j,k;
        rear=front=-1;
        rear++;
        Queue[rear].step=0;
        Queue[rear].sum=n;
        if(n==65535 ||!n)
        {
            Step=Queue[rear].step;
            return 1;
        }
        visited[n]=1;
        while(rear>front)
        {
            front++;
            if(Queue[front].sum==65535 ||!Queue[front].sum)
            {
                Step=Queue[front].step;
                return 1;
            }
            for(i=1;i<=4;i++)
            {
                for(j=1;j<=4;j++)
                {
                    Sum=Queue[front].sum;
                    Sum=Sum^sum[i-1][j];
                    Sum=Sum^sum[i+1][j];
                    Sum=Sum^sum[i][j-1];
                    Sum=Sum^sum[i][j+1];
                    Sum=Sum^sum[i][j];
                    if(!visited[Sum])
                    {
                        rear++;
                        Queue[rear].step=Queue[front].step+1;
                        Queue[rear].sum=Sum;
                        visited[Sum]=1;
                    }
                }
            }
        }
        return 0;
    }
    
    int main()
    {
        
        int i,j;
        char ch;
        for(i=1;i<=4;i++)
        {
            for(j=1;j<=4;j++)
            {
                n=n<<1;
                cin>>ch;
                if(ch=='b')
                {
                    n=n^1;
                }
            }
        }
        if(BFS())
        {
            printf("%d
    ",Step);
        }
        else
        {
            printf("Impossible
    ");
        }
        return 0;
    }
  • 相关阅读:
    使用Cloud application Studio在C4C UI里创建下拉列表(dropdown list)
    如何使用Kubernetes里的NetworkPolicy
    SpringBoot应用和PostgreSQL数据库部署到Kubernetes上的一个例子
    Kubernetes API server工作原理
    Kubernetes Helm入门指南
    两张图弄懂函数的递归(以golang为例)
    (十四)golang--函数和包
    【自然语言处理(三)】主题模型
    【自然语言处理】使用朴素贝叶斯进行语种检测
    【自然语言处理】利用朴素贝叶斯进行新闻分类(自己处理数据)
  • 原文地址:https://www.cnblogs.com/lzmfywz/p/3236029.html
Copyright © 2020-2023  润新知