Пример #1
0
    //按F从大到小排列
    protected List <LinkE> GetNextElements(LinkE s)
    {
        List <LinkE> list = new List <LinkE>();

        if (s.R - 1 >= 0 && Map[s.R - 1, s.C])
        {
            list.Add(new LinkE(s.R - 1, s.C));
        }
        if (s.C + 1 <= MapC - 1 && Map[s.R, s.C + 1])
        {
            list.Add(new LinkE(s.R, s.C + 1));
        }
        if (s.R + 1 <= MapR - 1 && Map[s.R + 1, s.C])
        {
            list.Add(new LinkE(s.R + 1, s.C));
        }
        if (s.C - 1 >= 0 && Map[s.R, s.C - 1])
        {
            list.Add(new LinkE(s.R, s.C - 1));
        }

        list.Sort((a, b) => (GetF(b) - GetF(a)));

        return(list);
    }
Пример #2
0
    //获取节点周围可行动的点
    protected List <LinkE> GetNextElements(LinkE s)
    {
        List <LinkE> list = new List <LinkE>();
        LinkE        e;

        if (s.R - 1 >= 0 && Map[s.R - 1, s.C])
        {
            e   = new LinkE(s.R - 1, s.C);
            e.F = GetF(e);
            list.Add(e);
        }
        if (s.C + 1 <= MapC - 1 && Map[s.R, s.C + 1])
        {
            e   = new LinkE(s.R, s.C + 1);
            e.F = GetF(e);
            list.Add(e);
        }
        if (s.R + 1 <= MapR - 1 && Map[s.R + 1, s.C])
        {
            e   = new LinkE(s.R + 1, s.C);
            e.F = GetF(e);
            list.Add(e);
        }
        if (s.C - 1 >= 0 && Map[s.R, s.C - 1])
        {
            e   = new LinkE(s.R, s.C - 1);
            e.F = GetF(e);
            list.Add(e);
        }

        return(list);
    }
Пример #3
0
    private bool Finding(LinkE s, LinkE e)
    {
        bool bo = false;

        MinHeap <LinkE> heap = new MinHeap <LinkE>();

        heap.Add(s);
        LinkE current;

        while (heap.Count > 0)
        {
            current = heap.ExtractMin();
            Debug.Log(current);
            Map[current.R, current.C] = false; //设为已走过

            if (current.EqualTo(end))
            {
                end.Pre = current.Pre;
                return(true);
            }

            List <LinkE> nextElements = GetNextElements(current);
            if (nextElements.Count > 0)
            {
                for (int i = 0; i < nextElements.Count; i++)
                {
                    nextElements[i].Pre = current;
                    heap.Add(nextElements[i]);
                }
            }
        }

        return(bo);
    }
Пример #4
0
    private int GetF(LinkE e)
    {
        int G = 1;//从上一点移动到e的距离
        int H = Math.Abs(e.C - end.C) + Math.Abs(e.R - end.R);

        return(G + H);
    }
Пример #5
0
    //需要被优化
    protected override LinkE GetNextE(LinkE s)
    {
        LinkE e = null;

        if (s.R - 1 >= 0 && Map[s.R - 1, s.C])
        {
            e = new LinkE(s.R - 1, s.C);
        }
        else if (s.C + 1 <= MapC - 1 && Map[s.R, s.C + 1])
        {
            e = new LinkE(s.R, s.C + 1);
        }
        else if (s.R + 1 <= MapR - 1 && Map[s.R + 1, s.C])
        {
            e = new LinkE(s.R + 1, s.C);
        }
        else if (s.C - 1 >= 0 && Map[s.R, s.C - 1])
        {
            e = new LinkE(s.R, s.C - 1);
        }
        if (e != null)
        {
            Map[e.R, e.C] = false; //设为已走过
        }
        return(e);
    }
Пример #6
0
    private void LoadMap()
    {
        Path = Application.streamingAssetsPath + "/" + "Map.txt";
        StreamReader reader = new StreamReader(Path);

        string[] rc = reader.ReadLine().Split(' ');
        column = int.Parse(rc[0]);
        row    = int.Parse(rc[1]);
        Map    = new bool[row, column];

        rc    = reader.ReadLine().Split(' ');
        start = new LinkE(int.Parse(rc[0]), int.Parse(rc[1]));

        rc  = reader.ReadLine().Split(' ');
        end = new LinkE(int.Parse(rc[0]), int.Parse(rc[1]));

        for (int i = 0; i < row; i++)
        {
            string line = reader.ReadLine();
            for (int j = 0; j < column; j++)
            {
                Map[i, j] = line[j] == '0';
            }
        }
        Debug.Log(Map);
    }
Пример #7
0
    public GameObject UpdateFind(LinkE e)
    {
        GameObject go = GameObject.Instantiate(cube_green);

        go.transform.position = new Vector3(GetX(e.C), 0, GetZ(e.R));
        return(go);
    }
