Example #1
0
        /// <summary>
        /// 获取当前站点的下一个可直达特殊点
        /// </summary>
        private HashSet <Station> GetNexts(Station current, Station start)
        {
            HashSet <Station> nexts = new HashSet <Station>();

            foreach (int i in current.PassLineIds)
            {
                MetroLine     line                = _metro.MetroLines.Find((MetroLine l) => l.Id == i);
                int           currentPosition     = line.Stations.IndexOf(current);
                HashSet <int> specialPositionsSet = new HashSet <int>();

                specialPositionsSet.Add(0);
                specialPositionsSet.Add(line.Stations.Count - 1);
                if (line.Stations.Contains(start))
                {
                    specialPositionsSet.Add(line.Stations.IndexOf(start));
                }
                foreach (Station s in line.GetTransferStations())
                {
                    specialPositionsSet.Add(line.Stations.IndexOf(s));
                }
                specialPositionsSet.Add(line.Stations.IndexOf(current));
                // 排序
                List <int> specialPositions = new List <int>(specialPositionsSet);
                specialPositions.Sort((a, b) => a > b ? 1 : -1);
                // 寻找下一个可达特殊点
                for (int j = 0; j < specialPositions.Count; j++)
                {
                    if (j == 0)
                    {
                        if (currentPosition == specialPositions[j] && j + 1 <= specialPositions.Count - 1)
                        {
                            nexts.Add(line.Stations[specialPositions[j + 1]]);
                        }
                    }
                    else if (j == specialPositions.Count - 1)
                    {
                        if (currentPosition == specialPositions[j] && j - 1 >= 0)
                        {
                            nexts.Add(line.Stations[specialPositions[j - 1]]);
                        }
                    }
                    else if (currentPosition > specialPositions[j - 1])
                    {
                        if (currentPosition == specialPositions[j])
                        {
                            nexts.Add(line.Stations[specialPositions[j - 1]]);
                            nexts.Add(line.Stations[specialPositions[j + 1]]);
                        }
                        else if (currentPosition < specialPositions[j])
                        {
                            nexts.Add(line.Stations[specialPositions[j - 1]]);
                            nexts.Add(line.Stations[specialPositions[j]]);
                        }
                    }
                }
            }
            return(nexts);
        }
Example #2
0
        // 两点之间寻路递归算法
        private void FindPath(List <Path> result, int startLine, int endLine, Station startStation, Station endStation,
                              List <Station> list, List <int> alreadyPassLines)
        {
            if (alreadyPassLines == null)
            {
                alreadyPassLines = new List <int>();
            }

            if (startStation.Id == endStation.Id)
            {
                return;
            }

            // 到同一路线上了,将结果加到list中
            if (startLine == endLine)
            {
                List <Station> temp = GetPathBetween(startStation, endStation, startLine);
                // 最后一次使用list了,直接用
                if (list == null) // 为null表示一开始就是同路,直接赋值
                {
                    list = temp;
                }
                else // 不为null表示之前有换线前的数据,在末尾添加
                {
                    foreach (Station s in temp)
                    {
                        list.Add(s);
                    }
                }
                list.Add(endStation);
                result.Add(new Path(list, alreadyPassLines));
            }

            // 还没在同一条路线上,寻找下一个换乘点
            alreadyPassLines.Add(startLine); // 当前路算已走过了,不要再回来了

            MetroLine line = _metro.MetroLines.Find((MetroLine l) => { return(l.Id == startLine); });

            foreach (Station s in line.GetTransferStations())
            {
                foreach (int i in s.PassLineIds)
                {
                    if (!alreadyPassLines.Contains(i))
                    {
                        List <Station> path;
                        List <Station> temp = GetPathBetween(startStation, s, startLine);
                        // 因为每个循环都会用到之前的list,所以这里不能更改list,传变量path
                        if (list == null) // 为null表示第一次,直接赋值
                        {
                            path = temp;
                        }
                        else // 不为null表示之前有路径,添加到末尾
                        {
                            path = new List <Station>();
                            foreach (Station st in list)
                            {
                                path.Add(st);
                            }
                            foreach (Station st in temp)
                            {
                                path.Add(st);
                            }
                        }
                        List <int> tempAlreadyLines = new List <int>();
                        foreach (int j in alreadyPassLines)
                        {
                            tempAlreadyLines.Add(j);
                        }
                        FindPath(result, i, endLine, s, endStation, path, tempAlreadyLines); // 递归查找子路径
                    }
                }
            }
        }