码迷,mamicode.com
首页 > 其他好文 > 详细

寻找两点间的最短距离

时间:2015-12-14 14:21:51      阅读:247      评论:0      收藏:0      [点我收藏+]

标签:

 

由于项目的需要,需要作出计算两点间的最短行走路径的算法  来来回回弄了几天  总算做出来了

 

参考:http://www.cnblogs.com/hanchan/archive/2009/09/23/1572509.html

 

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class DijkstraDouble : MonoBehaviour {

    // Use this for initialization
    void Start () {
        Graph graph = new Graph();
        graph.Init();    
 
        MyLinesDouble gr = new MyLinesDouble ();
        gr.InitLine (graph.NodeList, "A", "B");

        List<List<Node>> lines = gr.lines;
        Debug.Log (lines.Count);

        foreach (List<Node>  nodes in  lines) {
            gr.LogLine( nodes ) ;
            Debug.Log( gr.getLineDistance( nodes )) ;
        }

        
        Debug.Log( gr.getShortDistance( lines )) ;
        // 列出 说有的长度
        // 得出最短的距离


    }




}


public class MyLinesDouble
{
    /// <summary>
    /// 所有路线
    /// </summary>
    public List<List<Node>> lines  ;
    
    /// <summary>
    /// 用分裂的方法穷举出所有的路线
    /// </summary>
    /// <param name="nodes">Nodes.</param>
    /// <param name="startID">Start I.</param>
    /// <param name="endID">End I.</param>
    public void  InitLine(  List<Node>  nodes  , string  startID ,  string  endID )
    {
        lines = new List<List<Node>> ();
        
        string rootStrNode = startID;
        
        Node  rootNode = getNodeByStr( nodes ,  rootStrNode ) ;
        
        
        List<Node> motherLine = new List<Node> ();
        
        motherLine.Add (rootNode);
        
        foreach ( Edge  subEdge  in rootNode.EdgeList) {
            
            //分裂
            List<Node>  nMotherLine = new List<Node>( motherLine.ToArray());
            
            
            getSubNode(  nMotherLine ,   nodes   , subEdge.EndNodeID , endID ) ;
        }
        
    } 
    
    
    /// <summary>
    /// 看看这个节点是不是终点  如果是  就结束
    /// 看看这个节点是不是路过  如果是  姐结束
    /// 遍历该节点的所有子节点  递循环
    /// </summary>
    /// <param name="motherLine">Mother line.</param>
    /// <param name="nodes">Nodes.</param>
    /// <param name="fNode">F node.</param>
    /// <param name="endID">End I.</param>
    void getSubNode(   List<Node> motherLine , List<Node>  nodes   , string  fNode , string  endID)
    {
        //  arrive
        //  get  sub
        //  no way
        
        
        if (fNode == endID) { // 到达终点
            
            Node nNode = getNodeByStr (nodes, fNode);
            
            motherLine.Add (nNode); 
            
            lines.Add (motherLine);
            
            return ;
        }
        
        // 是不是已经路过
        if (isNotExitLine (motherLine, fNode)) { 
            Node nNode = getNodeByStr (nodes, fNode);
            
            motherLine.Add (nNode); 
            
            foreach ( Edge  subEdge  in nNode.EdgeList) {
                
                List<Node>  nMotherLine = new List<Node>( motherLine.ToArray());
                
                getSubNode(  nMotherLine  ,   nodes   , subEdge.EndNodeID , endID ) ;
            }
            
        } 
        
        
    }
    
    /// <summary>
    ///  看看是不是之前已经路过这个节点
    /// </summary>
    /// <returns><c>true</c>, if not exit line was ised, <c>false</c> otherwise.</returns>
    /// <param name="subNode">Sub node.</param>
    /// <param name="fNode">F node.</param>
    bool  isNotExitLine( List<Node> subNode ,  string  fNode)
    {
        bool isNotExit = true;
        foreach (Node  node  in  subNode) {
            if( node.ID == fNode)
                isNotExit = false ;
        }
        
        return isNotExit;
    }
    
    
    
