E - The Pilots Brothers' refrigerator

题目:

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

题意:

给你一张图,然你经过多次反转后整张图变成全是“-”,要求你求出最小的翻转次数,并且输出所有的翻转位置;

思路:

看到这道题我的第一反应是和Flip Game这道题一样应该用位运算,但是我的位运算学的不好,所以没有做,但是我看了网上大神的思路,果然,大神就是大神!

如果要把某个+翻成-,把它所在的行和列的所有格都翻一遍就可以了   。

那它本身翻7次,它所在行或列的单位被翻4次,其他位置的被翻2次。翻偶数次和没翻一样,翻7次和翻1次一样。那么定义一个数组,对所有的+做统计,令其所在的行和列的元素都为1,最后再看一下,元素为1的位置就是被翻转过的位置,而元素为1的元素个数就是n的值。

代码如下:

#include<stdio.h>
#include<string.h>

int i,j;
int a[4][4];

int main()
{
    memset(a,0,sizeof(a));
    char c;
    for(i=0; i<4; i++)
    {
        for(j=0; j<4; j++)
        {
            scanf("%c",&c);
            if(c=='+')
            {
                a[i][j]=!a[i][j];//转换成0和1代表整个图;0代表-,1代表+;
                //printf("%d\n",a[i][j]);
                for(int k=0; k<4; k++)//同行同列的翻转;
                {
                    a[i][k]=!a[i][k];
                    a[k][j]=!a[k][j];
                }
            }
        }
        scanf("%*c");
    }
    int s=0;
    for(i=0; i<4; i++)
    {
        for(j=0; j<4; j++)
        {
            if(a[i][j]==1)
                s++;
        }
    }
    printf("%d\n",s);
    for(i=0; i<4; i++)
    {
        for(j=0; j<4; j++)
        {
            if(a[i][j]==1)
            {
                printf("%d %d\n",i+1,j+1);
            }
        }
    }
    return 0;
}


猜你喜欢

转载自blog.csdn.net/titi2018815/article/details/81227388
今日推荐