题目地址:
题目描述:
Garden of Eden
Cellular automata are mathematical idealizations of physical systems in which both space and time are discrete, and the physical quantities take on a finite set of discrete values. A cellular automaton consists of a lattice (or array), usually infinite, of discrete-valued variables. The state of such automaton is completely specified by the values of the variables at each place in the lattice. Cellular automata evolve in discrete time steps, with the value at each place (cell) being affected by the values of variables at sites in its neighborhood on the previous time step. For each automaton there is a set of rules that define its evolution.
Garden of Eden |
For most cellular automata there are configurations (states) that are unreachable: no state will produce them by the application of the evolution rules. These states are called Gardens of Eden for they can only appear as initial states. As an example consider a trivial set of rules that evolve every cell into 0; for this automaton any state with non-zero cells is a Garden of Eden.
In general, finding the ancestor of a given state (or the non-existence of such ancestor) is a very hard, compute intensive, problem. For the sake of simplicity we will restrict the problem to 1-dimensional binary finite cellular automata. This is, the number of cells is a finite number, the cells are arranged in a linear fashion and their state will be either 0 or 1. To further simplify the problem each cell state will depend only on its previous state and that of its immediate neighbors (the one to the left and the one to the right).
The actual arrangement of the cells will be along a circumference, so that the last cell is next to the first.
Problem definition
Given a circular binary cellular automaton you must find out whether a given state is a Garden of Eden or a reachable state. The cellular automaton will be described in terms of its evolution rules. For example, the table below shows the evolution rules for the automaton: Cell=XOR(Left,Right).
Left | Cell | Right | New | |||||
[i-1] | [i] | [i + 1] | State | |||||
0 | 0 | 0 | 0 | 0 * 20 | ||||
0 | 0 | 1 | 1 | 1 * 21 | ||||
0 | 1 | 0 | 0 | 0 * 22 | ||||
0 | 1 | 1 | 1 | 1 * 23 | ||||
1 | 0 | 0 | 1 | 1 * 24 | ||||
1 | 0 | 1 | 0 | 0 * 25 | ||||
1 | 1 | 0 | 1 | 1 * 26 | ||||
1 | 1 | 1 | 0 | 0 * 27 | ||||
90 | = | Automaton Identifier | ||||||
Notice that, with the restrictions imposed to this problem, there are only 256 different automata. An identifier for each automaton can be generated by taking the New State vector and interpreting it as a binary number (as shown in the table). For instance, the automaton in the table has identifier 90. TheIdentity automaton (every state evolves to itself) has identifier 204.
Input
The input will consist of several test cases. Each input case will describe, in a single line, a cellular automaton and a state. The first item in the line will be the identifier of the cellular automaton you must work with. The second item in the line will be a positive integer N ( ) indicating the number of cells for this test case. Finally, the third item in the line will be a state represented by a string of exactly N zeros and ones. Your program must keep reading lines until the end of the input (end of file).Output
If an input case describes a Garden of Eden you must output the string GARDEN OF EDEN . If the input does not describe a Garden of Eden (it is a reachable state) you must output the string REACHABLE .The output for each test case must be in a different line.
Sample Input
0 4 1111 204 5 10101 255 6 000000 154 16 1000000000000000
Sample Sample Output
GARDEN OF EDEN REACHABLE GARDEN OF EDEN GARDEN OF EDEN题意:细胞有很多种变异方式,或者说生成方式,根据生成的状态推导哪些是可达的哪些是不可达的,可达即形成循环。
题解:DFS逆推导出状态判断可达。
代码:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
int num=0;
int N=0;
char str[50]={'\0'};
int Nstr[50]={0};
int Bit[50]={0};
int Genstr[50]={0};
int Maze[8][3]={{0,0,0},{0,0,1},{0,1 ,0},{0,1,1},{1,0,0},{1,0,1},{1,1,0},{1,1,1}};
int flag=1;
/*dfs the Nstr,generate the string Genstr*/
int DFS(int cur)
{
if(flag)
{
return(0);
}
if(cur==N)
{
if(Genstr[0]==Genstr[N]&&Genstr[1]==Genstr[N+1])//why?? the Garden of Eden's denfine is what??????
{
flag=1;
}
}
else
{
int i=0;
if(cur==0)
{
for(i=0;i<=8-1;i++)
{
if(Nstr[cur]==Bit[i])
{
Genstr[cur]=Maze[i][0];
Genstr[cur+1]=Maze[i][1];
Genstr[cur+2]=Maze[i][2];
DFS(cur+1);
}
}
}
else
{
for(i=0;i<=8-1;i++)
{
if(Nstr[cur]==Bit[i]&&Genstr[cur]==Maze[i][0]&&Genstr[cur+1]==Maze[i][1])
{
Genstr[cur+2]=Maze[i][2];
DFS(cur+1);
}
}
}
}
return(0);
}
/*for test*/
int test()
{
return(0);
}
/*main process*/
int MainProc()
{
while(scanf("%d%d%s",&num,&N,str)!=EOF)
{
flag=0;//the initialize flag is 0 indicate that it is garden of eden in the beginning
int k=num;// 10D->2B
int i=0;
for(i=0;i<=8-1;i++)
{
Bit[i]=k%2;//from low to the high bit
k/=2;
}
for(i=0;i<=N;i++)
{
Nstr[i]=str[i]-'0';//string to the number
}
DFS(0);
if(flag)
{
printf("REACHABLE\n");
}
else
{
printf("GARDEN OF EDEN\n");
}
}
return(0);
}
int main(int argc, char const *argv[])
{
/* code */
MainProc();
return 0;
}