题解 | #迷宫问题#递归解题

迷宫问题

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;

    }

}
全部评论

相关推荐

Southyeung:我说一下我的看法(有冒犯实属抱歉):(1)简历不太美观,给我一种看都不想看的感觉,感觉字体还是排版问题;(2)numpy就一个基础包,机器学习算法是什么鬼?我感觉你把svm那些写上去都要好一点。(2)课程不要写,没人看,换成获奖经历;(3)项目太少了,至少2-3个,是在不行把网上学习的也写上去。
点赞 评论 收藏
分享
05-12 16:04
已编辑
江西财经大学 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务