// Getting pattern of pyramid // true - values of all cells are "1" // false - values of all cells are "0" public bool Reduce(bool areducePossible) { bool res = false; // low level pyramids reducing is possible if (areducePossible) { // and level of pyramid is 1 (value containing pyramid), // return reduce value if (PyramidLevel == 1) { res = TopValue; } // else, if level of pyramid is 2 (lowest group pyramid) and low level pyramids reducing is possible, // reduce it else if (PyramidLevel == 2) { TopValue = Top4CellPyramid.Reduce(areducePossible); BottomLeftValue = BottomLeft4CellPyramid.Reduce(areducePossible); BottomUpsideValue = BottomUpside4CellPyramid.Reduce(areducePossible); BottomRightValue = BottomRight4CellPyramid.Reduce(areducePossible); Top4CellPyramid = null; BottomLeft4CellPyramid = null; BottomUpside4CellPyramid = null; BottomRight4CellPyramid = null; res = true; } else if (PyramidLevel > 2) { Top4CellPyramid.Reduce(areducePossible); BottomLeft4CellPyramid.Reduce(areducePossible); BottomUpside4CellPyramid.Reduce(areducePossible); BottomRight4CellPyramid.Reduce(areducePossible); res = true; } PyramidLevel--; } return(res); }
static void Main(string[] args) { if (args.Length == 0) { Console.WriteLine("Error: There are on input arguments."); Console.ReadLine(); return; } String sSource = args[0]; Console.WriteLine("tribit.exe {0}", sSource); // Input data checking. If input data is invalid, then exit if (!CheckSourceData(sSource)) { Console.ReadLine(); return; } // In case of 1 cell pyramid just print it and exit if (sSource.Length == 1) { Console.WriteLine("{0}", sSource); Console.ReadLine(); return; } // Initialize entire pyramid PyramidClass Pyramid = new PyramidClass(sSource); // Print this pyramid Console.WriteLine("{0}", Pyramid.GetString()); FourCellPyramid TopPyramid = Pyramid.TopPyramid; // Transition iteration process while (true) { // Make transition step TopPyramid.DoTransition(); // Print pyramid on current step Console.WriteLine("{0}", Pyramid.GetString()); // Check pyramid for posibility of reducing it if (TopPyramid.IsReducingPossible()) { // if reduce possible, do it TopPyramid.Reduce(true); // the changes // after reducing of the pyramid we must redefine line levels for cells TopPyramid.DefLineLevels(0); // Print pyramid just after reducing Console.WriteLine("{0}", Pyramid.GetString()); } // if pattern of root pyramid is definded, then reduce this root pyramid and finish algorithm if ((TopPyramid.PyramidLevel <= 1) && TopPyramid.IsReducingPossible()) { int value = TopPyramid.TopValue ? 1 : 0; Console.WriteLine("{0}", value); break; } } Console.ReadLine(); return; }