    /// <summary>
    /// g根据ID取得对象node
    /// </summary>
    /// <returns>The node by string.</returns>
    /// <param name="nodes">Nodes.</param>
    /// <param name="nodeID">Node I.</param>
    Node  getNodeByStr( List<Node>  nodes ,  string  nodeID)
    {
        
        foreach (Node node  in  nodes) {
            if( node.ID == nodeID )
            {
                return node ;
                
            }
        }
        
        return null;
    }



    
    /// <summary>
    /// Gets the short distance.  取得所有路线中最短的距离
    /// </summary>
    /// <returns>The short distance.</returns>
    /// <param name="lines">Lines.</param>
    public int  getShortDistance( List< List<Node> >   lines )
    {
        int index = 0;
        double distance = getLineDistance( lines[0]) ;
        
        for (int i =1; i < lines.Count; i ++) {
            List<Node>  nodes = lines[i] ;
            
            if( getLineDistance( nodes) < distance )
            {
                index = i ;
                distance = getLineDistance( nodes ) ;
            }
            
        }
        
        return index;
    }
    
    
    /// <summary>
    /// Gets the line distance.   取得所有节点的weigh总和
    /// </summary>
    /// <returns>The line distance.</returns>
    /// <param name="nodes">Nodes.</param>
    public double  getLineDistance(  List<Node>  nodes )
    {
        double dis = 0;
        
        
        
        for (int i = 1; i < nodes.Count; i++) {
            string start = nodes [i-1].ID;
            dis += getNodeDistance( nodes[i] , start ) ;
        }
        
        return dis;
    }
    
    
    
    /// <summary>
    ///  取得两个节点的距离
    /// </summary>
    /// <returns>The node distance.</returns>
    /// <param name="node">Node.</param>
    /// <param name="ID">I.</param>
    double  getNodeDistance(  Node  node  ,  string  ID )
    {
        foreach (Edge  edge  in  node.EdgeList) {
            if(  edge.EndNodeID == ID )
            {
                return edge.Weight ;
            }
        }
        
        return 0;
    }
    
    
    /// <summary>
    /// Logs the line.    ---> ->A->B->E->D
    /// </summary>
    /// <param name="nodes">Nodes.</param>
    public void  LogLine(  List<Node>  nodes )
    {
        string str = "";
        for (int i = 0; i < nodes.Count; i++) {
            str += "->" + nodes[i].ID;
        }
        
        Debug.Log (str);
    }
     

}




public class Edge
{
    public string StartNodeID;
    public string EndNodeID;
    public double Weight; //权值,代价        
}


public class Node
{
    private string iD ;
    private List<Edge> edgeList ;//Edge的集合--出边表
    
    public Node(string id )
    {
        this.iD = id ;
        this.edgeList = new  List<Edge>() ;
    }
    
    #region property
    
    public string ID
    {
        get
        {
            return this.iD ;
        }
    }
    
    public List<Edge>  EdgeList
    {
        get
        {
            return this.edgeList ;
        }
    }
    #endregion
}



public class Graph
{
    public List<Node> m_nodeList = null;
    public Graph()
    {
        m_nodeList = new List<Node>();
    }
    
    /// <summary>
    /// 获取图的节点集合
    /// </summary>
    public List<Node> NodeList
    {
        get { return this.m_nodeList; }
    }
    
