码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 1753 Flip Game

时间:2019-10-01 09:45:56      阅读:111      评论:0      收藏:0      [点我收藏+]

标签:hit   存储   ini   ios   c99   ever   you   结束   follow   

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).
题意:在4 × 4 的方格中放着棋子,棋子有黑白两色, b 代表白色朝上, w 代表黑色朝上, 问最少翻多少次可以使得棋盘全为黑色或白色,如果做不到则输出“Impossible”。
枚举法:发现一个规律:
            1.反转i次后,如果不能颜色一样,那么就再翻转一次,即i+1次
            2.反转偶数次和没有进行反转是相同。
#include <stdio.h>
#include <stdlib.h>
#include <cstring>
#include <iostream>
using namespace std;
class Chess
{
private:
    int result[17];     //存储点
    char map[4][4];    //方格 4 * 4
    char temp[4][4];   //存储转换后的方格状态
public:
    void init() //初始化输入并检查是否已经纯色
    {
        for (int i = 0; i < 4; i++) scanf("%s", map[i]);  //输入方格的初始状态
        if(check(map))    //检查
        {
            printf("0\n");
            exit(0);
        }
    }
    void reverse(char temp[][4], int x, int y)//单次反转动作
    {
        if (temp[x][y] == ‘b‘) temp[x][y] = ‘w‘;  //白色变黑色
        else temp[x][y] = ‘b‘;     //黑色变白色
    }
    void apply(int n)      //应用组合数进行反转
    {
        for (int i = n - 1; i >= 0; i--)
        {
            int x = result[i] / 4;    //row
            int y = result[i] % 4;    //column
            reverse(temp, x, y);
            if(x > 0) reverse(temp, x - 1, y);
            if(x < 3) reverse(temp, x + 1, y);
            if(y > 0) reverse(temp, x, y - 1);
            if(y < 3) reverse(temp, x, y + 1);
        }
    }
    int check(char temp[][4])    //检查是否反转成功
    {
        int flag = 0;
        for(int i = 0; i < 4; i++)
        {
            for (int j = 0; j < 4; j++)
            {
                if (temp[i][j] == ‘b‘) flag++;  //全为白色的状态
                else flag--;     //全为黑色的状态
            }
        }
        if(flag == 16 || flag == -16) return 1;    //翻转成功
        else return 0;    //翻转失败
    }
    void meiju(int start, int left, int num)  //枚举从16个数中取num个的所有组合
    {
        for(int i = start; i <= 16 - left; i++)
        {
            result[left - 1] = i;
            if(left == 1)
            {
                for(int j = 0; j < 4; j++)
                    for(int k = 0; k < 4; k++) temp[j][k] = map[j][k];
apply(num); if(check(temp)) //判断翻转是否成功 { printf("%d\n", num); exit(0); //一旦找到必定是最优解,可以直接结束程序 } } else meiju(i + 1, left - 1, num); } } }; int main() { Chess a; //创建对象 a.init(); //初始化 for (int i = 1; i <= 16; i++) a.meiju(0, i, i); printf("Impossible\n"); return 0; }

  

 

POJ 1753 Flip Game

标签:hit   存储   ini   ios   c99   ever   you   结束   follow   

原文地址:https://www.cnblogs.com/Edviv/p/11614739.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!