【思路】
16个开关,用二进制表示对16个开关的可能操作方案。即从0到2^16 -1开始枚举,该数的01二进制串1表示对应位置摁,0表示对应位置不摁。位置与矩阵的对应关系如下:
import java.io.*;
import java.util.ArrayList;
import java.util.List;
class node{
int x, y;
public node(int xx, int yy){
x = xx;
y = yy;
}
}
public class Main{
/*
同一行翻偶数次抵消 奇数次反转
*/
static int N = 4;
static char g[][] = new char[N][N];
static char backup[][] = new char[N][N];
//十字形翻转
public static void change(int i, int j){
for(int x = 0; x < 4; x ++){
g[i][x] = g[i][x]== '-'? '+':'-';
g[x][j] = g[x][j]== '-'? '+':'-';
}
//注意x,y这个位置上面翻了两次 要翻多一次回来
g[i][j] = g[i][j]== '-'? '+':'-';
}
public static boolean check(){
for(int i = 0; i < 4; i ++)
for(int j = 0; j < 4; j++)
if(g[i][j] == '+') return false;
return true;
}
//将二维坐标映射为一维
public static int get(int x, int y){
return x * 4 + y;
}
public static void main(String args[])throws Exception{
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
for(int i = 0; i < 4; i++)
g[i] = bf.readLine().toCharArray();
for(int i = 0; i < 4; i ++)
for(int j = 0; j < 4; j++)
backup[i][j] = g[i][j];
List<node> res = new ArrayList<>();
for(int op = 0; op < 1 << 16; op ++){// 1 <<16 为2^16
List<node> tmp = new ArrayList<>();
//还原
for(int i = 0; i < 4; i ++)
for(int j = 0; j < 4; j++)
g[i][j] = backup[i][j];
for(int i = 0; i < 4; i ++)
for(int j = 0; j < 4; j ++)
if( ( op >> get(i, j) &1 )== 1){//0 1 2 3 4
tmp.add( new node(i, j));
change(i, j);
}
if( check() ) //全部把手已打开
if(res.isEmpty() || res.size() > tmp.size() ) res = new ArrayList<>(tmp);
}
System.out.println(res.size() );
for(node t: res)
System.out.println( t.x + 1 +" "+ (t.y + 1) );
}
}