    /// <summary>
    /// 初始化拓扑图
    /// </summary>
    public void Init()
    {
        //***************** B Node *******************
        Node aNode = new Node("A");
        m_nodeList.Add(aNode);
        //A -> B
        Edge aEdge1 = new Edge();
        aEdge1.StartNodeID = aNode.ID;
        aEdge1.EndNodeID = "B";
        aEdge1.Weight = 10;
        aNode.EdgeList.Add(aEdge1);
        //A -> C
        Edge aEdge2 = new Edge();
        aEdge2.StartNodeID = aNode.ID;
        aEdge2.EndNodeID = "C";
        aEdge2.Weight = 20;
        aNode.EdgeList.Add(aEdge2);
        //A -> E
        Edge aEdge3 = new Edge();
        aEdge3.StartNodeID = aNode.ID;
        aEdge3.EndNodeID = "E";
        aEdge3.Weight = 30;
        aNode.EdgeList.Add(aEdge3);
        
        //***************** B Node *******************
        Node bNode = new Node("B");
        m_nodeList.Add(bNode);
        //B -> C
        Edge bEdge1 = new Edge();
        bEdge1.StartNodeID = bNode.ID;
        bEdge1.EndNodeID = "C";
        bEdge1.Weight = 5;
        bNode.EdgeList.Add(bEdge1);
        //B -> E
        Edge bEdge2 = new Edge();
        bEdge2.StartNodeID = bNode.ID;
        bEdge2.EndNodeID = "E";
        bEdge2.Weight = 10;
        bNode.EdgeList.Add(bEdge2);

        
        //B -> A
        Edge bEdge3 = new Edge();
        bEdge3.StartNodeID = bNode.ID;
        bEdge3.EndNodeID = "A";
        bEdge3.Weight = 10;
        bNode.EdgeList.Add(bEdge3);
        
        //***************** C Node *******************
        Node cNode = new Node("C");
        m_nodeList.Add(cNode);
        //C -> D
        Edge cEdge1 = new Edge();
        cEdge1.StartNodeID = cNode.ID;
        cEdge1.EndNodeID = "D";
        cEdge1.Weight = 30;
        cNode.EdgeList.Add(cEdge1);

        
        //C -> A
        Edge cEdge2 = new Edge();
        cEdge2.StartNodeID = cNode.ID;
        cEdge2.EndNodeID = "A";
        cEdge2.Weight = 20;
        cNode.EdgeList.Add(cEdge2);

        //C -> B
        Edge cEdge3 = new Edge();
        cEdge3.StartNodeID = cNode.ID;
        cEdge3.EndNodeID = "B";
        cEdge3.Weight = 5;
        cNode.EdgeList.Add(cEdge3);

        
        //***************** D Node *******************
        Node dNode = new Node("D");
        m_nodeList.Add(dNode);

        //D -> C
        Edge dEdge1 = new Edge();
        dEdge1.StartNodeID = dNode.ID;
        dEdge1.EndNodeID = "C";
        dEdge1.Weight = 30;
        dNode.EdgeList.Add(dEdge1);


        //D -> E
        Edge dEdge2 = new Edge();
        dEdge2.StartNodeID = dNode.ID;
        dEdge2.EndNodeID = "E";
        dEdge2.Weight = 20;
        dNode.EdgeList.Add(dEdge2);
        
        //***************** E Node *******************
        Node eNode = new Node("E");
        m_nodeList.Add(eNode);
        //E -> D
        Edge eEdge1 = new Edge();
        eEdge1.StartNodeID = eNode.ID;
        eEdge1.EndNodeID = "D";
        eEdge1.Weight = 20;
        eNode.EdgeList.Add(eEdge1);

        
        //E -> A
        Edge eEdge2 = new Edge();
        eEdge2.StartNodeID = eNode.ID;
        eEdge2.EndNodeID = "A";
        eEdge2.Weight = 30;
        eNode.EdgeList.Add(eEdge2);

        //E -> B
        Edge eEdge3 = new Edge();
        eEdge3.StartNodeID = eNode.ID;
        eEdge3.EndNodeID = "B";
        eEdge3.Weight = 10;
        eNode.EdgeList.Add(eEdge3);
    }
}

 

寻找两点间的最短距离

标签:

原文地址:http://www.cnblogs.com/didiaodexi/p/5044850.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!