zoukankan      html  css  js  c++  java
  • 机器寻径引导算法C#(最短路径表)

    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;
            }    
        }
    }
  • 相关阅读:
    再看数据库——(1)存储过程
    Android nomedia 避免图片等资源泄露在系统图库其中
    南邮JAVA程序设计实验1 综合图形界面程序设计
    【零基础入门学习Python笔记013】元祖:戴上了枷锁的列表
    关于部门后端所有转向java前初步设想
    Windows环境下msysgit安装git flow
    UI_UISlider控件
    名不符实的读写锁
    js+ canvas 实现人物走动
    Android HandlerThread 源代码分析
  • 原文地址:https://www.cnblogs.com/IThaitian/p/3580972.html
Copyright © 2011-2022 走看看