Пример #8
0
    private bool Finding(LinkE s, LinkE e)
    {
        bool bo = false;

        Stack <LinkE> stack = new Stack <LinkE>();

        stack.Push(s);
        LinkE current;

        while (stack.Count > 0)
        {
            current = stack.Pop();
            Debug.Log(current);
            Map[current.R, current.C] = false; //设为已走过

            if (current.EqualTo(end))
            {
                end.Pre = current.Pre;
                return(true);
            }

            List <LinkE> nextElements = GetNextElements(current);
            if (nextElements.Count > 0)
            {
                for (int i = 0; i < nextElements.Count; i++)
                {
                    nextElements[i].Pre = current;
                    stack.Push(nextElements[i]);
                }
            }
        }

        return(bo);
    }
Пример #9
0
    private bool Finding(LinkE s, LinkE e)
    {
        bool          bo    = false;
        Queue <LinkE> queue = new Queue <LinkE>();

        queue.Enqueue(s);

        LinkE current;

        while (queue.Count > 0)
        {
            current = queue.Dequeue();
            if (current.EqualTo(end))
            {
                end.Pre = current.Pre;
                return(true);
            }

            LinkE temp;

            while ((temp = GetNextE(current)) != null)
            {
                temp.Pre = current;
                queue.Enqueue(temp);
            }
        }

        return(bo);
    }
Пример #10
0
    private bool Finding(LinkE s, LinkE e)
    {
        bool bo = false;

        linkEStack = new Stack <LinkE>();
        linkEStack.Push(s);
        LinkE current;

        while (linkEStack.Count > 0)
        {
            current = linkEStack.Pop();
            Debug.Log(current);

            if (current.EqualTo(e))
            {
                e.Pre = current.Pre;
                return(true);
            }
            LinkE temp;
            while ((temp = GetNextE(current)) != null)
            {
                temp.Pre = current;
                linkEStack.Push(temp);
            }
        }

        return(bo);
    }
Пример #11
0
    public override void ShowPath(Action <LinkE> ac)
    {
        LinkE current = new LinkE(end, false);

        while (current.Pre != null)
        {
            current = current.Pre;
            ac(current);
        }
    }
Пример #12
0
    public override void ShowPath(Action <LinkE> ac)
    {
        LinkE current = new LinkE(start);

        while (current.Next != null)
        {
            current = current.Next;
            ac(current);
        }
    }
Пример #13
0
    private IEnumerator IE_finding(LinkE s, LinkE e)
    {
        bool              bo         = false;
        GameObject        gameobject = new GameObject();
        List <GameObject> gos        = new List <GameObject>();
        MinHeap <LinkE>   heap       = new MinHeap <LinkE>();

        heap.Add(s);
        LinkE current;

        while (heap.Count > 0)
        {
            yield return(new WaitForSeconds(FindDeltT));

            current = heap.ExtractMin();
            Debug.Log(current);
            gameobject = GameObject.Instantiate(GameControl._Instance.cube_green);
            gameobject.transform.position = new Vector3(GetX(current.C), 0, GetZ(current.R));
            gos.Add(gameobject);
            current.go = gameobject;
            Map[current.R, current.C] = false; //设为已走过

            if (current.EqualTo(end))
            {
                end.Pre = current.Pre;
                //结束
                foreach (var item in gos)
                {
                    GameObject.Destroy(item);
                    yield return(new WaitForEndOfFrame());
                }

                ShowPath(GameControl._Instance.ShowPath);

                yield break;
            }

            List <LinkE> nextElements = GetNextElements(current);
            if (nextElements.Count > 0)
            {
                for (int i = 0; i < nextElements.Count; i++)
                {
                    nextElements[i].Pre = current;
                    heap.Add(nextElements[i]);
                }
            }
        }

        yield return(null);
    }
Пример #14
0
    private IEnumerator IE_finding(LinkE s, LinkE e)
    {
        bool          bo         = false;
        GameObject    gameobject = new GameObject();
        Stack <LinkE> stack      = new Stack <LinkE>();

        stack.Push(s);
        LinkE current;

        while (stack.Count > 0)
        {
            yield return(new WaitForSeconds(FindDeltT));

            current = stack.Pop();
            Debug.Log(current);

            gameobject = GameObject.Instantiate(GameControl._Instance.cube_green);
            gameobject.transform.position = new Vector3(GetX(current.C), 0, GetZ(current.R));
            current.go = gameobject;
            Map[current.R, current.C] = false; //设为已走过

            if (current.EqualTo(end))
            {
                end.Pre = current.Pre;
                yield break;
            }

            List <LinkE> nextElements = GetNextElements(current);
            if (nextElements.Count > 0)
            {
                for (int i = 0; i < nextElements.Count; i++)
                {
                    nextElements[i].Pre = current;
                    stack.Push(nextElements[i]);
                }
            }
            else
            {
                //删除
                LinkE temp = new LinkE(current, false);
                while (temp.Pre != stack.Peek().Pre)
                {
                    temp = temp.Pre;
                    GameObject.Destroy(temp.go);
                }
            }
        }

        yield return(null);
    }
