A* find

//
//using System.Collections;
//using System.Collections.Generic;
//using UnityEngine;
////using myth;
//
//
//public class AStarPathFinder {
//
//    private BinaryPriorityQueue openList;
////    private SceneBlockInfo blockInfo;
//    private Squre[,] squres;
//    private const int CAPACITY = 128;
//    private List<Squre> clearList;
//    private int cnt;
//    private const int ITERATE_NUM = 1024;
//    private int destX;
//    private int destY;
//
//    public AStarPathFinder() {
//        openList = new BinaryPriorityQueue(new SqureComparator(),CAPACITY);
//        
//        clearList = new List<Squre>();
//        cnt = 0;
//        destX = 0;
//        destY = 0;
//    }
//
//    public List<Point> findPath(int sx, int sy, int dx, int dy, SceneBlockInfo blockInfo) {
//
//        int x0 = sx ;
//        int y0 = sy ;
//        destX = dx ;
//        destY = dy ;
//
//        if (x0 < 0 || x0 >= blockInfo.width
//                || y0 < 0 || y0 >= blockInfo.height
//                || destX < 0 || destX >= blockInfo.width
//                || destY < 0 || destY >= blockInfo.height
//                || blockInfo.bits.Get(x0 + y0 * blockInfo.width) == true
//                || blockInfo.bits.Get(destX + destY * blockInfo.width) == true) {
//            return null;
//        }
//		squres = new Squre[blockInfo.width,blockInfo.height] ;
//        squres[x0,y0] = new Squre(null, x0, y0, 0, 0, true);
//        clearList.Add(squres[x0,y0]);
//
//        processSqure(x0, y0, blockInfo);
//
//        while (openList.Count>0 && cnt < ITERATE_NUM) {
//            Squre s = (Squre)(openList.Pop());
//            if (s.x == destX && s.y == destY) {                
//                reset();
//                return pack(s, dx, dy);
//            }
//
//            clearList.Add(s);            
//            s.isClosed = true;
//            processSqure(s.x, s.y, blockInfo);
//            cnt++;
//        }
//
//        reset();
//        return null;
//
//    }
//
//    private void reset() {
//        openList.Clear();
//
//        foreach (Squre sqr in clearList) {
//            squres[sqr.x,sqr.y] = null;
//        }
//        clearList.Clear();
//        cnt = 0;
//    }
//
//    private List<Point> pack(Squre s, int dx, int dy) {
//        List<Point> list = new List<Point>();
//		Point p = new Point();
//		p.x = dx;
//		p.y = dy;
//        list.Insert(0,p);
//		
//        if (s.parent == null) {
//            return list;
//        }
//
//        int deltax = s.parent.x - s.x;
//        int deltay = s.parent.y - s.y;
//
//        s = s.parent;
//
//        if (s.parent == null) {
//            return list;
//        }
//
//        while (s.parent.parent != null) {
//
//            if (s.parent.x - s.x == deltax && s.parent.y - s.y == deltay) {
//                s = s.parent;
//                continue;
//            }
//			Point p1 = new Point();
//			p1.x = s.x;
//			p1.y = s.y ;
//            list.Insert(0,p1);
//            deltax = s.parent.x - s.x;
//            deltay = s.parent.y - s.y;
//            s = s.parent;
//        }
//		Point p2 = new Point();
//		p2.x = s.x;
//		p2.y = s.y ;
//        list.Insert(0,p2);
//        return list;
//    }
//
//    private void processSqure(int x0, int y0, SceneBlockInfo blockInfo) {
//        Squre s = squres[x0,y0];
//        int sg = s.G;
//
//        if (x0 > 0) {
//            if (x0 < blockInfo.width - 1) {
//                if (y0 > 0) {
//                    if (y0 < blockInfo.height - 1) {
//                        processOneSqure(x0 - 1, y0 - 1, s, sg + 14, blockInfo);
//                        processOneSqure(x0, y0 - 1, s, sg + 10, blockInfo);
//                        processOneSqure(x0 + 1, y0 - 1, s, sg + 14, blockInfo);
//                        processOneSqure(x0 - 1, y0, s, sg + 10, blockInfo);
//                        processOneSqure(x0 + 1, y0, s, sg + 10, blockInfo);
//                        processOneSqure(x0 - 1, y0 + 1, s, sg + 14, blockInfo);
//                        processOneSqure(x0, y0 + 1, s, sg + 10, blockInfo);
//                        processOneSqure(x0 + 1, y0 + 1, s, sg + 14, blockInfo);
//                    } else {
//                        processOneSqure(x0 - 1, y0 - 1, s, sg + 14, blockInfo);
//                        processOneSqure(x0, y0 - 1, s, sg + 10, blockInfo);
//                        processOneSqure(x0 + 1, y0 - 1, s, sg + 14, blockInfo);
//                        processOneSqure(x0 - 1, y0, s, sg + 10, blockInfo);
//                        processOneSqure(x0 + 1, y0, s, sg + 10, blockInfo);
//                    }
//                } else {
//                    processOneSqure(x0 - 1, y0, s, sg + 10, blockInfo);
//                    processOneSqure(x0 + 1, y0, s, sg + 10, blockInfo);
//                    processOneSqure(x0 - 1, y0 + 1, s, sg + 14, blockInfo);
//                    processOneSqure(x0, y0 + 1, s, sg + 10, blockInfo);
//                    processOneSqure(x0 + 1, y0 + 1, s, sg + 14, blockInfo);
//                }
//            } else {
//                if (y0 > 0) {
//                    if (y0 < blockInfo.height - 1) {
//                        processOneSqure(x0 - 1, y0 - 1, s, sg + 14, blockInfo);
//                        processOneSqure(x0, y0 - 1, s, sg + 10, blockInfo);
//                        processOneSqure(x0 - 1, y0, s, sg + 10, blockInfo);
//                        processOneSqure(x0 - 1, y0 + 1, s, sg + 14, blockInfo);
//                        processOneSqure(x0, y0 + 1, s, sg + 10, blockInfo);
//                    } else {
//                        processOneSqure(x0 - 1, y0 - 1, s, sg + 14, blockInfo);
//                        processOneSqure(x0, y0 - 1, s, sg + 10, blockInfo);
//                        processOneSqure(x0 - 1, y0, s, sg + 10, blockInfo);
//                    }
//                } else {
//                    processOneSqure(x0 - 1, y0, s, sg + 10, blockInfo);
//                    processOneSqure(x0 - 1, y0 + 1, s, sg + 14, blockInfo);
//                    processOneSqure(x0, y0 + 1, s, sg + 10, blockInfo);
//                }
//            }
//        } else {
//            if (y0 > 0) {
//                if (y0 < blockInfo.height - 1) {
//                    processOneSqure(x0, y0 - 1, s, sg + 10, blockInfo);
//                    processOneSqure(x0 + 1, y0 - 1, s, sg + 14, blockInfo);
//                    processOneSqure(x0 + 1, y0, s, sg + 10, blockInfo);
//                    processOneSqure(x0, y0 + 1, s, sg + 10, blockInfo);
//                    processOneSqure(x0 + 1, y0 + 1, s, sg + 14, blockInfo);
//                } else {
//                    processOneSqure(x0, y0 - 1, s, sg + 10, blockInfo);
//                    processOneSqure(x0 + 1, y0 - 1, s, sg + 14, blockInfo);
//                    processOneSqure(x0 + 1, y0, s, sg + 10, blockInfo);
//                }
//            } else {
//                processOneSqure(x0 + 1, y0, s, sg + 10, blockInfo);
//                processOneSqure(x0, y0 + 1, s, sg + 10, blockInfo);
//                processOneSqure(x0 + 1, y0 + 1, s, sg + 14, blockInfo);
//            }
//        }
//    }
//
//    private void processOneSqure(int x, int y, Squre parent, int g, SceneBlockInfo blockInfo) {
//		
//        if (blockInfo.bits.Get(x + y * blockInfo.width) == true) {
//            return;
//        }
//
//        Squre s = squres[x,y];
//
//        if (s != null && s.isClosed) {
//            return;
//        }
//
//        if (s == null) {
//            s = new Squre(parent, x, y, 0, g, false);
//            s.H = ((destX - x > 0 ? destX - x : x - destX) + (destY - y > 0 ? destY - y : y - destY)) * 10; // manhattan
//            squres[x,y] = s;
//            openList.Push(s);
//            clearList.Add(s);
//            return;
//        }
//
//        if (g < s.G) {
//            s.G = g;
//            s.parent = parent;
//        }
//    }
//
//    private class Squre {
//
//        public Squre parent;
//        public int x;
//        public int y;
//        public int H;
//        public int G;
//        public bool isClosed;
//
//        public Squre(Squre parent, int x, int y, int H, int G, bool isClosed) {
//            this.parent = parent;
//            this.x = x;
//            this.y = y;
//            this.H = H;
//            this.G = G;
//            this.isClosed = isClosed;
//        }
//    }
//
//	private class SqureComparator : IComparer  {
//		 int IComparer.Compare(object o1, object o2) {
//			Squre s1 = (Squre)o1;
//			Squre s2 = (Squre)o2;
//            return s1.G + s1.H - s2.G - s2.H;
//        }
//		
//   }
//}
//
//
//#if false
//using System.Collections;
//using System.Collections.Generic;
//using UnityEngine;
//
//public class SceneBlockInfo
//{
//	public int width;
//	public int height;
//	public BitArray bits;
//	
//	public SceneBlockInfo(int width, int height, BitArray bits)
//	{
//		this.width = width ;
//		this.height = height;
//		this.bits = bits;
//	}
//}
//
//public class AStarPathFinder {
//
//    private SortedList<Squre,int> openList;
////    private SceneBlockInfo blockInfo;
//    private Squre[,] squres;
//    private const int CAPACITY = 128;
//    private List<Squre> clearList;
//    private int cnt;
//    private const int ITERATE_NUM = 1024;
//    private int destX;
//    private int destY;
//
//    public AStarPathFinder() {
//        openList = new SortedList<Squre,int>(CAPACITY, new SqureComparator());
//        
//        clearList = new List<Squre>();
//        cnt = 0;
//        destX = 0;
//        destY = 0;
//    }
//
//    public List<Point> findPath(int sx, int sy, int dx, int dy, SceneBlockInfo blockInfo) {
//
//        int x0 = sx ;
//        int y0 = sy ;
//        destX = dx ;
//        destY = dy ;
//
//        if (x0 < 0 || x0 >= blockInfo.width
//                || y0 < 0 || y0 >= blockInfo.height
//                || destX < 0 || destX >= blockInfo.width
//                || destY < 0 || destY >= blockInfo.height
//                || blockInfo.bits.Get(x0 + y0 * blockInfo.width) == true
//                || blockInfo.bits.Get(destX + destY * blockInfo.width) == true) {
//            return null;
//        }
//		squres = new Squre[blockInfo.width,blockInfo.height] ;
//        squres[x0,y0] = new Squre(null, x0, y0, 0, 0, true);
//        clearList.Add(squres[x0,y0]);
//
//        processSqure(x0, y0, blockInfo);
//
//        while (openList.Count>0 && cnt < ITERATE_NUM) {
//            Squre s = openList.Keys[0];
//            if (s.x == destX && s.y == destY) {                
//                reset();
//                return pack(s, dx, dy);
//            }
//
//            clearList.Add(s);            
//            s.isClosed = true;
//            processSqure(s.x, s.y, blockInfo);
//            cnt++;
//        }
//
//        reset();
//        return null;
//
//    }
//
//    private void reset() {
//        openList.Clear();
//
//        foreach (Squre sqr in clearList) {
//            squres[sqr.x,sqr.y] = null;
//        }
//        clearList.Clear();
//        cnt = 0;
//    }
//
//    private List<Point> pack(Squre s, int dx, int dy) {
//        List<Point> list = new List<Point>();
//        list.Insert(0,new Point(dx, dy));
//		
//        if (s.parent == null) {
//            return list;
//        }
//
//        int deltax = s.parent.x - s.x;
//        int deltay = s.parent.y - s.y;
//
//        s = s.parent;
//
//        if (s.parent == null) {
//            return list;
//        }
//
//        while (s.parent.parent != null) {
//
//            if (s.parent.x - s.x == deltax && s.parent.y - s.y == deltay) {
//                s = s.parent;
//                continue;
//            }
//
//            list.Insert(0,new Point(s.x , s.y ));
//            deltax = s.parent.x - s.x;
//            deltay = s.parent.y - s.y;
//            s = s.parent;
//        }
//
//        list.Insert(0,new Point(s.x , s.y ));
//        return list;
//    }
//
//    private void processSqure(int x0, int y0, SceneBlockInfo blockInfo) {
//        Squre s = squres[x0,y0];
//        int sg = s.G;
//
//        if (x0 > 0) {
//            if (x0 < blockInfo.width - 1) {
//                if (y0 > 0) {
//                    if (y0 < blockInfo.height - 1) {
//                        processOneSqure(x0 - 1, y0 - 1, s, sg + 14, blockInfo);
//                        processOneSqure(x0, y0 - 1, s, sg + 10, blockInfo);
//                        processOneSqure(x0 + 1, y0 - 1, s, sg + 14, blockInfo);
//                        processOneSqure(x0 - 1, y0, s, sg + 10, blockInfo);
//                        processOneSqure(x0 + 1, y0, s, sg + 10, blockInfo);
//                        processOneSqure(x0 - 1, y0 + 1, s, sg + 14, blockInfo);
//                        processOneSqure(x0, y0 + 1, s, sg + 10, blockInfo);
//                        processOneSqure(x0 + 1, y0 + 1, s, sg + 14, blockInfo);
//                    } else {
//                        processOneSqure(x0 - 1, y0 - 1, s, sg + 14, blockInfo);
//                        processOneSqure(x0, y0 - 1, s, sg + 10, blockInfo);
//                        processOneSqure(x0 + 1, y0 - 1, s, sg + 14, blockInfo);
//                        processOneSqure(x0 - 1, y0, s, sg + 10, blockInfo);
//                        processOneSqure(x0 + 1, y0, s, sg + 10, blockInfo);
//                    }
//                } else {
//                    processOneSqure(x0 - 1, y0, s, sg + 10, blockInfo);
//                    processOneSqure(x0 + 1, y0, s, sg + 10, blockInfo);
//                    processOneSqure(x0 - 1, y0 + 1, s, sg + 14, blockInfo);
//                    processOneSqure(x0, y0 + 1, s, sg + 10, blockInfo);
//                    processOneSqure(x0 + 1, y0 + 1, s, sg + 14, blockInfo);
//                }
//            } else {
//                if (y0 > 0) {
//                    if (y0 < blockInfo.height - 1) {
//                        processOneSqure(x0 - 1, y0 - 1, s, sg + 14, blockInfo);
//                        processOneSqure(x0, y0 - 1, s, sg + 10, blockInfo);
//                        processOneSqure(x0 - 1, y0, s, sg + 10, blockInfo);
//                        processOneSqure(x0 - 1, y0 + 1, s, sg + 14, blockInfo);
//                        processOneSqure(x0, y0 + 1, s, sg + 10, blockInfo);
//                    } else {
//                        processOneSqure(x0 - 1, y0 - 1, s, sg + 14, blockInfo);
//                        processOneSqure(x0, y0 - 1, s, sg + 10, blockInfo);
//                        processOneSqure(x0 - 1, y0, s, sg + 10, blockInfo);
//                    }
//                } else {
//                    processOneSqure(x0 - 1, y0, s, sg + 10, blockInfo);
//                    processOneSqure(x0 - 1, y0 + 1, s, sg + 14, blockInfo);
//                    processOneSqure(x0, y0 + 1, s, sg + 10, blockInfo);
//                }
//            }
//        } else {
//            if (y0 > 0) {
//                if (y0 < blockInfo.height - 1) {
//                    processOneSqure(x0, y0 - 1, s, sg + 10, blockInfo);
//                    processOneSqure(x0 + 1, y0 - 1, s, sg + 14, blockInfo);
//                    processOneSqure(x0 + 1, y0, s, sg + 10, blockInfo);
//                    processOneSqure(x0, y0 + 1, s, sg + 10, blockInfo);
//                    processOneSqure(x0 + 1, y0 + 1, s, sg + 14, blockInfo);
//                } else {
//                    processOneSqure(x0, y0 - 1, s, sg + 10, blockInfo);
//                    processOneSqure(x0 + 1, y0 - 1, s, sg + 14, blockInfo);
//                    processOneSqure(x0 + 1, y0, s, sg + 10, blockInfo);
//                }
//            } else {
//                processOneSqure(x0 + 1, y0, s, sg + 10, blockInfo);
//                processOneSqure(x0, y0 + 1, s, sg + 10, blockInfo);
//                processOneSqure(x0 + 1, y0 + 1, s, sg + 14, blockInfo);
//            }
//        }
//    }
//
//    private void processOneSqure(int x, int y, Squre parent, int g, SceneBlockInfo blockInfo) {
//        if (blockInfo.bits.Get(x + y * blockInfo.width) == true) {
//            return;
//        }
//
//        Squre s = squres[x,y];
//
//        if (s != null && s.isClosed) {
//            return;
//        }
//
//        if (s == null) {
//            s = new Squre(parent, x, y, 0, g, false);
//            s.H = ((destX - x > 0 ? destX - x : x - destX) + (destY - y > 0 ? destY - y : y - destY)) * 10; // manhattan
//            squres[x,y] = s;
//            openList.Add(s,1);
//            clearList.Add(s);
//            return;
//        }
//
//        if (g < s.G) {
//            s.G = g;
//            s.parent = parent;
//        }
//    }
//
//    public class Squre {
//
//        public Squre parent;
//        public int x;
//        public int y;
//        public int H;
//        public int G;
//        public bool isClosed;
//
//        public Squre(Squre parent, int x, int y, int H, int G, bool isClosed) {
//            this.parent = parent;
//            this.x = x;
//            this.y = y;
//            this.H = H;
//            this.G = G;
//            this.isClosed = isClosed;
//        }
//    }
//
//	private class SqureComparator : IComparer<Squre>  {
//		public int Compare(Squre o1, Squre o2) {
//           /* return o1.G + o1.H - o2.G - o2.H; */
//			//TODO key not equal for sortedlist
//			if(o1.G + o1.H - o2.G - o2.H == 0)
//			{
//				return 1;
//			}
//			return o1.G + o1.H - o2.G - o2.H;
//        }
//
//   }
//}
//#endif

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值