The Pilots Brothers' refrigerator

来源:互联网 发布:p2p网络借贷逾期率 编辑:程序博客网 时间:2024/05/18 01:56

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
61 11 31 44 14 3

4 4

思路;

如何把一个‘+’在其他handles不变的情况下变成‘-’,方法是;把‘+’所在的行,列,的字符全部都

变化一次,结果是‘+’所在位置被变动了7次,+’所在的行,列,的字符均变动了4次,其他的都是两次,

因为偶数次变动对最终结果无影响,所以相当于只把‘+’变成了‘-’,并没有改变其他的。

然后还是因为这个道理,有一个‘+’,他所在的行和列均会变动一次,最终偶数次无影响,奇数次的位置就

是我们需要改变的

代码:

#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;int a[5][5];int main(){    char c;    memset(a, 0, sizeof(a));    for(int i=1; i<=4; i++)        for(int j=1; j<=4; j++)        {            c = getchar();            while(c == '\n') c = getchar();            if(c == '+')            {                for(int k=1; k<=4; k++)                {                    a[i][k]++;                    a[k][j]++;                }                a[i][j]--;            }        }    int sum = 0;    for(int i=1; i<=4; i++)        for(int j=1; j<=4; j++)            sum += a[i][j]%2;    printf("%d\n", sum);    for(int i=1; i<=4; i++)        for(int j=1; j<=4; j++)            if(a[i][j]%2)                printf("%d %d\n", i, j);    return 0;}


0 0
原创粉丝点击