internal AStarShortestPath(char[,] _Matrix, int _h, int _w) { this.Matrix = _Matrix; this.w = _w; this.h = _h; for (int i = 0; i < this.h; i++) { for (int j = 0; j < this.w; j++) { if (this.Matrix[i, j] == 'e') { e._x = j; e._y = i; } else if (this.Matrix[i, j] == 's') { s._x = j; s._y = i; } } } currentNode = s; }
private int calc_G(asNode node)//水品代价计算函数 { if (node._x != currentNode._x || node._y != currentNode._y) { return(currentNode.G + evalCost_X); } else { return(currentNode.G + evalCost_H); } }
internal List <asNode> Anilysize() { List <asNode> rtn_Path = new List <asNode>(); s.H = cacu_H(s); e.H = cacu_H(e); while (currentNode.H != 0) { loadChildNodes(currentNode); if (childList.Count > 0) { foreach (asNode n in childList) { openList.Add(n); } } if (openList.Count > 0) { //openList.Sort(new NodeComparer()); closeList.Add(openList[0]); asNode t = openList[0]; Matrix[t._y, t._x] = 'r'; openList.RemoveAt(0); } else { return(null); } loadChildNodes(closeList[closeList.Count - 1]); if (childList.Count == 0 && !IsEnd(closeList[closeList.Count - 1])) { closeList.RemoveAt(closeList.Count - 1); } if (closeList.Count != 0) { currentNode = closeList[closeList.Count - 1]; } else { currentNode = s; } } asNode end_tag = closeList[closeList.Count - 1].Parent; while (end_tag.Parent != s) { rtn_Path.Add(end_tag); end_tag = end_tag.Parent; } rtn_Path.Add(end_tag); return(rtn_Path); }
private bool IsEnd(asNode n) { //。。。。。。 return(false); }
private void loadChildNodes(asNode p) { childList.Clear(); for (int i = -1; i < 2; i++) { for (int j = -1; j < 2; j++) { if (!(i == 0 && j == 0)) { if (Matrix[i + p._y, j + p._x] != 'o' && Matrix[i + p._y, j + p._x] != 'r' && Matrix[i + p._y, j + p._x] != 's') { asNode n = new asNode(); n._x = p._x + j; n._y = p._y + i; n.G = calc_G(n); n.H = cacu_H(n); n.Parent = currentNode; childList.Add(n); } } } } if (Matrix[p._y, p._x - 1] == 'o') { delfromChildList(p._x - 1, p._y - 1); delfromChildList(p._x - 1, p._y + 1); } if (Matrix[p._y - 1, p._x] == 'o') { delfromChildList(p._x - 1, p._y - 1); delfromChildList(p._x + 1, p._y - 1); } if (Matrix[p._y, p._x + 1] == 'o') { delfromChildList(p._x + 1, p._y + 1); delfromChildList(p._x + 1, p._y - 1); } if (Matrix[p._y + 1, p._x] == 'o') { delfromChildList(p._x - 1, p._y + 1); delfromChildList(p._x + 1, p._y + 1); } foreach (asNode n in openList) { for (int i = 0; i < childList.Count; i++) { if (n._x == childList[i]._x && n._y == childList[i]._y) { childList.RemoveAt(i); break; } } } }
private int cacu_H(asNode node) //曼哈顿法估算函数(斜角代价计算函数) { return(10 * (Math.Abs(node._x - e._x) + Math.Abs(node._y - e._y))); // ; }