题解 | #迷宫问题#递归解题
迷宫问题
https://www.nowcoder.com/practice/cf24906056f4488c9ddb132f317e03bc
using System; using System.Linq; using System.Collections.Generic; public class Program { static bool isRes = false; static List<int[]> points; static bool[,] mazes; public static void Main() { string str = System.Console.ReadLine(); string[] strs = str.Split(' '); int row = int.Parse(strs[0]); int col = int.Parse(strs[1]); Point poin = new Point(0, 0); string[] resul = new string[row * col]; mazes = new bool[row, col]; points = new List<int[]>(); int[,] maze = new int[row, col]; for (int i = 0; i < row; i++) { string str1 = System.Console.ReadLine(); string[] strs1 = str1.Split(' '); for (int j = 0; j < col; j++) { if (j > strs1.Length) { break; } maze[i, j] = int.Parse(strs1[j]); mazes[i, j] = true; } } bool[] bools = new bool[4]; bools[0] = true; int index = 0; int[] list = new int[2] {0, 0}; isRes = getPoit(maze, list, bools); points.Add(list); for (int i = points.Count - 1; i >= 0; i--) { Console.WriteLine("(" + points[i][0] + "," + points[i][1] + ")"); } System.Console.ReadLine(); } private static bool getPoit(int[,] maze, int[] point, bool[] runCru) { int row = maze.GetLength(0); int col = maze.GetLength(1); if (point[0] == (row - 1) && point[1] == (col - 1)) { return true; } if (!runCru[0]) { runCru[0] = true; int[] point1 = new int[2]; Array.Copy(point, point1, 2); if ((point1[1] - 1) >= 0) { if (maze[point1[0], point1[1] - 1] == 0) { point1[1] = point1[1] - 1; bool[] bools = new bool[4]; bools[1] = true; if (!isRes && mazes[point1[0], point1[1]]) { mazes[point1[0], point1[1]] = false; if (!isRes && getPoit(maze, point1, bools)) { points.Add(point1); return true; } else { mazes[point1[0], point1[1]] = false; } } } else { mazes[point1[0], point1[1] - 1] = false; } } } if (!runCru[1]) { runCru[1] = true; int[] point1 = new int[2]; Array.Copy(point, point1, 2); if ((point[1] + 1) < (col)) { if (maze[point1[0], point1[1] + 1] == 0 && mazes[point1[0], point1[1] + 1]) { point1[1] = point1[1] + 1; bool[] bools = new bool[4]; bools[0] = true; if (mazes[point1[0], point1[1]]) { mazes[point1[0], point1[1]] = false; if (!isRes && getPoit(maze, point1, bools)) { points.Add(point1); return true; } } } else { mazes[point1[0], point1[1] + 1] = false; } } } if (!runCru[2]) { runCru[2] = true; int[] point1 = new int[2]; Array.Copy(point, point1, 2); if ((point[0] - 1) >= (0) && mazes[point1[0] - 1, point1[1]]) { if (maze[point1[0] - 1, point1[1]] == 0 ) { point1[0] = point1[0] - 1; bool[] bools = new bool[4]; bools[3] = true; if (mazes[point1[0], point1[1]]) { mazes[point1[0], point1[1]] = false; if (!isRes && getPoit(maze, point1, bools)) { points.Add(point1); return true; } } } else { mazes[point1[0] - 1, point1[1]] = false; } } } if (!runCru[3]) { runCru[3] = true; int[] point1 = new int[2]; Array.Copy(point,point1,2); if ((point[0] + 1) < (row)) { if (maze[point1[0] + 1, point[1]] == 0 && mazes[point1[0] + 1, point1[1]]) { point1[0] = point1[0] + 1; bool[] bools = new bool[4]; bools[2] = true; if (mazes[point1[0], point1[1]]) { mazes[point1[0], point1[1]] = false; if (!isRes && getPoit(maze, point1, bools)) { points.Add(point1); return true; } } } else { mazes[point1[0] + 1, point1[1]] = false; } } } return false; } }