Garden of Eden uva +回溯

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

题意:这个题意理解起来很难,如果理解了,也不难做出。

首先,看图

 题目中说它有256个不同的编码,也就是图中new state列的编码类型,由一系列1和0组成。而left-cell-right的组合是固定的,有八种类型,cell就是原串。题目要求,给编码类型,经过编码过后的新串,由新串逆推,是否能得到原串,能则输出“REACHABLE”,不能则“Garden of Eden”。那怎么逆推呢?可将编码类型转成八位长度的二进制,然后由新串根据这个八位二进制的编码,选择left-cell-right组合,找到直到符合规则的,可用dfs来搜索。

代码:

#include<iostream>
#include<cstdio>
using namespace std;
int S[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 a[50],p[50],len;
int code[8];
bool dfs(int cur){

    if(cur==len||cur==len-1){
        for(int i=0;i<8;i++){
        if(code[i]==a[cur]&&
           p[cur-1]==S[i][0]&&
           p[cur]==S[i][1]&&
           p[cur+1]==S[i][2]){
            if(cur==len-1&&!dfs(cur+1))
            return 0;
            else
            return 1;
            }
         }
        return 0;
    }
    for(int i=0;i<8;i++)
    if(a[cur]==code[i]&&
       p[cur-1]==S[i][0]&&
       p[cur]==S[i][1])
    {

        p[cur+1]=S[i][2];
        if(dfs(cur+1))
        return 1;


    }
    return 0;


}
int main(){
    int k;
    char str[50];
    while(~scanf("%d%d%s",&k,&len,str)){
      for(int i=0;i<len;i++){
        a[i+1]=str[i]-'0';
      }
      for(int i=0;i<8;i++){
        code[i]=k%2;
        k/=2;

      }
      bool flag=1;
     for(int i=0;i<8;i++)
     if(a[1]==code[i])
     {
        p[0]=S[i][0];
        p[1]=S[i][1];
        p[2]=S[i][2];
        p[len]=S[i][0];
        p[len+1]=S[i][1];
        if(dfs(2)){
            flag=0;
            break;
        }
     }
     if(flag) cout<<"GARDEN OF EDEN\n";
    else cout<<"REACHABLE\n";
    }


return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值