C#实现迷宫问题——利用栈

给定一个迷宫 求出路径 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 穷举_迷宫问题
{
    
    class Program
    {
       static  int[,] map = new int[5, 5] { { 0, 0, 1, 1, 0 }, //Map二位数组用来储存迷宫  1表示不可通行 0表示可通行
                                     { 0, 1, 1, 0, 0 },
                                     { 0, 1, 1, 0, 1 },
                                     { 0, 0, 0, 0, 1 },
                                     { 1, 1, 1, 0, 0 }};

       static Stack<Position> stack = new Stack<Position>();//构造一个空栈用来存储路径

       static Position end = new Position(4, 4);//目标位置
        static void Main(string[] args)
        {
            Position start = new Position();//起点默认为(0,0)
            
            stack.Push(start);//将起点入栈 
            map[start.x, start.y] = 2;//入栈的路径点的值为2
            Position current=null;//记住当前的栈顶元素
            while (stack != null)
            {
                current = stack.Peek();
                if (ArriveEnd(current))//判断是否到达终点
                {
                    break;
                }

                Position next;
                
                //四种走法
                next = new Position(current.x, current.y + 1);
                if (!OutOfBounds(next) && WeatherCross(next))//当前节点可以通行
                {
                    map[next.x, next.y] = 2;
                    stack.Push(next);
                    continue;
                }

                next = new Position(current.x + 1, current.y);
                if (!OutOfBounds(next) && WeatherCross(next))//当前节点可以通行
                {
                    map[next.x, next.y] = 2;
                    stack.Push(next);
                    continue;
                }

                next = new Position(current.x, current.y – 1);
                if (!OutOfBounds(next) && WeatherCross(next))//当前节点可以通行
                {
                    map[next.x, next.y] = 2;
                    stack.Push(next);
                    continue;
                }

                next = new Position(current.x – 1, current.y);
                if (!OutOfBounds(next) && WeatherCross(next))//当前节点可以通行
                {
                    map[next.x, next.y] = 2;
                    stack.Push(next);
                    continue;
                }

                Position NotCross = stack.Pop();                
            }

            while (stack.Count != 0)
            {
                Position node = stack.Pop();
                Console.WriteLine(node.x + ” ” + node.y);
            }

            Console.ReadKey();
        }

        public static bool ArriveEnd(Position current)
        {
            if (current.x == end.x && current.y == end.y)//是终点 就返回True
            {
                return true;
            }
            return false;
        }

        public static bool OutOfBounds(Position next)
        {
            if (next.x >= 0 && next.x <= 4 && next.y >= 0 && next.y <= 4)
            {
                return false;
            }
            return true;
        }

        public static bool WeatherCross(Position next)
        {
            if (map[next.x, next.y] != 1 && map[next.x, next.y] != 2)
            {
                return true;
            }
            return false;
        }
    }
    

}

有些代码可以再抽成一个方法 这里就不写了

    原文作者:迷宫问题
    原文地址: https://blog.csdn.net/Beyond_Wang/article/details/79704948
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