机器寻径引导算法C#(最短路径表)

本文介绍了一种基于深度优先搜索的迷宫寻路算法实现。通过递归方式探索所有可能路径,并利用栈来记录已走过的路径。文章详细展示了如何使用C#语言实现该算法,包括如何判断当前位置是否可通行、如何进行路径回溯等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GrapCity.Competition.CastleRush.Ai;
using GrapCity.Competition.CastleRush.Ai.View;

namespace _D01B6320_82D9_4D54_AFC9_C502657F2D99_
{
    public class AimStartOnPosition
    {
        //全局变量
        int[,] Arr = new int[60, 60];//迷宫算法:最大迷宫为50*50
        Position BeginPos;
        MapView MAP = new MapView();

        bool canplace(int prePosValue, int x, int y)//判断当前点能否走通
        {
            if (x >= 0 && y >= 0 && x < MAP.Map.GetLength(0) && y < MAP.Map.GetLength(1) && Arr[x, y] != -1)//未越界且不是障碍物(-1)
            {
                if (Arr[x, y] == 0) return true;//该点还未走过
                else return (prePosValue + 1) < Arr[x, y];//该点走过,选择更近的路径
            }
            return false;
        }
        public Stack<Position> path = new Stack<Position>();//记录路径   
        void search(Position CurP)
        {
            int x, y, num;
            Position NewCurP;
            num = Arr[CurP.X, CurP.Y];
            x = CurP.X - 1; y = CurP.Y; //(左)
            NewCurP = new Position(x, y);
            {
                if (canplace(num, x, y))
                {
                    Arr[x, y] = num + 1;
                    path.Push(NewCurP);//进栈
                    search(NewCurP); //深度优先搜索
                    path.Pop();//出栈
                }
            }
            x = CurP.X; y = CurP.Y + 1;//(下)
            NewCurP = new Position(x, y);
            {
                if (canplace(num, x, y))
                {
                    Arr[x, y] = num + 1;
                    path.Push(NewCurP);//进栈
                    search(NewCurP);
                    path.Pop();//出栈 
                }
            }
            x = CurP.X + 1; y = CurP.Y;//(右)
            NewCurP = new Position(x, y);
            {
                if (canplace(num, x, y))
                {
                    Arr[x, y] = num + 1;
                    path.Push(NewCurP);//进栈
                    search(NewCurP);
                    path.Pop();//出栈
                }
            }
            x = CurP.X; y = CurP.Y - 1;//(上)
            NewCurP = new Position(x, y);
            {
                if (canplace(num, x, y))
                {
                    Arr[x, y] = num + 1;
                    path.Push(NewCurP);//进栈
                    search(NewCurP);
                    path.Pop();//出栈 
                }
            }
        }


        public int[,] ShortestPath(Position BeginPosition, MapView map)//特殊之处:BeginPosition
        {                                                                                               
            MAP = map;//全局化map变量
            BeginPos = new Position(BeginPosition.X, BeginPosition.Y);//将起始点全局化           
            for (int i = 0; i < map.Map.GetLength(0); i++)////初始化地图数组Arr
            {
                for (int j = 0; j < map.Map.GetLength(1); j++)//
                {
                    if (map.Map[i, j].GetItemType() == ItemType.River
                        || map.Map[i, j].GetItemType() == ItemType.Mine
                        || map.Map[i, j].GetItemType() == ItemType.Caslte)
                    { Arr[i, j] = -1; }//骑士不可走过
                    else //路、骑士
                    { Arr[i, j] = 0; }//骑士可走                 
                }
            }
            Arr[BeginPos.X, BeginPos.Y] = 1;//起点位置为1
            search(BeginPos);//搜索最短路径     
            for (int i = 0; i < map.Map.GetLength(0); i++)////初始化地图数组Arr
            {
                for (int j = 0; j < map.Map.GetLength(1); j++)//
                {
                    if (Arr[i, j] == 0)
                    { Arr[i, j] = 10000; }//对于那些不可达的路(仁保持着原数组值),修改为无穷远10000

                }
            }

            return Arr;
        }    
    }
}

 

转载于:https://www.cnblogs.com/IThaitian/p/3580972.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值