zoukankan      html  css  js  c++  java
  • A星算法

    没有采用二叉堆算法优化, 学习了几天终于搞除了一个demo, 这个列子如果点击按钮生成的方块大小不正确,可以先设置下预设调成相应的大小

    只能上下左右走

    1

    G(BA9_T(CC$G)N_1W07U0KC

    using UnityEngine;
    using System.Collections;
    using System.Collections.Generic;
    using System;
    
    public class AStar
    {
    
        public static PriorityQueue closedList;
        public static PriorityQueue openList;
    
    
    
        public static List<Node> FindPath(Node start, Node goal)
        {
            closedList = new PriorityQueue();
            openList = new PriorityQueue();                 //初始化OpenList
            openList.Push(start);                           //把开始节点放入OpenList中
    
            start.F = 0;
            start.G = 0;
            start.H = NodeCost(start, goal);
    
            start.SetH(start.H);
            start.SetF(start.F);
            start.SetG(start.G);
    
    
            Node node = null;
            List<Node> neighbours = null;
            Node neighbourNode = null;
            DateTime now = System.DateTime.Now;
    
            while (openList.Length != 0)
            {
                node = openList.First();
    
                //如果是终点,就返回一个列表list过去
                if (node.x == goal.x && node.y == goal.y)
                {
                    Debug.Log("算法所消耗的时间: " + (System.DateTime.Now - now).TotalSeconds);
                    return CalculatePath(node);
                }
    
                neighbours = new List<Node>();
                GridManager.Instance.GetNeighbours(node, neighbours);           //获取周围的格子加入列表中
    
                //遍历周围的方块
                for (int i = 0; i < neighbours.Count; i++)
                {
                    neighbourNode = neighbours[i];
    
                    float newCost = node.G + TraverseCost(node,neighbourNode);
    
                    //相邻的方块如果以前的G值小于 这次计算的G值就忽略该格子
                    if ((closedList.Contains(neighbourNode) || openList.Contains(neighbourNode)) && (neighbourNode.G <= newCost))
                    {
                        continue;
    
                    }else{
                        neighbourNode.G = newCost;
                        neighbourNode.parent = node;
                        neighbourNode.H = NodeCost(neighbourNode, goal);                                //这个格子离终点的估算
                        neighbourNode.F = neighbourNode.G + neighbourNode.H;
                        
    
                        //显示H F G的值
                        neighbourNode.SetH(neighbourNode.H);
                        neighbourNode.SetF(neighbourNode.F);
                        neighbourNode.SetG(neighbourNode.G);
    
                        
                        //如果方块不在开启列表中,就添加到开启列表中
                        if (!openList.Contains(neighbourNode))
                        {
                            openList.Push(neighbourNode);
                        }
                    }
                }
    
                //把寻找的节点放入关闭列表中
                closedList.Push(node);
                openList.Remove(node);
                node.closeed = true;
            }
    
            Debug.Log("算法所消耗的时间: " + (System.DateTime.Now - now).TotalMilliseconds);
    
            if (node.x != goal.x && node.y != goal.y)
            {
                Debug.LogError("没有找到路径");
                return null;
            }
    
            Debug.Log("找到路径了");
            return CalculatePath(node);
        }
    
        /// <summary>
        /// 获取两个节点的距离(不计算障碍物)
        /// </summary>
        /// <param name="a">开始节点</param>
        /// <param name="b">结束节点</param>
        /// <returns>返回x +y 的距离</returns>
        private static int NodeCost(Node a, Node b)
        {
            int x = (int)Mathf.Abs(b.x - a.x);
            int y = (int)Mathf.Abs(b.y - a.y);
            return x + y;
        }
    
        /// <summary>
        /// 获取最终路径
        /// </summary>
        /// <param name="node"></param>
        /// <returns></returns>
        private static List<Node> CalculatePath(Node node)
        {
            List<Node> list = new List<Node>();
    
            while (node != null)
            {
                list.Add(node);
                node = node.parent;
            }
            list.Reverse();
            return list;
        }
    
    
        public static float TraverseCost(Node a,Node b) 
        {
            if (b.xiejia)
            {
                b.xiejia = false; 
                return a.G + 1.4f;
            }
            else 
            {
                return a.G + 1.0f;
            }
        }
    
    }

    下载地址: http://yunpan.cn/ccS42whwzQ3Sy  访问密码 97d9

    如果你感兴趣,你可以把你妹妹介绍给我
  • 相关阅读:
    Luogu 1080 【NOIP2012】国王游戏 (贪心,高精度)
    Luogu 1314 【NOIP2011】聪明的质检员 (二分)
    Luogu 1315 【NOIP2011】观光公交 (贪心)
    Luogu 1312 【NOIP2011】玛雅游戏 (搜索)
    Luogu 1525 【NOIP2010】关押罪犯 (贪心,并查集)
    Luogu 1514 引水入城 (搜索,动态规划)
    UVA 1394 And Then There Was One / Gym 101415A And Then There Was One / UVAlive 3882 And Then There Was One / POJ 3517 And Then There Was One / Aizu 1275 And Then There Was One (动态规划,思维题)
    Luogu 1437 [HNOI2004]敲砖块 (动态规划)
    Luogu 1941 【NOIP2014】飞扬的小鸟 (动态规划)
    HDU 1176 免费馅饼 (动态规划)
  • 原文地址:https://www.cnblogs.com/plateFace/p/4656341.html
Copyright © 2011-2022 走看看