forked from nanwan03/poj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
1222 EXTENDED LIGHTS OUT.java
50 lines (50 loc) · 1.35 KB
/
1222 EXTENDED LIGHTS OUT.java
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
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int testcase = in.nextInt();
int[][] puzzle = new int[6][8];
int[][] press = new int[6][8];
for (int i = 1; i <= testcase; ++i) {
System.out.println("PUZZLE #" + i);
for (int row = 1; row < 6; ++row) {
for (int col = 1; col < 7; ++col) {
puzzle[row][col] = in.nextInt();
}
}
enumerate(puzzle, press);
for (int row = 1; row < 6; ++row) {
for (int col = 1; col < 7; ++col) {
System.out.print(press[row][col] + " ");
}
System.out.println("");
}
}
}
private static void enumerate(int[][] puzzle, int[][] press) {
for (int col = 1; col < 7; ++col) {
press[1][col] = 0;
}
while (!guess(puzzle, press)) {
press[1][1]++;
int col = 1;
while (press[1][col] > 1) {
press[1][col] = 0;
press[1][++col]++;
}
}
}
private static boolean guess(int[][] puzzle, int[][] press) {
for (int row = 1; row < 5; ++row) {
for (int col = 1; col < 7; ++col) {
press[row + 1][col] = (puzzle[row][col] + press[row][col] + press[row - 1][col] + press[row][col - 1] + press[row][col + 1]) % 2;
}
}
for (int col = 1; col < 7; ++col) {
if ((press[5][col - 1] + press[5][col] + press[5][col + 1] + press[4][col]) % 2 != puzzle[5][col]) {
return false;
}
}
return true;
}
}