// Garden of Eden (Eden) // PC/VA IDs: 110806/10001, popularity: B, success rate: average level: 2 // verdict: accepted // submission date: 2011-08-11 // UV Run Time: 0.660 S // copyright (c) 2011, Qiu. Metaphysis # Yeah dot net // you can use the backtracking method to find the precursor of a given State. If you can find the precursor, It is reachable. If you cannot find the precursor, It is the Eden state. //// It is worth noting that someone on the Internet uses the following solution: Simulate the evolution of cellular automation, and determine the relationship between new States, initial states, and generated states, if the new status is the same as the initial status, the initial status is reachable. If the new status is the same as a previously generated status, a loop is formed, it will not reach the initial state, and the initial state will be regarded as the Eden state. This solution is incorrect //. Assume that there is a State A and Evolution Rule E, from initial State A through a series of states to status B, then, when a series of statuses such as status A and status B reach status B again, the preceding solution is used to determine the statuses from status A to status B, will come to the wrong conclusion of the Eden state. /// For more information, see: http://mathworld.wolfram.com/ElementaryCellularAutomaton.html. # Include <iostream> # include <vector> using namespace STD; # define codesize 8 bool finished; bool is_garden; // obtain the corresponding conversion rules based on the status of the current cell and the left and right cells. Int indexer (int A, int B, int c) {return (A <2) + (B <1) + C ;} void Evolution (vector <int> & A, vector <int> & B, int automaton []) {int last =. size ()-1; B [0] = automaton [indexer (A [last], a [0], a [1])]; for (INT I = 1; I <last; I ++) B [I] = automaton [indexer (A [I-1], a [I], a [I + 1])]; B [last] = automaton [indexer (A [last-1], a [last], a [0])];} // compare whether the two statuses are the same. Bool equal (vector <int> & A, vector <int> & B) {for (INT I = 0; I <. size (); I ++) if (a [I]! = B [I]) return false; return true;} // trace back to find the precursor. Void backtrack (vector <int> & ancestor, vector <int> & target, int automatic [], int current) {If (current = (ancestor. size ()-2) {vector <int> now (ancestor. size (); evolution (ancestor, now, automaton); If (equal (now, target) {finished = true; is_garden = false ;}} else {for (INT I = 0; I <= 1; I ++) {int Index = indexer (ancestor [current], ancestor [current + 1], I ); if (automaton [Index] = target [current + 1]) {Ancestor [current + 2] = I; backtrack (ancestor, target, automatic, current + 1); If (finished) return ;}}}} // determine whether a precursor exists. Bool no_ancestor (vector <int> & target, int automaton []) {int cells = target. size (); vector <int> ancestor (cells); For (INT I = 0; I <codesize; I ++) if (automaton [I] = target [0]) {ancestor [cells-1] = (I & 4)> 2 ); ancestor [0] = (I & 2)> 1); ancestor [1] = (I & 1); finished = false; is_garden = true; backtrack (ancestor, target, automaton, 0); If (is_garden = false) return false;} return true;} // determines whether the given condition is No is in Eden state. Bool is_garden_of_eden (INT identifier, int cells, string state) {// identifies some special cellular automation. If (identifier = 204) return false; // when the automatic machine number is 0, if there is a non-0 lattice, It is the Garden of Eden. If (identifier = 0) {for (INT I = 0; I <state. Length (); I ++) if (State [I]! = '0') return true; return false;} // when the automatic machine number is 255, if there is a non-1 lattice, It is the Garden of Eden. If (identifier = 255) {for (INT I = 0; I <state. Length (); I ++) if (State [I]! = '1') return true; return false;} // gets the encoding Transformation Rules for the initial state and the automatic machine. Vector <int> ancestor (cells); For (INT I = 0; I <cells; I ++) ancestor [I] = (State [I]-'0 '); int automaton [codesize]; for (INT I = 0; I <codesize; I ++) {automaton [I] = identifier % 2; identifier/= 2 ;} return no_ancestor (ancestor, automaton);} int main (int ac, char * AV []) {int identifier, cells; string state; // read data. While (CIN> identifier> cells> state) {bool result = is_garden_of_eden (identifier, cells, State); cout <(result? "Garden of Eden": "reachable") <Endl;} return 0 ;}