Пример #15
0
    private IEnumerator IE_finding(LinkE s, LinkE e)
    {
        linkEStack = new Stack <LinkE>();
        linkEStack.Push(s);
        LinkE      current;
        GameObject gameobject = new GameObject();

        while (linkEStack.Count > 0)
        {
            yield return(new WaitForSeconds(FindDeltT));

            current = linkEStack.Pop();
            Debug.Log(current);
            gameobject = GameObject.Instantiate(GameControl._Instance.cube_green);
            gameobject.transform.position = new Vector3(GetX(current.C), 0, GetZ(current.R));
            current.go = gameobject;
            if (current.EqualTo(e))
            {
                e.Pre = current.Pre;
                yield break;
            }
            LinkE temp;

            bool isAdd = false;

            //顺时针压入栈中
            while ((temp = GetNextE(current)) != null)
            {
                temp.Pre = current;
                linkEStack.Push(temp);
                isAdd = true;
            }

            //删除操作
            if (!isAdd)
            {
                temp     = new LinkE();
                temp.Pre = current;
                while (temp.Pre != linkEStack.Peek().Pre)
                {
                    temp = temp.Pre;
                    GameObject.Destroy(temp.go);
                }
            }
        }

        yield return(null);
    }
Пример #16
0
    private IEnumerator IE_finding(LinkE s, LinkE e)
    {
        Queue <LinkE> queue = new Queue <LinkE>();

        queue.Enqueue(s);

        GameObject        gameobject = new GameObject();
        List <GameObject> gos        = new List <GameObject>();

        LinkE current;

        while (queue.Count > 0)
        {
            yield return(new WaitForSeconds(FindDeltT));

            current = queue.Dequeue();

            Debug.Log(current);
            gameobject = GameObject.Instantiate(GameControl._Instance.cube_green);
            gameobject.transform.position = new Vector3(GetX(current.C), 0, GetZ(current.R));
            gos.Add(gameobject);


            if (current.EqualTo(end))
            {
                end.Pre = current.Pre;
                //结束
                foreach (var item in gos)
                {
                    GameObject.Destroy(item);
                    yield return(new WaitForEndOfFrame());
                }

                ShowPath(GameControl._Instance.ShowPath);

                yield break;
            }

            LinkE temp;

            while ((temp = GetNextE(current)) != null)
            {
                temp.Pre = current;
                queue.Enqueue(temp);
            }
        }
    }
Пример #17
0
    private IEnumerator IE_finding(LinkE s, LinkE e)
    {
        if (isFindPass)
        {
            yield break;
        }

        yield return(new WaitForSeconds(FindDeltT));

        LinkE linkE = null;

        Map[s.R, s.C] = false; //设为已走过
        GameObject gameobject = GameObject.Instantiate(GameControl._Instance.cube_green);

        gameobject.transform.position = new Vector3(GetX(s.C), 0, GetZ(s.R));



        if (s.EqualTo(e))
        {
            isFindPass = true;
            yield break;
        }

        while ((linkE = GetNextE(s)) != null)
        {
            yield return(mono.StartCoroutine(IE_finding(linkE, e)));

            if (isFindPass)
            {
                s.Next = linkE;
                yield break;
            }
        }
        GameObject.Destroy(gameobject);

        yield return(null);
    }
Пример #18
0
    private bool FindingPath(LinkE s, LinkE e)
    {
        LinkE linkE = null;

        Map[s.R, s.C] = false; //设为已走过


        if (s.EqualTo(e))
        {
            return(true);
        }

        while ((linkE = GetNextE(s)) != null)
        {
            bool bo = FindingPath(linkE, e);
            if (bo)
            {
                s.Next = linkE;
                return(true);
            }
        }

        return(false);
    }
Пример #19
0
    //顺时针,从上开始
    protected override LinkE GetNextE(LinkE s)
    {
        LinkE e = null;

        if (s.R - 1 >= 0 && Map[s.R - 1, s.C])
        {
            e = new LinkE(s.R - 1, s.C);
        }
        else if (s.C + 1 <= MapC - 1 && Map[s.R, s.C + 1])
        {
            e = new LinkE(s.R, s.C + 1);
        }
        else if (s.R + 1 <= MapR - 1 && Map[s.R + 1, s.C])
        {
            e = new LinkE(s.R + 1, s.C);
        }
        else if (s.C - 1 >= 0 && Map[s.R, s.C - 1])
        {
            e = new LinkE(s.R, s.C - 1);
        }


        return(e);
    }
Пример #20
0
 public DFS(bool[,] map, LinkE s, LinkE e) : base(map, s, e)
 {
 }
Пример #21
0
    public void ShowPath(LinkE e)
    {
        GameObject go = GameObject.Instantiate(cube_red);

        go.transform.position = new Vector3(GetX(e.C), 0, GetZ(e.R));
    }
Пример #22
0
 public PathFind(bool[,] map, LinkE s, LinkE e)
 {
     this.Map   = map;
     this.start = s;
     this.end   = e;
 }
Пример #23
0
 public abstract void IE_Finding();                //携程寻路
 protected abstract LinkE GetNextE(LinkE s);       //寻路策略
Пример #24
0
 public AStar_Stack(bool[,] map, LinkE s, LinkE e) : base(map, s, e)
 {
 }
Пример #25
0
 public BFS_Queue(bool[,] map, LinkE s, LinkE e) : base(map, s, e)
 {
 }