Пример #1
0
        /// <summary>Precomputes all allowed positions and predecessors</summary>
        /// <param name="nPaths">Number of paths</param>
        /// <param name="WindowSize">Window size</param>
        private void PrecomputePositions(int W, int H, int nPaths, int WindowSize)
        {
            //possible positions: [0] goes from 0 to < Height-nPaths, [1] goes from [0] to Height-nPaths+1, nPaths-1 goes from [nPaths-2] to Heights-1
            allPositions = new List <int[]>();

            //possible previous states
            allowedPrevious = new List <List <int> >();

            //Compare from left to right
            intComparer ic = new intComparer();

            int[] curPosition = new int[nPaths];
            for (int k = 0; k < nPaths; k++)
            {
                curPosition[k] = k * MINDISTANCEBETWEENLINES;
            }

            while (curPosition[0] < H - (nPaths - 1) * MINDISTANCEBETWEENLINES)
            {
                int[] position = (int[])curPosition.Clone();
                allPositions.Add(position);
                allowedPrevious.Add(null);
                incrementPositionWithoutOverlap(ref curPosition, nPaths - 1, H, nPaths);
            }

            Parallel.For(0, allPositions.Count, k =>
                         //for (int k = 0; k < allPositions.Count; k++)
            {
                List <int[]> lstPossiblePrev = GetAllValidOrigins(allPositions[k], H, WindowSize);
                List <int> possibleIndexes   = new List <int>();
                foreach (int[] possibPrev in lstPossiblePrev)
                {
                    int p = allPositions.BinarySearch(possibPrev, ic);
                    if (p < 0)
                    {
                        throw new Exception("Error!");
                    }

                    possibleIndexes.Add(p);
                }

                allowedPrevious[k] = possibleIndexes;
            });
        }
Пример #2
0
    static void Main(string[] args)
    {
        string            startPoint = Console.ReadLine().Substring(9);
        string            endPoint   = Console.ReadLine().Substring(9);
        int               N          = int.Parse(Console.ReadLine());
        List <Przystanek> Przystanki = new List <Przystanek>();

        for (int i = 0; i < N; i++)
        {
            Przystanki.Add(new Przystanek());
            Przystanki[Przystanki.Count - 1].Wczytaj();
        }
        int startid = Przystanki.FindIndex(x => x.id == startPoint);
        int endid   = Przystanki.FindIndex(x => x.id == endPoint);
        int M       = int.Parse(Console.ReadLine());

        for (int i = 0; i < M; i++)
        {
            string[] route = Console.ReadLine().Split(' ');
            string   Pz    = route[0].Substring(9);
            string   Pdo   = route[1].Substring(9);
            int      indz  = Przystanki.FindIndex(x => x.id == Pz);
            int      inddo = Przystanki.FindIndex(x => x.id == Pdo);
            double   latn  = Przystanki[inddo].lat;
            double   lonn  = Przystanki[inddo].lon;
            Przystanki[indz].Przydziel(inddo, latn, lonn);
        }
        if (startid == endid)
        {
            Console.WriteLine(Przystanki[startid].name);
        }
        else
        {
            int          curid;
            int          licznik  = 0;
            List <int[]> odstartu = new List <int[]>(); // 0 - poprzedni przystanek id, 1 - poziom
            odstartu.Add(new int[] { startid, 0, -1, 0 });
            intComparer dc = new intComparer();
            do
            {
                curid = odstartu[licznik][0];
                int ldojazd = Przystanki[curid].dojazd.Count;
                for (int i = 0; i < ldojazd; i++)
                {
                    double oo   = Przystanki[curid].dystans[i] * 1000 + odstartu[licznik][1];
                    int[]  test = new int[] { Przystanki[curid].dojazd[i], (int)oo, curid, licznik };
                    if (LContains(odstartu, test, 0) == false)
                    {
                        odstartu.Add(test);
                    }
                    else if (LContains(odstartu, test, 0) == true && odstartu[odstartu.FindIndex(x => x[0] == test[0])][1] > oo)
                    {
                        int indeksdupy = odstartu.FindIndex(x => x[0] == test[0]);
                        odstartu.Add(test);
                        odstartu.RemoveRange(indeksdupy, 1);
                    }
                }
                odstartu.Sort(licznik + 1, odstartu.Count - licznik - 1, dc);
                licznik++;
            }while(licznik < odstartu.Count);
            List <int> wyswietl = new List <int>();
            wyswietl.Add(endid);
            if (odstartu.Exists(x => x[0] == endid) == true)
            {
                int nextid;
                do
                {
                    int previd = wyswietl[wyswietl.Count - 1];
                    nextid = odstartu.Find(x => x[0] == previd)[2];
                    int[] u = odstartu.Find(x => x[0] == previd);
                    wyswietl.Add(nextid);
                }while(nextid != startid);
                wyswietl.Reverse();
                foreach (int a in wyswietl)
                {
                    Console.WriteLine(Przystanki[a].name);
                }
            }
            else
            {
                Console.WriteLine("IMPOSSIBLE");
            }
        }
        dumpTime();
    }