forked from nanwan03/poj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
1753 Flip Game - Enumeration+dfs.java
67 lines (66 loc) · 1.63 KB
/
1753 Flip Game - Enumeration+dfs.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import java.util.*;
public class Main {
private static int steps = 0x3f3f3f3f;
private static int[] dx={0,0,0,1,-1};
private static int[] dy={0,1,-1,0,0};
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int source = 0;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 4; ++i) {
sb.append(in.nextLine().trim());
}
for (int i = 0; i < sb.length(); ++i) {
source = (source << 1) + (sb.substring(i, i + 1).equals("b") ? 1 : 0);
}
if (solve(source)) {
System.out.println(steps);
} else {
System.out.println("Impossible");
}
}
private static boolean solve(int source) {
for (int i = 0; i < 16; ++i) {
int temp = source;
int number = 0;
for (int j = 0; j < 4; ++j) {
if ((i & (1 << j)) > 0) {
for (int k = 0; k <= 4; ++k) {
int x = 0 + dx[k];
int y = j + dy[k];
temp = flip(x, y, temp);
}
number++;
}
}
dfs(1, number, temp, 0);
dfs(1, number, temp, 1);
}
return steps != 0x3f3f3f3f;
}
private static int flip(int x, int y, int source) {
if (x >= 0 && x < 4 && y >= 0 && y < 4) {
source ^= 1 << (x * 4 + y);
}
return source;
}
private static void dfs(int cur, int number, int source, int flag) {
if (cur == 4) {
if (source == 0xffff || source == 0) {
steps = Math.min(number, steps);
}
return;
}
for (int i = cur - 1, j = 0; j < 4; ++j) {
if( (((source & (1 << (i * 4 + j))) >> (i * 4 + j)) ^ flag) == 1) {
for (int k = 0; k <= 4; ++k) {
int x = cur + dx[k];
int y = j + dy[k];
source = flip(x, y, source);
}
number++;
}
}
dfs(cur + 1, number, source, flag);
}
}