dfs
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;
public class Main {
private static final int N = 4;
private static char[][] g = new char[N][N];
private static List<Node> ans = new ArrayList<>();
private static List<Node> tmp = new ArrayList<>();
private static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
private static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
static class Node {
int x;
int y;
public Node(int x, int y) {
this.x = x;
this.y = y;
}
}
private static void turn_one(int x, int y) {
if (g[x][y] == '+') g[x][y] = '-';
else g[x][y] = '+';
}
private static void turn_all(int x, int y) {
for (int i = 0; i < 4; i ++) {
turn_one(x, i);
turn_one(i, y);
}
turn_one(x, y);
}
private 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;
}
private static void dfs(int x, int y) {
// 所有的把手都操作完了。
if (x == 3 && y == 4) {
if (check()) {
if (ans.isEmpty() || ans.size() > tmp.size())
ans = new ArrayList<>(tmp);
}
return;
}
// 判断边界,如果y出界则移动到下一行
if (y == 4) {
x ++;
y = 0;
}
// 操作把手(x, y)
turn_all(x, y);
tmp.add(new Node(x, y));
dfs(x, y + 1);
// 恢复现场
tmp.remove(tmp.size() - 1);
turn_all(x, y);
// 不操作把手
dfs(x, y + 1);
}
public static void main(String[] args) throws IOException {
for (int i = 0; i < 4; i ++) g[i] = br.readLine().toCharArray();
dfs(0, 0);
bw.append(ans.size() + "\n");
for (Node op: ans) {
bw.append((op.x + 1) + " " + (op.y + 1) + "\n");
}
br.close();
bw.flush();
bw.close();
}
}
二进制
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;
public class Main {
private static final int N = 4;
private static char[][] g = new char[N][N];
private static char[][] backup = new char[N][N];
private static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
private static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
static class Node {
int x;
int y;
public Node(int x, int y) {
this.x = x;
this.y = y;
}
}
// 二维坐标转换一维
private static int get(int x, int y) {
return x * 4 + y;
}
private static void turn_one(int x, int y) {
if (g[x][y] == '+') g[x][y] = '-';
else g[x][y] = '+';
}
private static void turn_all(int x, int y) {
for (int i = 0; i < 4; i ++) {
turn_one(x, i);
turn_one(i, y);
}
turn_one(x, y);
}
private 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 void main(String[] args) throws IOException {
for (int i = 0; i < 4; i ++) g[i] = br.readLine().toCharArray();
List<Node> res = new ArrayList<>();
for (int op = 0; op < 1 << 16; op ++) {
List<Node> tmp = new ArrayList<>();
for (int i = 0; i < 4; i ++) System.arraycopy(g[i], 0, backup[i], 0, N); // 备份
// 进行操作
for (int i = 0; i < 4; i ++)
for (int j = 0; j < 4; j ++)
if ((op >> get(i, j) & 1) == 1) {
tmp.add(new Node(i, j));
turn_all(i, j);
}
// 判断所有开关是否全打开
if (check()) {
if (res.isEmpty() || res.size() > tmp.size())
res = new ArrayList<>(tmp);
}
for (int i = 0; i < 4; i ++) System.arraycopy(backup[i], 0, g[i], 0, N); // 还原
}
bw.append(res.size() + "\n");
for (Node op: res) bw.append((op.x + 1) + " " + (op.y + 1) + "\n");
br.close();
bw.flush();
bw.close();
}
}