0%

Acwing 95费解的开关(思维、二进制、递推)

题目链接

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import java.io.*;
import java.util.Arrays;

public class Main {
private static int T;
private static char[][] mp;
private static char[][] g;
private static int[] dx = new int[] {1, 0, -1, 0, 0};
private static int[] dy = new int[] {0, 1, 0, -1, 0};

static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
static BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));

public static void main(String[] args) throws IOException {
T = Integer.parseInt(in.readLine());
while(T-- > 0) {
mp = new char[5][5];
for(int i = 0; i < 5; i++) {
String tmp = in.readLine();
mp[i] = tmp.toCharArray();
}
solve();
in.readLine();
}
in.close();
out.flush();
out.close();
}

public static void turn(int x, int y) {
for(int i = 0; i < 5; i++) {
int xx = x + dx[i];
int yy = y + dy[i];
if(xx < 0 || xx >= 5 || yy < 0 || yy >= 5) continue;
g[xx][yy] ^= 1;
}
}

public static void solve() throws IOException{
int res = 7;
g = new char[5][5];
for(int i = 0; i < 5; i++) {
g[i] = Arrays.copyOf(mp[i], mp[i].length);
}

for(int op = 0; op < 32; op++) {
for(int i = 0; i < 5; i++) {
g[i] = Arrays.copyOf(mp[i], mp[i].length);
}
int step = 0;
for(int i = 0; i < 5; i++) {
if((op >> i & 1) == 1) {
step++;
turn(0, i);
}
}
for(int i = 0; i < 4; i++) {
for(int j = 0; j < 5; j++) {
if(g[i][j] == '0') {
step++;
turn(i + 1, j);
}
}
}
boolean dark = false;
for(int i = 0; i < 5; i++) {
if(g[4][i] == '0') {
dark = true;
break;
}
}
if(!dark) {
res = Math.min(res, step);
}
}
if(res > 6) res = -1;
out.write(res + "\n");
}
}
求大佬赏个饭