using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Collections; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { int [,] a = new int[6,10] { {0,0,0,0,0,1,0,1,0,1}, {1,1,0,1,1,1,0,0,0,1}, {0,0,1,1,0,0,0,1,0,1}, {0,0,0,0,0,1,0,0,0,0}, {1,1,1,2,1,1,1,0,1,0}, {0,0,0,0,0,0,0,0,1,3}, }; MyMaze m = new MyMaze(a, 6, 10); m.DispMaze(); m.FindPath(); m.DispMaze(); m.PrintStack(); } } class MyMaze { private readonly int m_Row = -1; private readonly int m_Col = -1; private int[,] m_RealMaze; private enum m_Signs { Passage, Wall, Entry, Exit, Visited }; private Cell m_EntryCell = null; private Cell m_ExitCell = null; private Stack m_Stack; #region Initialize Maze public MyMaze(int[,] maze, int row, int col) { if (row > 0) { this.m_Row = row + 2; } if (col > 0) { this.m_Col = col + 2; } m_RealMaze = new int[this.m_Row, this.m_Col]; for (int i = 0; i < this.m_Row; i++) { m_RealMaze[i, 0] = (int)m_Signs.Wall; m_RealMaze[i, m_Col - 1] = (int)m_Signs.Wall; } for (int i = 0; i < this.m_Col; i++) { m_RealMaze[0, i] = (int)m_Signs.Wall; m_RealMaze[m_Row - 1, i] = (int)m_Signs.Wall; } for (int i = 1, m = 0; i < this.m_Row - 1; i++, m++) { for (int j = 1, n = 0; j < this.m_Col - 1; j++, n++) { if (maze[m, n] == (int)m_Signs.Entry) { m_RealMaze[i, j] = (int)m_Signs.Entry; m_EntryCell = new Cell(i, j, 'E'); } else if (maze[m, n] == (int)m_Signs.Exit) { m_RealMaze[i, j] = (int)m_Signs.Exit; m_ExitCell = new Cell(i, j, 'X'); } else if (maze[m, n] == (int)m_Signs.Passage) { m_RealMaze[i, j] = (int)m_Signs.Passage; } else { m_RealMaze[i, j] = (int)m_Signs.Wall; } } } m_Stack = new Stack(); } #endregion #region Display Maze public void DispMaze() { for (int i = 0; i < this.m_Row; i++) { for (int j = 0; j < this.m_Col; j++) { Console.Write(m_RealMaze[i, j].ToString()); Console.Write(" "); } Console.WriteLine(); } Console.WriteLine("**********************"); } #endregion #region FindPathProc public void FindPath() { int row, col; if (this.m_EntryCell == null || this.m_ExitCell == null) { Console.WriteLine("Error Maze"); return; } Cell CurrentCell = m_EntryCell; while (!CurrentCell.Equals(this.m_ExitCell)) { bool flag = false; row = CurrentCell.m_x; col = CurrentCell.m_y; if (!CurrentCell.Equals(this.m_EntryCell)) { m_RealMaze[row, col] = (int)m_Signs.Visited; } PushUnivisited(row - 1, col, 'U', ref flag); PushUnivisited(row + 1, col, 'D', ref flag); PushUnivisited(row, col - 1, 'L', ref flag); PushUnivisited(row, col + 1, 'R', ref flag); if (m_Stack.Count == 0) { return; } else { try { if (!flag) { m_Stack.Pop(); } CurrentCell = (Cell)m_Stack.Peek(); if (!CurrentCell.m_IsLoded) { CurrentCell.m_IsLoded = true; } } catch { return; } } } } private void PushUnivisited(int row, int col, char des, ref bool flag) { if (this.m_RealMaze[row, col] == (int)m_Signs.Passage || this.m_RealMaze[row, col] == (int)m_Signs.Exit) { Cell tmp = new Cell(row, col, des); m_Stack.Push(tmp); flag = true; } } #endregion public void PrintStack() { while (m_Stack.Count > 0) { Console.WriteLine(((Cell)(m_Stack.Pop())).ToString()); } } #region ClassCell class Cell { public int m_x, m_y; public char m_destination; public bool m_IsLoded; public Cell(int x, int y, char c) { m_x = x; m_y = y; m_destination = c; m_IsLoded = false; } public override bool Equals(object obj) { if (this.m_x == ((Cell)obj).m_x && this.m_y == ((Cell)obj).m_y) { return true; } else { return false; } } public override int GetHashCode() { return base.GetHashCode(); } public override string ToString() { return "x = " + m_x.ToString() + " y = " + m_y.ToString() + " Des = " + m_destination.ToString(); } } #endregion } }