• POJ:1753-Flip Game(二进制+bfs)


    题目链接:http://poj.org/problem?id=1753

    Flip Game

    Time Limit: 1000MS Memory Limit: 65536K

    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:
    Choose any one of the 16 pieces.
    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


    • 题意就是给你一个4*4的图,要求你通过一定的翻转将图全改为b或者w。每次翻转一个点这个点的上下左右也会跟着翻转。问最少需要翻转多少次。

    • 其实是一个很有意思的搜索题,因为每个点只有两种情况,所以可以把一个图看成一个二进制的数字,就可以使用位运算来模拟翻转, 然后bfs,看到达目标数字需要的最少步骤。


    #include<stdio.h>
    #include<queue>
    #include<cstring>
    using namespace std;
    const int maxn = 1e5;
    bool vis[maxn];
    struct node
    {
        int va,step;
    } now,Next;
    char s[10][10];
    
    void init()
    {
        memset(vis,0,sizeof(vis));
        for(int i=1; i<4; i++)
            scanf("%s",s[i]);
        now.step = 0;
        now.va = 0;
        int t = 1;
        for(int i=0; i<4; i++)
        {
            for(int j=0; j<4; j++)
            {
                int k;
                if(s[i][j] == 'b')
                    k = 1;
                else
                    k = 0;
                now.va += k*t;
                t *= 2;
            }
        }
    }
    
    void deal(int pos)
    {
        int num[20],x = now.va;
        Next.step = now.step + 1;
        //先转化成二进制
        for(int i=0; i<=15; i++)
        {
            num[i] = x % 2;
            x /= 2;
        }
        num[pos] ^= 1;
        if(pos > 4)
            num[pos-4] ^= 1;
        if(pos%4 != 3)
            num[pos+1] ^= 1;
        if(pos%4 != 0)
            num[pos-1] ^= 1;
        if(pos < 12)
            num[pos+4] ^= 1;
        int t= 1,ans = 0;
        //操作之后转化为10进制,10进制更好标记状态
        for(int i=0;i<16;i++)
        {
            ans += t*num[i];
            t*= 2;
        }
        Next.va = ans;
    }
    
    int bfs()
    {
        if(now.va == 0 || now.va == 65535)
            return now.step;
        queue<node> qu;
        qu.push(now);
        vis[now.va] = true;
        while(!qu.empty())
        {
            now = qu.front();
            qu.pop();
            for(int i=0; i<16; i++)
            {
                deal(i);
                if(!vis[Next.va])
                {
                    if(Next.va == 0 || Next.va == 65535)//最终的情况代表的数字就是0或者65535
                        return Next.step;
                    vis[Next.va] = true;
                    qu.push(Next);
                }
            }
        }
        return -1;
    }
    
    int main()
    {
        while(scanf("%s",s[0])!=EOF)
        {
            init();
            int ans = bfs();
            if(ans != -1)
                printf("%d
    ",ans);
            else
                printf("Impossible
    ");
        }
        return 0;
    }
  • 相关阅读:
    你认为做好测试计划工作的关键是什么?
    一套完整的测试应该由哪些阶段组成?
    你对测试最大的兴趣在哪里?为什么?
    如何测试一个纸杯?
    黑盒测试和白盒测试各自的优缺点
    在您以往的工作中,一条软件缺陷(或者叫Bug)记录都包含了哪些内容?如何提交高质量的软件缺陷(Bug)记录?
    测试人员在软件开发过程中的任务
    软件测试分为几个阶段? 各阶段的测试策略和要求是什么?
    软件测试的策略
    软件产品质量特性
  • 原文地址:https://www.cnblogs.com/GoldenFingers/p/9107242.html
Copyright © 2020-2023  润新知