zoukankan      html  css  js  c++  java
  • POJ 1753 Flip Game

    Flip Game
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 24474   Accepted: 10517

    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 <stdio.h>
    #include <queue>
    #include <string.h>
    struct node
    {
        int data, step;
    };
    using namespace std;
    queue<struct node>q;
    
    int flip(int s, int n)
    {
        s ^= 1<<n;
        if(n % 4 != 0)s ^= 1<<(n-1);
        if(n % 4 != 3)s ^= 1<<(n+1);
        if(n-4 >= 0)s ^= 1<<(n-4);
        if(n+4 < 16)s ^= 1<<(n+4);
        return s;
    }
    
    bool vis[65536];
    void bfs()
    {
        while(!q.empty())
        {
            struct node u = q.front();
            q.pop();
            if(u.data == 65535 || u.data == 0)
            {
                printf("%d
    ", u.step);
                return;
            }
            for(int i = 0; i < 16; i++)
            {
                int x = flip(u.data, i);
                if(!vis[x])
                {
                    q.push((struct node){x, u.step+1});
                    vis[x] = 1;
                }
            }
        }
        printf("Impossible
    ");
    }
    
    int main()
    {
        char s[5];
        int x = 0;
        for(int i = 0; i < 4; i++)
        {
            scanf("%s", s);
            for(int j = 0; j < 4; j++)
            {
                if(s[j] == 'b')
                    x = (x<<1) + 1;
                else x <<= 1;
            }
        }
        memset(vis, 0, sizeof(vis));
        while(!q.empty())q.pop();
        q.push((struct node){x, 0});
        vis[x] = 1;
        bfs();
        return 0;
    }
  • 相关阅读:
    空悬指针、野指针、内存泄漏、内存溢出
    自定义消息的操作方法ON_MESSAGE(..)
    为什么static成员变量一定要在类外初始化?
    Ubuntu 系统目录结构
    Beyond Compare 4 30天评估期结束的解决办法
    C++ string 字符串 结尾 标志
    C语言——枚举类型用法
    结构体struct-联合体union-枚举enum
    网卡bood
    kvm 安装
  • 原文地址:https://www.cnblogs.com/wolfred7464/p/3143749.html
Copyright © 2011-2022 走看看