poj 2965 The Pilots Brothers' refrigerator

来源:互联网 发布:优衣库 淘宝 代购 编辑:程序博客网 时间:2024/06/06 00:01

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 rowi 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 34 4

 

题意:

在一个冰箱门上有16个把手,‘—’代表门是开着的,‘+’代表门是关着的,要想打开冰箱,必须将16个把手都打开,而当我们对位置[i,j]的把手操作时,它所在的行与列的其他把手状态也会发生改变。问打开冰箱所需要的最少步数,并且输出操作的过程。

 

借鉴高手的思路:

要使一个为'+'的符号变为'-',则对其相应的行和列的操作次数肯定是奇数次。

设置一个4*4的整型数组handle,初始值为零,用于记录每个点的操作数,那么在每个'+'上的行和列的位置都加1,得到结果模2(因为一个点进行偶数次操作的效果和没进行操作一样,也就是取反),然后计算整型数组中1的个数即为总操作次数,值为1的位置是要操作的位置(其他原来操作数为偶数的因为操作并不发生效果,因此不进行操作)

以上的操作次序对结果无影响,如果存在一个最小的步骤,则此步骤一定在以上操作之中。(因为以上操作已经包含了所有可改变欲改变位置的操作了)

#include <iostream>
using namespace std;
int main()
{
    int handle[5][5]={0},i,j,k;
    char ch;
    for(i=1;i<=4;i++)
       for(j=1;j<=4;j++)
       {
          cin>>ch;
          if(ch=='+')
          {
              handle[i][j]=!handle[i][j];
              for(k=1;k<=4;k++)
              {
                  handle[i][k]=!handle[i][k];
                  handle[k][j]=!handle[k][j];
              }
          }
       }
    k=0;
    int tempx[16],tempy[16];
    for(i=1;i<=4;i++)
      for(j=1;j<=4;j++)
        if(handle[i][j]==1)
        {
            tempx[k]=i; tempy[k]=j; k++;
        }
    cout<<k<<endl;
    for(i=0;i<k;i++)
        cout<<tempx[i]<<" "<<tempy[i]<<endl;
    return 0;
}
0 0
原创粉丝点击