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

题意:一个冰箱有16个并排成4*4矩阵的手柄,每个手柄有两种状态,打开(“ - ”),关闭(“ + ”)。只有16把手柄都处于打开状态时,才能打开冰箱。我们可以每次可以选择改变某把手柄的状态(开-->关,关-->开),同时此行此列的所有手柄的状态都会改变。问想要打开冰箱最少需要改变多少次手柄的状态,并且输出其路径。

思路:我们可以记录一下每个点的转换次数,如果为奇数的话,相当于这一点翻转了,如果为偶数的话就仍然是初始状态。根据这个特性,我们可以对每一个“+”进行翻转,同时对此行此列的都要进行翻转。全部翻转结束后,所有点的翻转次数为奇数的点就相当于是对此点进行的翻转,又因为翻转顺序没有要求,因此我们可以遍历一遍,把所有的点都记录一下,输出即可。

代码如下:

#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
struct node
{
    int x,y;
} list[20];
int main()
{
    char str[5][5];
    int num[5][5]= {0};
    for(int i=0; i<4; i++)  
        cin>>str[i];
    for(int i=0; i<4; i++)
    {
        for(int j=0; j<4; j++)
        {
            if(str[i][j]=='-')
                continue;
            num[i][j]=!num[i][j];  //“+”的点翻转,
            for(int k=0; k<4; k++)  //此行此列都翻转
            {
                num[k][j]=!num[k][j];
                num[i][k]=!num[i][k];
            }
        }
    }
    int ans=0;
    for(int i=0; i<4; i++)
        for(int j=0; j<4; j++)
            if(num[i][j])    //记录此点
            {
                list[ans].x=i+1;
                list[ans].y=j+1;
                ans++;
            }
    cout<<ans<<endl;
    for(int i=0; i<ans; i++)
        cout<<list[i].x<<" "<<list[i].y<<endl;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值