Exemplo n.º 1
0
        private void Traverse(Cell cell)
        {
            int counter =1;
            do
            {
                counter++;
                if (cell == null || (!cell.CanStep()) || cell.IsVisited)
                {  }
                else if (cell.IsEnd())
                {
                    if (stack.Count + 1 == maze.MaxPathLength)
                    {
                        result.Add(1);
                        Console.WriteLine("{0}", counter);
                        Console.WriteLine("Another path found, total count is {0}.", result.Count);
                    }
                }
                else
                {
                    cell.IsVisited = true;
                    stack.Push(cell);
                }

                cell = stack.Peek();

                if (!cell.RightVisited)
                { cell = cell.TakeRight(); continue; }
                if (!cell.BottomVisited)
                { cell = cell.TakeBottom(); continue; }
                if (!cell.LeftVisited)
                { cell = cell.TakeLeft(); continue; }
                if (!cell.TopVisited)
                { cell = cell.TakeTop(); continue; }

                cell = stack.Pop();
                if (cell != null) cell.ResetVisited();
                if(stack.Count>0)
                    cell = stack.Peek();
            } while (stack.Count > 0);
        }
Exemplo n.º 2
0
 private Cell StepUp(Cell cell)
 {
     if (IsFirstRow(cell)) return null;
     return _cells[cell.Row - 1, cell.Col];
 }
Exemplo n.º 3
0
 private Cell StepLeft(Cell cell)
 {
     if (IsFirstCol(cell)) return null;
     return _cells[cell.Row, cell.Col - 1];
 }
Exemplo n.º 4
0
 private Cell StepRight(Cell cell)
 {
     if (IsLastCol(cell)) return null;
     return _cells[cell.Row, cell.Col + 1];
 }
Exemplo n.º 5
0
 private bool IsLastRow(Cell cell)
 {
     return cell.Row == _rowCnt - 1;
 }
Exemplo n.º 6
0
 private Cell StepDown(Cell cell)
 {
     if (IsLastRow(cell)) return null;
     return _cells[cell.Row + 1, cell.Col];
 }
Exemplo n.º 7
0
 private bool IsLastCol(Cell cell)
 {
     return cell.Col == _colCnt - 1;
 }
Exemplo n.º 8
0
 private bool IsFirstRow(Cell cell)
 {
     return cell.Row == 0;
 }
Exemplo n.º 9
0
 private bool IsFirstCol(Cell cell)
 {
     return cell.Col == 0;
 }
Exemplo n.º 10
0
 public bool IsBorderCell(Cell cell)
 {
     return cell.Row == 0 || cell.Col == 0 || cell.Row == _rowCnt - 1 || cell.Col == _colCnt - 1;
 }
Exemplo n.º 11
0
        public Maze(int rowCount, int colCount, int[,] values)
        {
            _cells = new Cell[rowCount, colCount];
            _rowCnt = rowCount;
            _colCnt = colCount;
            var pits = 0;
            for (int i = 0; i < rowCount; i++)
            {
                for (int j = 0; j < colCount; j++)
                {
                    var cell = _cells[i, j] = new Cell(i, j, values[i, j]);
                    if (cell.IsStart()) _startCell = cell;
                    if (cell.IsEnd()) _endCell = cell;
                    if (!cell.CanStep()) pits++;
                }
            }

            for (int i = 0; i < rowCount; i++)
            {
                for (int j = 0; j < colCount; j++)
                {
                    var cell = _cells[i, j];
                    cell.Left = StepLeft(cell);
                    cell.Right = StepRight(cell);
                    cell.Top = StepUp(cell);
                    cell.Bottom = StepDown(cell);
                }
            }
            _maxPathLength = (_rowCnt * _colCnt) - pits;
        }