迪杰斯特拉算法 计算两点之间最短距离

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

//迪杰斯特拉算法 计算两点最短距离
namespace Dijkstra
{
    class Program
    {
        /// 
        /// 邻接矩阵
        /// 
        private static int?[,] rectMatrix;

        /// 
        /// 点 的距离
        /// 
        private static int[] distanse;

        /// 
        /// 访问过的点
        /// 
        private static int[] visitPoints;

        /// 
        /// 路径
        /// 
        private static string[] path;

        /// 
        /// 坐标点数量
        /// 
        private static int pointNum = 10;

        /// 
        /// 起始点
        /// 
        private static int startPoint;

        /// 
        /// 终点
        /// 
        private static int endPoint;

        static void Main(string[] args)
        {
            visitPoints = new int[pointNum];
            distanse = new int[pointNum];
            path = new string[pointNum];
            initRectMatrix();
            Console.WriteLine("请输入起始点坐标:");
            string strPoint = Console.ReadLine();
            Console.WriteLine("请输入终点坐标:");
            string ePoint = Console.ReadLine();
            if (Int32.TryParse(strPoint, out startPoint) && Int32.TryParse(ePoint, out endPoint))
            {
                computePath(startPoint);
                Console.WriteLine("最佳路径:" + path[endPoint]);
                Console.WriteLine("最短距离:" + distanse[endPoint]);
                Console.ReadLine();
            }
        }

        /// 
        /// 计算路径
        /// 
        /// 
        private static void computePath(int index)
        {
            //设置为1,标记当前点已经遍历
            visitPoints[index] = 1;
            if (isCurIndex(index))
            {
                int curDistanse;
                for (int i = 0; i < pointNum; i++)
                {
                    if (i != index && rectMatrix[index, i] != null)
                    {
                        curDistanse = distanse[index] == 0 ? 0 : (int)distanse[index];
                        if (curDistanse + rectMatrix[index, i] < distanse[i] || distanse[i] == 0)
                        {
                            distanse[i] = curDistanse + (int)rectMatrix[index, i];
                            if (index == startPoint)
                            {
                                path[i] = startPoint.ToString() + "=>" + i.ToString();
                            }
                            else
                            {
                                path[i] = path[index].ToString() + "=>" + i.ToString();
                                computePath(i);
                            }
                        }
                    }
                }
            }

            int visit = getVisitIndex();
            if (visit != -1)
            {
                computePath(visit);
            }
        }

        /// 
        /// 得到未访问节点的最小索引
        /// 
        /// 
        private static int getVisitIndex()
        {
            int index=-1;
            for(int i=0;i
        /// 判断当前节点是否与上一节点相连
        /// 
        /// 
        /// 
        private static bool isCurIndex(int index)
        {
            if (index == startPoint ||(path[index] != null 
                && path[index].IndexOf(index.ToString()) != -1))
            {
                return true;
            }
            return false;
        }
        /// 
        /// 初始化邻接矩阵
        /// 
        private static void initRectMatrix()
        {
            rectMatrix = new int?[pointNum, pointNum];

            rectMatrix[0, 1] = 15;
            rectMatrix[0, 2] = 20;
            rectMatrix[1, 3] = 30;
            rectMatrix[1, 2] = 30;
            rectMatrix[1, 4] = 20;
            rectMatrix[1, 5] = 30;
            rectMatrix[2, 3] = 30;
            rectMatrix[2, 4] = 20;
            rectMatrix[2, 6] = 30;
            rectMatrix[3, 5] = 30;
            rectMatrix[4, 7] = 20;
            rectMatrix[4, 8] = 30;
            rectMatrix[5, 6] = 30;
            rectMatrix[6, 9] = 20;
            rectMatrix[7, 8] = 30;
            rectMatrix[7, 9] = 30;
            rectMatrix[8, 4] = 20;
            rectMatrix[8, 0] = 30;
            rectMatrix[8, 3] = 30;
            rectMatrix[9, 2] = 20;
            rectMatrix[9, 7] = 30;
            rectMatrix[9, 6] = 30;

            for (int i = 0; i < pointNum; i++)
            {
                rectMatrix[i, i] = 0;
            }
        }
    }
}

你可能感兴趣的:(C#)