POJ2965 Refrigerator(穷举+位操作)

Description

The game “The Pilots Brothers: following the stripy elephant” has a quest where a player needs to open a refrigerator.

There are 16 handles on the refrigerator door. Every handle can be in one of two states: open or closed. The refrigerator is open only when all handles are open. The handles are represented as a matrix 4х4. You can change the state of a handle in any location [i, j] (1 ≤ i, j ≤ 4). However, this also changes states of all handles in row i and all handles in column j.

The task is to determine the minimum number of handle switching necessary to open the refrigerator.

Input

The input contains four lines. Each of the four lines contains four characters describing the initial state of appropriate handles. A symbol “+” means that the handle is in closed state, whereas the symbol “−” means “open”. At least one of the handles is initially closed.

Output

The first line of the input contains N – the minimum number of switching. The rest N lines describe switching sequence. Each of the lines contains a row number and a column number of the matrix separated by one or more spaces. If there are several solutions, you may give any one of them.

Sample Input

-+–
—-
—-
-+–

Sample Output

6
1 1
1 3
1 4
4 1
4 3
4 4

算法

算法就是穷举,但是稍有玄学的是,题中表明需要找到最小操作数,但是我遍历全部可能操作会超时,但是在找到第一个可行操作就直接输出返回反而AC了,令人惊奇

Trick

还是要使用一整形变量的二进制位表示操作数,多重循环实在是很麻烦,也很不雅观,使用二进制位的话会简洁很多。

AC Code

#include<stdio.h>

#define ROW 4
#define COL 4

void Operate(int Present[ROW + 3][COL + 3], int x, int y);
//Operate on Array 'Present' row 'x', column 'y'
void GetOrigin(int Origin[ROW + 3][COL + 3]);
//Get the origin state
int GetBit(int k, int i);
//Get the ith bit in k to determine whether to perform the corresponding operation
int Test(int Present[ROW + 3][COL + 3]);
//Is the set of operation OK?
int Compute(int k);
//How many operations are there in k?
void Display(int k);
//Display all the necessary operations

int main()
{
    int Present[ROW + 3][COL + 3], Origin[ROW + 3][COL + 3];
    int Min = 20,   //Initialize the 'Min' to 20, which is larger than any of the possible number of total operations
        MinOpe;     //

    GetOrigin(Origin);

    for (int k = 0; k < 65536; k++) {
        for (int i = 1; i <= 4; i++) {
            for (int j = 1; j <= 4; j++) {
                Present[i][j] = Origin[i][j];
            }
        }

        for (int i = 1; i <= 4; i++) {
            for (int j = 1; j <= 4; j++) {
                if (GetBit(k, (i - 1) * 4 + j - 1)) {
                    Operate(Present, i, j);
                }
            }
        }

        if (Test(Present)) {
            int Total = Compute(k);
            if (Total < Min) {
                Min = Total;
                MinOpe = k;
                printf("%d\n", Min);
                Display(MinOpe);
                return 0;
            }
        }
    }


    return 0;
}

void Operate(int Present[ROW + 3][COL + 3], int x, int y)
//Operate on Array 'Present' row 'x', column 'y'
{
    for (int i = 1; i <= 4; i++) {
        Present[x][i] = !Present[x][i];
    }
    for (int i = 1; i <= 4; i++) {
        Present[i][y] = !Present[i][y];
    }
    Present[x][y] = !Present[x][y];
}

void GetOrigin(int Origin[ROW + 3][COL + 3])
//Get the origin state
{
    char c;

    for (int i = 1; i <= 4; i++) {
        for (int j = 1; j <= 4; j++) {
            c = getchar();
            if (c == '-') {
                Origin[i][j] = 0;
            }
            else {
                Origin[i][j] = 1;
            }
        }
        getchar();
    }
}

int GetBit(int k, int i)
//Get the ith bit in k to determine whether to perform the corresponding operation
{
    return ((k >> i) & 1);
}

int Test(int Present[ROW + 3][COL + 3])
//Is the set of operation OK?
{
    for (int i = 1; i <= 4; i++) {
        for (int j = 1; j <= 4; j++) {
            if (Present[i][j] != 0) {
                return 0;
            }
        }
    }

    return 1;
}

int Compute(int k)
//How many operations are there in k?
{
    int Total = 0;

    for (int i = 0; i < 16; i++) {
        Total += GetBit(k, i);
    }
    return Total;
}

void Display(int k)
//Display all the necessary operations
{
    for (int i = 0; i < 16; i++) {
        if (GetBit(k, i)) {
            printf("%d %d\n", i / 4 + 1, i % 4 + 1);
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值