//按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); }
//获取节点周围可行动的点 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); }
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); }
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); }
//需要被优化 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); }
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); }
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); }
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); }
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); }
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); }
public override void ShowPath(Action <LinkE> ac) { LinkE current = new LinkE(end, false); while (current.Pre != null) { current = current.Pre; ac(current); } }
public override void ShowPath(Action <LinkE> ac) { LinkE current = new LinkE(start); while (current.Next != null) { current = current.Next; ac(current); } }
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); }
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); }
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); }
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); } } }
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); }
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); }
//顺时针,从上开始 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); }
public DFS(bool[,] map, LinkE s, LinkE e) : base(map, s, e) { }
public void ShowPath(LinkE e) { GameObject go = GameObject.Instantiate(cube_red); go.transform.position = new Vector3(GetX(e.C), 0, GetZ(e.R)); }
public PathFind(bool[,] map, LinkE s, LinkE e) { this.Map = map; this.start = s; this.end = e; }
public abstract void IE_Finding(); //携程寻路 protected abstract LinkE GetNextE(LinkE s); //寻路策略
public AStar_Stack(bool[,] map, LinkE s, LinkE e) : base(map, s, e) { }
public BFS_Queue(bool[,] map, LinkE s, LinkE e) : base(map, s, e) { }