AcWing 116. 飞行员兄弟-java暴力代码
原题链接
简单
作者:
health_1
,
2025-02-24 10:56:01
· 江西
,
所有人可见
,
阅读 1
import java.util.*;
public class Main {
private static char[][] g = new char[5][5];
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
for(int i = 0; i < 4; i ++) {
String str = in.nextLine();
for(int j = 0; j < 4; j ++) {
g[i][j] = str.charAt(j);
}
}
List<PII> res = new ArrayList<PII>();
char[][] backup = new char[5][5];
for(int i = 0; i < 4; i ++) {
backup[i] = Arrays.copyOf(g[i], g[i].length);
}
for(int i = 0; i < Math.pow(2, 16); i ++) {
List<PII> temp = new ArrayList<PII>();
for(int j = 0; j <= 15; j ++) {
if((i >> j & 1) == 1) {
int x = j / 4;
int y = j % 4;
turn(x, y);
temp.add(new PII(x, y));
}
}
boolean flag = true;
for(int x = 0; x < 4; x ++) {
for(int y = 0; y < 4; y ++) {
if(g[x][y]=='+')flag = false;
}
}
if(flag) {
if(res.isEmpty()||res.size()>temp.size()) {
res = new ArrayList<PII>(temp);
}
}
for(int l = 0; l < 4; l ++) {
g[l] = Arrays.copyOf(backup[l], backup[l].length);
}
}
System.out.println(res.size());
for(PII r:res) {
System.out.println((r.x + 1)+" "+(r.y + 1));
}
}
public static void turn(int h, int l) {
for(int i = 0; i < 4; i ++) {
g[h][i] = ((g[h][i] == '-')?'+':'-');
g[i][l] = ((g[i][l] == '-')?'+':'-');
}
g[h][l] = ((g[h][l] == '-')?'+':'-');
}
}
class PII{
public int x;
public int y;
public PII(int x, int y){
this.x = x;
this.y = y;
}
}