寻路

来源:互联网 发布:定格动画软件app 编辑:程序博客网 时间:2024/05/16 09:22
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
 
namespace ShortPathTest
{
    class GenShortPath
    {
        public int GenTargetPath(int tableWidth, int x1, int y1, int x2, int y2)
        {
            if (tableWidth <= 0) return -1;
 
            int retValue = -1;
            //初始化一个图
            Element[,] map = new Element[tableWidth, tableWidth];
            int[,] distance = new int[tableWidth * tableWidth, tableWidth * tableWidth];
            List<Element> pointsInMap = new List<Element>();
            for (int i = 0; i < tableWidth; i++)
            {
                for (int j = 0; j < tableWidth; j++)
                {
                    Element e = new Element() { posX = i, posY = j };
                    map[i, j] = e;
                    pointsInMap.Add(e);
                }   
            }
 
            for (int i = 0; i < tableWidth * tableWidth; i++)
            {
                for (int j = 0; j < tableWidth * tableWidth; j++)
                            {
                    if (i == j)
                    {
                        distance[i, j] = 0;
                    }
                    else
                    {
                        int posX_1 = pointsInMap.posX;
                        int posY_1 = pointsInMap.posY;
 
                        int posX_2 = pointsInMap[j].posX;
                        int posY_2 = pointsInMap[j].posY;
 
                        //判断两个点的相对位置
                        if (Math.Abs(posX_1 - posX_2) == 1 && Math.Abs(posY_1 - posY_2) == 2)
                        {
                            distance[i, j] = 1;
                        }
                        else if (Math.Abs(posX_1 - posX_2) == 2 && Math.Abs(posY_1 - posY_2) == 1)
                        {
                            distance[i, j] = 1;
                        }
                        else
                        {
                            distance[i, j] = int.MaxValue;
                        }
                    }
                            }
            }
 
            //寻找路点
            int pointStartIndex = tableWidth * x1 + y1;
            Dictionary<int, int> dist = new Dictionary<int,int>();
            Dictionary<int, List<Element>> path = new Dictionary<int, List<Element>>();
            for (int i = 0; i < tableWidth * tableWidth; i++)
            {
                path = new List<Element>();
                dist  = distance[i,pointStartIndex];
                if (dist < int.MaxValue)
                {
                    path.Add(pointsInMap[pointStartIndex]);
                    path.Add(pointsInMap);
                }
            }
 
            List<Element> s = new List<Element>();
            s.Add(pointsInMap[pointStartIndex]);
 
            for (int t = 0; t <= tableWidth *  tableWidth - 1; t++)
            {
                int min = int.MaxValue;
                int k = -1;
                for (int i = 0; i < tableWidth * tableWidth; i++)
                {
                    if (!s.Contains(pointsInMap) && dist < min) {
                        k = i;
                        min = dist;
                    }
                }
 
                if (min == int.MaxValue) break;
 
                s.Add(pointsInMap[k]);
 
                for (int i = 0; i < tableWidth * tableWidth; i++)
                {
                    if (!s.Contains(pointsInMap) && distance[k, i] != int.MaxValue && (dist[k] + distance[k, i]) < dist)
                    {
                        dist = dist[k] + distance[k, i];
                        path = new List<Element>(path[k]);
                        path.Add(pointsInMap);
                    }
                }
            }
            foreach (int k in path.Keys)
            {
                List<Element> curPath = path[k];
                Element ele = curPath[curPath.Count - 1];
 
                if (ele.posX == x2 && ele.posY == y2)
                {
                    string str = "Path to::========>>>" + curPath[curPath.Count - 1].ToString() + "\n";
                    for (int i = 0; i < curPath.Count; i++)
                    {
                        str += curPath.ToString() + "-->";
                    }
                    Console.WriteLine(str);
                }
            }
 
            return retValue;
        }
    }
}
0 0