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.
借鉴的思路:
如果在某个(i,j)上操纵了两遍,相当于没有操纵。
对于某个(i,j),如果该它对应的行和对应的列每个元素都操纵一遍,整个图只有(i,j)位置发生了改变。
设置一个数组用于记录每个位置操纵的次数。
故若某个位置为'+',则其所在的行和所在的列的每个元素的操纵数都加一(此时注意ij位置也是加一)
最后操作数为偶数的相当于没操作,为奇数的操作。
#include<iostream>
#include<memory.h>
using namespace std;
int source[4][4];
int tempSoruce[4][4];
int result[4][4];
void copyArray(){
for(int i = 0; i <4; i++){
for(int j = 0; j <4; j++){
tempSoruce[i][j] = source[i][j];
}
}
}
int main(){
char temp;
memset(source,0,sizeof(source));
for(int i = 0; i < 4; i++){
for(int j = 0; j < 4; j++){
cin>>temp;
if(temp == '+'){
for(int k = 0; k <4; k++){
source[i][k] ++;
source[k][j]++;
}
source[i][j]--;
}
}
}
int sum = 0;
for(int i = 0; i <4; i++){
for(int j = 0; j <4; j++){
if(source[i][j]%2){
sum++;
}
}
}
cout<<sum<<endl;
for(int i = 0; i <4; i++){
for(int j = 0; j <4; j++){
if(source[i][j]%2){
cout<<i+1<<" "<<j+1<<endl;
}
}
}
}