Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* (2D) Finite Matrix - transverser
- * ---incooperated a Spiral Matrix Printer
- *
- * (C) WhiZTiM 27th June, 2013
- * FOR EDUCATIONAL PURPOSES only!
- */
- using System;
- namespace SP
- {
- //2D Coordinate...
- public struct Coord
- {
- public int X { get; set; }
- public int Y { get; set; }
- }
- //make a prepaid contract for element types
- public abstract class Element
- {
- protected bool _taken { get; set; }
- public void setTaken(bool value) { _taken = value; }
- public bool isTaken() { return _taken; }
- }
- //String elements
- public class StringElement : Element
- {
- private readonly string _value;
- public StringElement(string value) : base()
- {
- _value = value;
- }
- public string str() { return _value; }
- }
- //A thin wrapper for StringElements
- public class Spiral_Array
- {
- private readonly StringElement[,] _array;
- public Spiral_Array (StringElement[,] elements)
- {
- _array = elements;
- sizeX = _array.GetLength(0);
- sizeY = _array.GetLength(1);
- }
- public StringElement this[int x, int y]
- { get{ return _array[x,y]; }
- private set { /* */ }
- }
- public int sizeX{ get; private set; }
- public int sizeY{ get; private set; }
- }
- //This is a helper class that creates 2D 'Element' Objects from a string array
- public class Helper_2D_Elements
- {
- public StringElement[,] Elements {get; private set; }
- public Helper_2D_Elements(string[] array)
- {
- int arrSize = array.Length;
- Elements = new StringElement[arrSize,arrSize];
- for(int i = 0; i < arrSize; i++)
- {
- string[] str = array[i].Split(' ');
- for(int j = 0; j < arrSize; j++)
- {
- Elements[i,j] = new StringElement(str[j]);
- }
- }
- }
- }
- //this is an independent traveller object...
- /*
- * It takes a finite grid of size (0,0) to (X, Y)
- * transverses through it in very definite direction
- * and only passes through a point once!
- */
- public class Element_Traveller2D
- {
- public enum Direction : int { Right, Down, Left, Up }
- public enum SweepDirection { Clockwise, Aniclockwise }
- private Coord _coord;
- private readonly int LimX, LimY;
- private readonly long _max;
- private bool[,] _taken;
- private long _taken_count;
- private int _preference;
- private bool _first;
- private SweepDirection _sweep;
- public Element_Traveller2D(int X, int Y, SweepDirection sweep, Direction startingAt)
- {
- LimX = X; LimY = Y;
- _coord = new Coord{ X = 0, Y = 0 }; //defaults to 0,0
- _taken = new bool[X,Y]; //defaults to false
- _first = true;
- _max = X * Y;
- _taken_count = 0;
- _preference = (int) startingAt;
- _sweep = sweep;
- }
- private void changeDir()
- {
- if(_sweep == SweepDirection.Clockwise)
- { _preference = (++_preference) % 4; }
- else
- { _preference = (--_preference) % 4; }
- }
- public Coord nextPoint()
- {
- if(_first){
- _taken[0,0] = true;
- _taken_count++;
- _first = false;
- return _coord;
- }
- for(int _a = 0; _a < 4; _a++) {
- switch (_preference) {
- case 0:
- if(_coord.Y + 1 < LimY && !_taken[_coord.X, _coord.Y + 1])
- {
- _coord.Y += 1; _taken_count++;
- _taken[_coord.X, _coord.Y] = true;
- return _coord;
- }
- changeDir();
- break;
- case 1:
- if(_coord.X + 1 < LimX && !_taken[_coord.X + 1, _coord.Y])
- {
- _coord.X += 1; _taken_count++;
- _taken[_coord.X, _coord.Y] = true;
- return _coord;
- }
- changeDir();
- break;
- case 2:
- if(_coord.Y - 1 >= 0 && !_taken[_coord.X, _coord.Y - 1])
- {
- _coord.Y -= 1; _taken_count++;
- _taken[_coord.X, _coord.Y] = true;
- return _coord;
- }
- changeDir();
- break;
- case 3:
- if(_coord.X - 1 >= 0 && !_taken[_coord.X - 1, _coord.Y])
- {
- _coord.X -= 1; _taken_count++;
- _taken[_coord.X, _coord.Y] = true;
- return _coord;
- }
- changeDir();
- break;
- default:
- changeDir();
- break;
- }
- }
- _taken_count = _max;
- return new Coord();
- }
- public bool canMove()
- {
- return (_taken_count < _max);
- }
- }
- /* This class is responsible for instantiating the Traveller object
- * and transverse through the grid... while printing it
- * */
- public class Spiral_Printer
- {
- private readonly Spiral_Array _value;
- private readonly Element_Traveller2D traveller;
- public Spiral_Printer(Spiral_Array array)
- {
- _value = array;
- traveller = new Element_Traveller2D( _value.sizeX, _value.sizeY,
- Element_Traveller2D.SweepDirection.Clockwise, Element_Traveller2D.Direction.Right );
- }
- public void Print()
- {
- while(traveller.canMove())
- {
- Coord dim = traveller.nextPoint();
- Console.Write("{0} ", _value[dim.X, dim.Y].str());
- }
- }
- }
- public class Program
- {
- public static void Main(string[] args)
- {
- Program prog = new Program();
- //read inputs
- string[] values = prog.readInput();
- if(string.IsNullOrEmpty(values[0]))
- {
- Console.WriteLine("ID10T!");
- return;
- }
- //create an array of string elements
- Helper_2D_Elements elements = new Helper_2D_Elements(values);
- //create the Spiral Array class
- Spiral_Array spiral = new Spiral_Array(elements.Elements);
- //Create a printer
- Spiral_Printer printer = new Spiral_Printer(spiral);
- printer.Print();
- }
- string[] readInput()
- {
- string str = Console.ReadLine();
- if(string.IsNullOrEmpty(str) || string.IsNullOrWhiteSpace(str) ) {
- Console.WriteLine("Invalid input Numbnuts");
- return null;
- }
- string[] arrCount = str.Split(' ');
- int arrSize = arrCount.Length;
- string[] StrList = new string[arrSize];
- StrList[0] = str;
- for(int i = 1; i < arrSize; i++)
- {
- str = Console.ReadLine();
- StrList[i] = str;
- }
- return StrList;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment