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;
    }
  • 相关阅读:
    OpenCASCADE Chamfer 3D Basics
    OpenCASCADE Chamfer 2D
    .NetCore 连接 Oracle 数据库,直接C# 或者 ORM框架(EFCore、XPO)
    心内科疾病指南
    HttpClient 调用 RestAPI 接口的用法
    在 Blazor 应用中使用 DevExtreme widgets
    2021 最近一次检查甘油三脂,验证苯扎贝特的效果。
    紫鹊界本味湘菜,
    如何Rest接口获取网上的股票数据,有哪些资源?-- 推荐Tushare金融数据
    优秀常用的「资源搜索网站」,收藏
  • 原文地址:https://www.cnblogs.com/wolfred7464/p/3143749.html
Copyright © 2011-2022 走看看