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的开关,-代表开,+代表关,所有开关都是-才能开冰箱.操纵一个开关的时候这行和这列的开关都会被影响到.也就是开一个开关总共有七个开关状态被改变.求需要扳几次开关,以及开关的位置.
思路:
看上去和熄灯差不多,但是不可以用熄灯的方法做,因为开关会影响到一行一列的开关,会把之前的状态破坏掉,百度了一下发现如果要只想改变i行j列的开关状态,则把i行和j列的开关统统扳一遍,效果就是只改变(i,j)开关的状态.用一个数组b记录开关的状态,因为同一开关扳两次相当于没影响,到最后只需要统计扳动开关次数除以2的余数即可.
代码:
#include <cstring>
#include <cstdio>
void setbut(int b[][4],int m,int i){
if(m==1) //设置行
for(int j=0;j<4;j++)
b[i][j]++;
else //设置列
for(int j=0;j<4;j++)
b[j][i]++;
}
int calsteps(int a[][4],int b[][4]){
int i,j,minstep=0;
for(i=0;i<4;i++)
for(j=0;j<4;j++){
if(a[i][j]==1){
setbut(b,1,i);
setbut(b,0,j);
b[i][j]--;
}
}
for(i=0;i<4;i++)
for(j=0;j<4;j++){
if(b[i][j]%2==0)
b[i][j]=0;
if(b[i][j]%2==1)
minstep++;
}
return minstep;
}
int main()
{
char ch;
int minstep,a[4][4],b[4][4];
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
if((ch=getchar())=='+')
a[i][j]=1;
else a[i][j]=0;
}
getchar();
}
minstep=calsteps(a,b);
printf("%d\n",minstep);
for(int i=0;i<4;i++)
for(int j=0;j<4;j++){
if(b[i][j]%2==1)
printf("%d %d\n",i+1,j+1);
}
return 0;
}