UVA - 10001 Garden of Eden (回溯)


  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.

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).


LeftCellRightNew     
[i-1][i][i + 1]State     
0000 0 * 20   
 0011 1 * 21   
0100 0 * 22   
0111 1 * 23   
1001 1 * 24   
1010 0 * 25   
1101 1 * 26   
1110 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. The Identity 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  (  $4 \le N \le 32$ ) 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
 
 

题目解析:

我不知道,为理解这题脑细胞耗死了多少?看了网络上的题解都未能明白题意,为了能让大家明白我这题,尽量描述的通俗易懂。


我想分几个部分来说明问题。

(1)目的:

有256中自动机。

题目给出的3个变量分别是:自动机的种类,新字符串的长度,新字符串。

问新字符串是否可以,按照自动机的规则,由其他的旧字符串转化而来,

可以的话输出REACHABLE,否则输出GARDEN OF EDEN。


(2)规则:

LeftCellRightNew     
[i-1][i][i + 1]State     
0000 0 * 20   
 0011 1 * 21   
0100 0 * 22   
0111 1 * 23   
1001 1 * 24   
1010 0 * 25   
1101 1 * 26   
1110 0 * 27   
  90 =Automaton Identifier
 

请看上面的表格,

左边3列:原字符串当前格子(Cell)以及左右两边的格子的状态,

第4列:当原字符串某一段连续的3个格子符合第4列对应的左边3列所描述的状态时,那么就把这3个格子中间的那个格子的值(Cell[i])转化成New State的值。

由于左边这3列描述了所有可能的情况,所以对任意一个字符串,总可以通过这个自动机描述的法则转化成一个新字符串。


上面的话什么意思呢?


对于编译方式

例如:

自动机编号是 8 ,将8转化为2进制是00001000,每行的都对应8的一个2进制码
其意思为:
0 0 0 -->0
0 0 1 -->0
0 1 0 -->0
0 1 1 -->0

1 0 0 -->1

1 0 1 -->0

1 1 0 -->0

1 1 1 -->0

然后题目要求就是问给出的字符串,在这种转化机制下,能否由其他串转化来。


(3)实现步骤

现在我按照上面表,自动机90 的规则 , 将新串 10101 逆推出旧串。

逆推的规则见上图

 0 0 1 -> 1 

新串:      1 0 1 0 1

旧串:   0 0 1

 0 1 0 - > 0

新串:      1 0 1 0 1

旧串:   0 0 1 0

1 0 1 -> 0 

新串:      1 0 1 0 1

旧串:   0 0 1 0 1

0 1 0 -> 0

新串:      1 0 1 0 1

旧串:   0 0 1 0 1 0

1 0 0 -> 1

新串:      1 0 1 0 1

旧串:   0 0 1 0 1 0 0

由前面两个位置推出后面一个位置,读者可以自行推导一下,上面是新串逆推出旧串的过程。

再看这句话。

The actual arrangement of the cells will be along a circumference, so that the last cell is next to the first.

细胞将沿着圆周,使得最后一个细胞与第一个相连。


还记得我们是怎么推导出第一个位置的吗?新串前两个位置的两个字符,是由最后两个位置的字符来决定的。换句话说,如果能生成一个环,那就要满足后面两个和前面两个相同,才能能构成环,那么输出REACHABLE,否则输出GARDEN OF EDEN。


可能读者还有一个疑惑,为什么编号为204的自动机是一个every state evolves to itself?

因为204的编码自动机编码为00110011,任何2进制的字符串,经过204号规则,推出的都是它自己本身,如果不信的话,读者可以自行推导。


AC代码:

#include <stdio.h>
#include <string.h>
const int N = 40;
int n;
int vis[N],state[N],newStr[N];//vis数组表示旧的字符串
bool ok;
int dir[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} };
void dfs(int cur) {  //cur表示当前newStr的下标
	if( cur == n) {
		if( vis[0] == vis[n] && vis[1] == vis[n+1]) { //如果能构成环,则符合题意
			ok = true;
		}
		return ;
	}
	for(int i = 0; i < 8; i++) { //查找8种编码
		if( state[i] == newStr[cur]) { //找到与当前newStr相同的state编码
			if(!cur) { //如果是首个位置,则vis的值可以任意取与其对应的
				vis[cur] = dir[i][0];
				vis[cur+1] = dir[i][1];
				vis[cur+2] = dir[i][2];
				dfs(cur+1);
			}
			else if(vis[cur] == dir[i][0]&& vis[cur+1] == dir[i][1]) {
				vis[cur+2] = dir[i][2];
				dfs(cur+1);
			}
		}
	}
}
int main() {
	char str[N];
	int k;
	while( scanf("%d%d%s",&k,&n,str) != EOF) {
		memset(vis,0,sizeof(vis));
		memset(state,0,sizeof(state));
		memset(newStr,0,sizeof(newStr));
		ok = false;
		for(int i = 0; i < 8; i++) { //求出每行对应的2进制编码
			state[i] = k % 2;
			k /= 2;
		}
		for(int i = 0; i < n; i++) {
			newStr[i] = str[i] - '0';
		}
		dfs(0);
		if( ok ) {
			printf("REACHABLE\n");
		}
		else {
			printf("GARDEN OF EDEN\n");
		}
	}
	return 0;
}



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值