File size: 1,617 Bytes
c4b0eef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/*
  >>~~ UVa Online Judge ACM Problem Solution ~~<<

  ID: 10001
  Name: Garden of Eden
  Problem: https://onlinejudge.org/external/100/10001.pdf
  Language: C++

  Author: Arash Shakery
  Email: arash.shakery@gmail.com
*/

#include <stdio.h>
#include <iostream>
using namespace std;

#define isset(n,i) (((n)>>(i))&1)

int automaton,n;
bool nstate[40],bstate[40];

bool BT(int i)
{
    if(i==n)
    {
        int t1=4*bstate[n-2] + 2*bstate[n-1] + bstate[0];
        int t2=4*bstate[n-1] + 2*bstate[0] + bstate[1];
        if(nstate[n-1] == isset(automaton,t1)  &&  nstate[0]==isset(automaton,t2))
            return true;
        else
            return false;
    }

    bool ans=false;

    int t1=4*bstate[i-2] + 2*bstate[i-1] + 1;
    if(isset(automaton,t1) == nstate[i-1])
    {
        bstate[i]=1;
        ans|= BT(i+1);
    }


    int t2=4*bstate[i-2] + 2*bstate[i-1] + 0;
    if(isset(automaton,t2) == nstate[i-1])
    {
        bstate[i]=0;
        ans|= BT(i+1);
    }

    return ans;
}


bool answer()
{
    int i;
    for(i=0; i<8; i++)
        if( isset(automaton,i) == nstate[1] )
        {
            bstate[0]= isset(i,2);
            bstate[1]= isset(i,1);
            bstate[2]= isset(i,0);
            if(BT(3))
                return true;
        }

    return false;
}


int main()
{
    char s[40];
    int i;

    while(cin>>automaton>>n>>s)
    {
        for(i=0; s[i]; i++)
            if(s[i]=='1')
                nstate[i]=1;
            else
                nstate[i]=0;

        if(answer())
            puts("REACHABLE");
        else
            puts("GARDEN OF EDEN");
    }
}