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

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

    Flip Game
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 36419   Accepted: 15866

    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
    

    Source

     
    题意:输入4*4的字符,包含b,w,每次选择一个位置,改变这个位置上下左右包括自身的值,问最少需要改变多少次。
     
    分析: 暴力枚举一遍。
     
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cstdlib>
    #include <algorithm>
    
    using namespace std;
    
    int a[6][6];
    char str[10][10];
    int b[5][2]= {1,0,0,1,-1,0,0,-1,0,0};
    int Min;
    
    int pai(int ans)
    {
        int t=a[1][1];
        int flag = 0;
        for(int i=1; i<=4; i++)
        {
            for(int j=1; j<=4; j++)
            {
                if(a[i][j]!=t)
                {
                    flag = 1;
                    i = 5;
                    break;
                }
            }
        }
        if(flag == 0)
        {
            if(ans < Min)
                Min = ans;
            return 1;
        }
        return 0;
    }
    
    void DFS(int x, int y, int ans)
    {
        if(y>4)
        {
            x+=1;
            y-=4;
        }
        if(x>4||y>4)
        {
            int f = pai(ans);
            return ;
        }
        DFS(x, y+1, ans);
        for(int k=0; k<5; k++)
        {
            a[x+b[k][0]][y+b[k][1]]^=1;
        }
        DFS(x, y+1, ans+1);
        for(int k=0; k<5; k++)
        {
            a[x+b[k][0]][y+b[k][1]]^=1;
        }
    }
    
    int main()
    {
        char str[10][10];
        memset(a,-1,sizeof(a));
        for(int i=1; i<=4; i++)
        {
            scanf("%s", str[i]+1);
        }
        for(int i=1; i<=4; i++)
        {
            for(int j=1; j<=4; j++)
            {
                if(str[i][j] == 'b')
                    a[i][j] = 1;
                else
                    a[i][j] = 0;
            }
        }
        Min = 20;
        if(pai(0))
            Min = 0;
        else
            DFS(1,1,0);
        if(Min==20)
            printf("Impossible
    ");
        else
            printf("%d
    ",Min);
    
        return 0;
    }
  • 相关阅读:
    hdu 4970 树状数组 “改段求段”
    hdu 2242 无向图/求用桥一分为二后使俩个bcc点权值和之差最小并输出 /缩点+2次新图dfs
    hdu3715 2-sat+二分
    hdu 3639 有向图缩点+建反向图+搜索
    hdu 3072 有向图缩点成最小树形图计算最小权
    hdu 3061 hdu 3996 最大权闭合图 最后一斩
    hdu 3879 hdu 3917 构造最大权闭合图 俩经典题
    hdu 4738 无向图缩点断桥 // 细节坑题
    hdu3452 无向树去掉最小的边集使任何叶子与根不连通 / 最小割
    hdu 3657 最小割的活用 / 奇偶方格取数类经典题 /最小割
  • 原文地址:https://www.cnblogs.com/mengzhong/p/5036390.html
Copyright © 2011-2022 走看看