简单测试广度遍历获取最短路径貌似时间复杂度挺高消耗挺大

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TestBianli : MonoBehaviour
{

/*
private void Start()
{
    List<Transform> transforms = new List<Transform>();
    List<Transform> all = new List<Transform>();
    transforms.Add(transform);
    Width(transforms, all);
    for (int i = 0; i < all.Count; i++)
    {
        Debug.Log(all[i].name);
    }
}
*/

struct Road
{
    private List<Point> points;

    private int length;

    public Road(List<Point> points)
    {
        this.points = points;

        length = 0;

        if (points == null)
            return;

        for (int i = 0; i < points.Count - 1; i++)
        {
            length += points[i].connectPoint[points[i + 1]];
        }
    }

    public void ShowRoad()
    {
        string str = "";
        for (int i = 0; i < points.Count; i++)
        {
            str += points[i].name + " ";
        }
        Debug.Log(str);
    }

    public int Length
    {
        get
        {
            return length;
        }
    }

    public void AddPoint(Point point,int length)
    {
        points.Add(point);
        this.length += length;
    }

    public List<Point> GetPoint()
    {
        List<Point> list = new List<Point>();
        list.AddRange(points);
        return list;
    }

    public static bool operator >(Road a,Road b)
    {
        if (a.length > b.length)
        {
            return true;
        }
        return false;
    }

    public static bool operator <(Road a, Road b)
    {
        if (a.length < b.length)
        {
            return true;
        }
        return false;
    }

}

private void Width(List<Transform> trans,List<Transform> all)
{
    if (trans.Count == 0)
        return;
    List< Transform> child = new List<Transform>();
    for (int i = 0; i < trans.Count; i++)
    {
        for (int j = 0; j < trans[i].childCount; j++)
        {
            child.Add(trans[i].GetChild(j));
        }
    }
    all.AddRange(child);
    Width(child,all);
}

private void Start()
{
    Point A = new Point("A");
    Point B = new Point("B");
    Point C = new Point("C");
    Point D = new Point("D");
    Point E = new Point("E");
    Point F = new Point("F");
    Point G = new Point("G");
    Point H = new Point("H");

    A.AddPoint(B, 1);
    A.AddPoint(C, 1);
    B.AddPoint(D, 1);
    C.AddPoint(E, 1);
    C.AddPoint(H, 1);
    D.AddPoint(E, 1);
    E.AddPoint(F, 1);
    E.AddPoint(G, 1);
    F.AddPoint(D, 1);
    G.AddPoint(H, 1);
    G.AddPoint(F, 1);
    H.AddPoint(A, 1);

    List<Point> points = new List<Point>() { A };
    List<Point> all = new List<Point>() { A };

    PointFind(points,all);

    for (int i = 0; i < all.Count; i++)
    {
        Debug.Log(all[i].name);
    }

    print("...........");

    List<List<Point>> road = new List<List<Point>> { new List<Point> { A } };

    PointFind(road, F);

    for (int i = 0; i < myRoads.Count; i++)
    {
        Debug.Log(myRoads[i].name);
    }

    print(" 加 ...........权 ");

    myRoads.Clear();

    myRoadLength = 0;

    List<Road> r = new List<Road>() { new Road(new List<Point>() { A }) };

    PointFind(F, r, r.Count);

    for (int i = 0; i < myRoads.Count; i++)
    {
        Debug.Log(myRoads[i].name);
    }

    Debug.Log("权值: " + myRoadLength);
}

/// <summary>
/// 仅广度遍历所有节点
/// </summary>
/// <param name="points"></param>
/// <param name="all"></param>
private void PointFind(List<Point> points, List<Point> all)
{
    if (points.Count == 0)
        return;
    List<Point> child = new List<Point>();
    for (int i = 0; i < points.Count; i++)
    {
        foreach (var item in points[i].connectPoint)
        {
            if (!all.Contains(item.Key))
            {
                child.Add(item.Key);
            }
        }
    }
    if (child.Count == 0)
        return;
    all.AddRange(child);
    PointFind(child, all);
}

List<Point> myRoads = new List<Point>();

int myRoadLength = 0;

/// <summary>
/// 权值相同时的找路算法
/// </summary>
/// <param name="list"></param>
/// <param name="end"></param>
private void PointFind(List<List<Point>> list,Point end)
{
    if (list.Count == 0)
        return;
    if (myRoads.Count > 0)
        return;
    Debug.Log(" find  .... ");
    //路线
    List<List<Point>> road = new List<List<Point>>();
    //遍历当前的路线
    for (int i = 0; i < list.Count; i++)
    {
        //路线的下一层级
        List<Point> need = new List<Point>();

        string str = "";

        for (int k = 0; k < list[i].Count; k++)
        {
            str += list[i][k].name + " ";
        }

        Debug.Log(str);

        //遍历下层
        foreach (var item in list[i][list[i].Count -1].connectPoint)
        {
            if (!list[i].Contains(item.Key))
            {
                need.Add(item.Key);
                Debug.Log("Add need: "+item.Key.name);
            }
        }
        //如果到底没有找到 则这条路线无用
        if (need.Count == 0)
        {
            continue;
        }
        //添加到新路线 包含下级
        for (int j = 0; j < need.Count; j++)
        {
            List<Point> temp = new List<Point>();
            temp.AddRange(list[i]);
            temp.Add(need[j]);
            road.Add(temp);
            if (need[j].Equals(end) && myRoads.Count == 0)
            {
                myRoads = temp;
                return;
            }
         }
    }
    PointFind(road, end);
}

/// <summary>
/// 权值不相同时的找路算法
/// </summary>
/// <param name="list"></param>
/// <param name="end"></param>
private void PointFind(Point end, List<Road> list,int length)
{
    if (list.Count == 0 || length <= 0 || list.Count < length)
        return;
    //路线
    List<Road> road = new List<Road>();
    int tempLength = -1;
    //遍历当前的路线
    for (int i = 0; i < length; i++)
    {
        //当前路线
        List<Point> temp = list[i].GetPoint();
        //尾结点
        Point lastPoint = temp[temp.Count -1];
        //需要的子结点
        List<Point> need = new List<Point>();
        //遍历子结点加入
        foreach (var item in lastPoint.connectPoint)
        {
            if (!temp.Contains(item.Key))
            {
                need.Add(item.Key);
            }
        }
        //如果无新结点则完成
        if (need.Count == 0)
            continue;

        Debug.Log("权值: " + list[i].Length);
        list[i].ShowRoad();

        //计算添加新路线按权值排序
        for (int j = 0; j < need.Count; j++)
        {
            int index = -1;
            Road tempRoad = new Road(list[i].GetPoint());
            tempRoad.AddPoint(need[j],lastPoint.connectPoint[need[j]]);

            Debug.Log("need: " + need[j].name + " count: " + road.Count);

            //没有路线则加入
            if (road.Count == 0)
            {
                road.Add(tempRoad);
                index = 0;
            }
            //有则插入
            else
            {
                //做排序 插入排序
                for (int k = 0; k < road.Count; k++)
                {
                    if (tempRoad < road[k])
                    {
                        index = k;
                        break;
                    }
                }
                //如果没有小的则加到最后
                if (index == -1)
                {
                    road.Add(tempRoad);
                    index = road.Count - 1;
                }
                //有则插入
                else
                {
                    road.Insert(index, tempRoad);
                }
            }
            //如果未最后一个结点 前面忽略了找到之后的长的 为此能进来的只有为零的和小的路径
            if (need[j].Equals(end))
            {
                if (myRoads.Count == 0 || myRoadLength > road[index].Length)
                {
                    myRoads = tempRoad.GetPoint();
                    myRoadLength = tempRoad.Length;
                    tempLength = index;
                }
            }
        }
    }
    if (tempLength == -1)
        tempLength = road.Count;
    PointFind(end, road, tempLength);
}

}

public class Point
{
public Dictionary<Point, int> connectPoint = new Dictionary<Point, int>();

/// <summary>
/// 添加连接点
/// </summary>
/// <param name="point"></param>
/// <param name="dis"></param>
public void AddPoint(Point point,int dis)
{
    if (!connectPoint.ContainsKey(point))
        connectPoint[point] = dis;
    else if (connectPoint[point] > dis)
    {
            connectPoint[point] = dis;
    }
}

/// <summary>
/// 前面一个点
/// </summary>
public Point frontPoint;

public readonly string name;

public Point(string name)
{
    this.name = name;
}

public override bool Equals(object obj)
{
    Point point = obj as Point;
    if (point != null && point.name.Equals(name))
    {
        return true;
    }
    return false;
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值