zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    HDU--1212大数取模
    欧拉函数
    自制体重转换器
    常用快捷键
    Markdown基本语法
    python全栈开发day50-jquery之ajax、XmlHttpRquest
    python全栈开发day49-jquery的位置信息、事件流、事件对象,事件委托,事件绑定和解绑
    阳历转阴历算法
    python全栈开发day48-jqurey自定义动画,jQuery属性操作,jQuery的文档操作,jQuery中的ajax
    python全栈开发day47-jqurey
  • 原文地址:https://www.cnblogs.com/lzmfywz/p/3236029.html
Copyright © 2011-2022 走看看