点击打开链接
题目太难懂了,看了老半天搞不清楚。。。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int n, m, d[8], g[100], f[100], ff;
string s;
void work(int x)
{
if (ff) return;
if (x == m)
{
f[m] = f[0]; f[m + 1] = f[1];
int y = f[m - 2] * 4 + f[m - 1] * 2 + f[m];
int z = f[m - 1] * 4 + f[m] * 2 + f[m + 1];
if (d[y] == g[m - 1] && d[z] == g[0]) ff = 1;
return;
}
for (int i = 0; i <= 1; i++)
{
f[x] = i;
if (x < 2) work(x + 1);
else
{
int y = f[x] + f[x - 1] * 2 + f[x - 2] * 4;
if (d[y] == g[x - 1]) work(x + 1);
}
if (ff) return;
}
}
int main()
{
while (cin >> n >> m >> s)
{
for (int i = 0; i < 8; i++, n /= 2) d[i] = n % 2;
for (int i = 0; i < m; i++) g[i] = s[i] - '0';
ff = 0; work(0);
if (ff) cout << "REACHABLE" << endl;
else cout << "GARDEN OF EDEN" << endl;
}
return 0;
}