POJ 2965 The Pilots Brothers' refrigerator

The Pilots Brothers' refrigerator

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

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

# 题意:
> 给你4*4的矩阵。每个点有两种状态,+代表关,-代表开。每个点有一个操作就是该点所在行列所有状态翻转。问最少多少次可以全部打开,并且输出最少个数情况下翻转的点。

# 题解:
> 看到一个特别简单的方法。对于一个关闭的点,想要把他打开,首先翻转一下他自己,为了不影响其他的点他所在行列都得翻转一次。这样最后是奇数次的即为翻转点。

# 代码:
```c++
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
    char input[10][10];
    int rec[10][10];
    memset(rec,0,sizeof(rec));
    for (int i = 1;i <= 4;i++){
        scanf("%s",input[i]+1);
        for (int j = 1;j <= 4;j++){
            if (input[i][j] == '+'){
                for (int k = 1;k <= 4;k++){
                    rec[i][k]++;
                    rec[k][j]++;
                }
                rec[i][j]--;
            }
        }
    }
    int ans = 0;
    for (int i = 1;i <= 4;i++){
        for (int j = 1;j <= 4;j++){
            ans+=(rec[i][j]%2);
        }
    }
    cout<<ans<<endl;
     for (int i = 1;i <= 4;i++){
        for (int j = 1;j <= 4;j++){
            if (rec[i][j]%2)
                cout<<i<<" "<<j<<endl;
        }
    }
}

posted @ 2016-04-03 19:03  Thecoollight  阅读(313)  评论(0编辑  收藏  举报