forked from sitz/UVa-Online-Judge
-
Notifications
You must be signed in to change notification settings - Fork 1
/
10001.cpp
105 lines (97 loc) · 1.93 KB
/
10001.cpp
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <bits/stdc++.h>
using namespace std;
int automaton[2][2][2], state[40], map_[160][160], reachable[160], n, maxn;
void init_automaton(int identifier)
{
memset(automaton, 0, sizeof(automaton));
for (int i = 0; i <= 1; ++i)
{
for (int j = 0; j <= 1; ++j)
{
for (int k = 0; k <= 1; ++k)
{
automaton[i][j][k] = identifier & 1;
identifier >>= 1;
}
}
}
}
void init_state(char str_state[])
{
memset(state, 0, sizeof(state));
for (int i = 0; i < strlen(str_state); ++i)
{
state[i] = str_state[i] - '0';
}
}
void add_edge(int id1, int cur1, int next1, int id2, int cur2, int next2)
{
int node1 = id1 * 4 + cur1 * 2 + next1;
int node2 = id2 * 4 + cur2 * 2 + next2;
map_[node1][node2] = 1;
}
void init_map_(void)
{
memset(map_, 0, sizeof(map_));
maxn = n * 4;
for (int id = 0; id < n; ++id)
{
for (int cur = 0; cur <= 1; ++cur)
{
for (int next = 0; next <= 1; ++next)
{
for (int next2 = 0; next2 <= 1; ++next2)
{
int next_id = (id + 1) % n;
if (state[next_id] == automaton[cur][next][next2])
{
add_edge(id, cur, next, next_id, next, next2);
}
}
}
}
}
}
int calc_state()
{
for (int first = 0; first < 4; ++first)
{
memset(reachable, 0, sizeof(reachable));
reachable[first] = 1;
for (int id = 0; id < n - 1; ++id)
{
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
if (reachable[id * 4 + i] && map_[id * 4 + i][(id + 1) * 4 + j])
{
reachable[(id + 1) * 4 + j] = 1;
if (id == n - 2)
{
if (map_[(id + 1) * 4 + j][first])
{
return 1;
}
}
}
}
}
}
}
return 0;
}
int main()
{
int identifier, is_reachable;
char str_state[1000];
while (scanf("%d%d%s", &identifier, &n, str_state) != EOF)
{
init_automaton(identifier);
init_state(str_state);
init_map_();
is_reachable = calc_state();
printf(is_reachable ? "REACHABLE\n" : "GARDEN OF EDEN\n");
}
return 0;
}