POJ1753FlipGame(穷举)

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

算法

这类固定大小的棋盘上翻转棋子的问题都可以使用暴搜,一道类似的更简单直接的题目是POJ的1222,附一下我的那道题解,可以先看一下1222,1753就很明了了。传送门
这道题较之1222没有什么大新意,只是判断操作是否满足要求时有两种,并且要计算最少操作数。

AC Code

#include<stdio.h>

void GetPlate(int Origin[6][6]);
void FirstThree(int Origin[6][6], int Present[6][6], int FirstOpe[6], int Ope[5][6], int side);
//determine all the operations according to the operation of the first row
int Test(int Present[6][6]);
//is our operation OK?
int ComputeOpe(int Ope[5][6], int FirstOpe[6]);
//How many operations are there?
void Operate(int Present[6][6], int x, int y, int Go);
//Operate

int main()
{
    int Origin[6][6], Present[6][6], Ope[5][6], FirstOpe[6];
    int min = 17;

    GetPlate(Origin);
    for (FirstOpe[1] = 0; FirstOpe[1] <= 1; FirstOpe[1]++)
    for (FirstOpe[2] = 0; FirstOpe[2] <= 1; FirstOpe[2]++)
    for (FirstOpe[3] = 0; FirstOpe[3] <= 1; FirstOpe[3]++)
    for (FirstOpe[4] = 0; FirstOpe[4] <= 1; FirstOpe[4]++) {
        for (int i = 0; i < 6; i++) {
            for (int j = 0; j < 6; j++) {
                Present[i][j] = Origin[i][j];
            }
        }

        FirstThree(Origin, Present, FirstOpe, Ope, 1);
        if (Test(Present)) {
            int Total = ComputeOpe(Ope, FirstOpe);
            if (Total < min) {
                min = Total;
            }
        }

        for (int i = 0; i < 6; i++) {
            for (int j = 0; j < 6; j++) {
                Present[i][j] = Origin[i][j];
            }
        }

        FirstThree(Origin, Present, FirstOpe, Ope, 0);
        if (Test(Present)) {
            int Total = ComputeOpe(Ope, FirstOpe);
            if (Total < min) {
                min = Total;
            }
        }
    }

    if (min < 17) {
        printf("%d\n", min);
    }
    else {
        printf("Impossible\n");
    }

    return 0;
}

void GetPlate(int Origin[6][6])
{
    char c;

    for (int i = 1; i <= 4; i++) {
        for (int j = 1; j <= 4; j++) {
            scanf("%c", &c);
            if (c == 'b') {
                Origin[i][j] = 1;
            }
            else {
                Origin[i][j] = 0;
            }
        }
        getchar();
    }
}

void FirstThree(int Origin[6][6], int Present[6][6], int FirstOpe[6], int Ope[5][6], int side)
{
    for (int i = 1; i <= 4; i++) {
        Operate(Present, 1, i, FirstOpe[i]);
    }

    for (int i = 2; i <= 4; i++) {
        for (int j = 1; j <= 4; j++) {
            if (Present[i - 1][j] == side) {
                Ope[i][j] = 0;
            }
            else {
                Ope[i][j] = 1;
            }
            Operate(Present, i, j, Ope[i][j]);
        }
    }
}

void Operate(int Present[6][6], int x, int y, int Go)
{
    if (Go) {
        Present[x][y] = !Present[x][y];
        Present[x - 1][y] = !Present[x - 1][y];
        Present[x][y - 1] = !Present[x][y - 1];
        Present[x + 1][y] = !Present[x + 1][y];
        Present[x][y + 1] = !Present[x][y + 1];
    }
}

int Test(int Present[6][6])
{
    for (int i = 2; i <= 4; i++) {
        if (Present[4][i] != Present[4][i - 1]) {
            return 0;
        }
    }

    return 1;
}

int ComputeOpe(int Ope[5][6], int FirstOpe[6])
{
    int Total = 0;
    for (int i = 1; i <= 4; i++) {
        Total += FirstOpe[i];
    }

    for (int i = 2; i <= 4; i++) {
        for (int j = 1; j <= 4; j++) {
            Total += Ope[i][j];
        }
    }

    return Total;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值