www.pudn.com > C sharp和MapObjects实现.rar > CPath.cs
using System;
using System.Collections;
namespace MainSystem
{
struct Routine //一条公交线路
{
public short nFlag; //0:双向;1:上行;2:下行
public short nStationNumber;
public string szRoutineName;
public string[] szStaionName;
};
struct Node
{
public short nNodeNumber;
public short[] nRoutineOrder;
public short[] nStationOrder;
};
struct Station
{
public string szStationName;
public short nRoutineNumber;
public short[] pnRoutineID;
public short[] pnOrder;
};
struct PathNode
{
public short nSegNumber;
public string[] szRoutineName;
public string[] szFromStationName;
public string[] szToStationName;
};
///
/// Summary description for CPath.
///
public class CPath
{
private int TIMELIMIT = 3;
private Routine[] _pRoutine;
private Station[] _pStations;
private short _nRCount;
private short _nSCount;
public CPath()
{
_pRoutine = new Routine[1000];
_pStations = new Station[5000];
}
public void Build(CEnvironment env)
{
_nRCount = BuildRoutine(_pRoutine,env);
_nSCount = BuildStationIndex(_pRoutine,_nRCount,_pStations);
}
short BuildRoutine(Routine[] pRoutine, CEnvironment env)
{
System.Data.DataTable typeTbl = env.m_dataSet.Tables["公交车站路线"];
System.Data.DataRow[] rowstypes;
if (env.m_szBusFilter != "")
rowstypes = typeTbl.Select(env.m_szBusFilter);
else
rowstypes = typeTbl.Select();
short nCount = 0;
short nSCount = 0;
string rName;
string sName;
string sTemp1;
string sTemp2;
string[] sNames = new string[350];
foreach (System.Data.DataRow myRow in rowstypes)
{
rName = myRow[0].ToString();
sTemp1 = myRow[1].ToString();
sName = myRow[2].ToString();
sTemp2 = myRow[0].ToString();
if(nCount ==0)
{
pRoutine[nCount] = new Routine();
pRoutine[nCount].szRoutineName=string.Copy (rName);
if(rName.Length > 4 && rName.Substring(rName.Length - 4,4).Equals("上行"))
pRoutine[nCount].nFlag = 1;
else if(rName.Length > 4 && rName.Substring(rName.Length - 4,4).Equals("下行"))
pRoutine[nCount].nFlag = 2;
else
pRoutine[nCount].nFlag = 0;
nCount++;
nSCount = 0;
sNames[nSCount] = string.Copy(sName);
nSCount = 1;
}
else
{
if(pRoutine[nCount-1].szRoutineName.Equals( rName))
{
sNames[nSCount]=string.Copy( sName);
nSCount++;
}
else
{
//结束上一站
pRoutine[nCount-1].nStationNumber = nSCount;
pRoutine[nCount-1].szStaionName = new string [nSCount];
for(short i=0;i 4 && rName.Substring(rName.Length - 4,4).Equals("上行"))
pRoutine[nCount].nFlag = 1;
else if(rName.Length > 4 && rName.Substring(rName.Length - 4,4).Equals("下行"))
pRoutine[nCount].nFlag = 2;
else
pRoutine[nCount].nFlag = 0;
pRoutine[nCount].szRoutineName=string.Copy( rName);
nCount++;
nSCount = 0;
sNames[nSCount]=string.Copy (sName);
nSCount = 1;
}
}
}
pRoutine[nCount-1].nStationNumber = nSCount;
pRoutine[nCount-1].szStaionName = new string [nSCount];
for(short i=0;i= 0)
{
nodeCurrent = new Node();
nodeCurrent.nNodeNumber = 1;
nodeCurrent.nRoutineOrder = new short [10];
nodeCurrent.nStationOrder = new short [10];
nodeCurrent.nRoutineOrder[0] = i;
nodeCurrent.nStationOrder[0] = nStationOrder;
//入队列操作
queueNodes.Enqueue(nodeCurrent);
}
}
while( !(0 == queueNodes.Count) )
{
nodeCurrent = (Node)queueNodes.Dequeue();//.GetFront();
if((nodeCurrent.nNodeNumber>=TIMELIMIT+1) || (nodeCurrent.nNodeNumber>nMinChangeTimes-1) || (queueNodes.Count >1500000)) //换乘次数限制
break;
//得到队列头
//从队头站点序列的最后一个站开始检索是否能够找到到站
nRoutineCurrent = nodeCurrent.nRoutineOrder[nodeCurrent.nNodeNumber-1];
nStationCurrent = nodeCurrent.nStationOrder[nodeCurrent.nNodeNumber-1];
nStationOrder = HasStation( pAllRoutines[nRoutineCurrent], szTo, nStationCurrent, pAllRoutines[nRoutineCurrent].nFlag);
if(nStationOrder>=0) //表示找到
{
nodeCurrent.nRoutineOrder[nodeCurrent.nNodeNumber] = nRoutineCurrent;
nodeCurrent.nStationOrder[nodeCurrent.nNodeNumber] = nStationOrder;
nodeCurrent.nNodeNumber++;
if(nodeCurrent.nNodeNumber>nMinChangeTimes)
break;
PathNode pPathTempNode = new PathNode();
pPathTempNode.nSegNumber = (short)(nodeCurrent.nNodeNumber-1);
pPathTempNode.szRoutineName = new string[pPathTempNode.nSegNumber];
pPathTempNode.szFromStationName = new string[pPathTempNode.nSegNumber];
pPathTempNode.szToStationName = new string[pPathTempNode.nSegNumber];
for(i=0;i20)
break;
}
else //要将所有可能的路径加入队列
{
for(i=(short)(nStationCurrent+1);i=0;i--)
{
string szTheStation;
szTheStation=string.Copy(pAllRoutines[nRoutineCurrent].szStaionName[i]);
short j = SearchStation(pStations,nSCount,szTheStation);
for(short k=0;k=0;i--) //反向搜索
if(theRoutine.szStaionName[i].Equals(szStation))
return i;
}
return -1;
}
short SearchStation(Station[] pStations,short nSCount,string theName)
{
short j;
//用于统计时间
for(j=0;j