示例#1
0
 public void WorkPriorityQueue()
 {
     Student firstStudent = new Student("Alina", "Kylish", 123456789, new DateTime(1988, 4, 3));
     Student secondStudent = new Student("Elena", "Kylish", 987654321, new DateTime(1987, 8, 15));
     Student thirdStudent = new Student("Oleg", "Ivanov", 975312468, new DateTime(1992, 11, 20));
     Student fourthStudent = new Student("Andrey", "Pavlov", 1243576890, new DateTime(1977, 8, 10));
     PriorityQueue<Student> students = new PriorityQueue<Student>();
     students.Enqueue(firstStudent, 0);
     students.Enqueue(secondStudent, 1);
     students.Enqueue(thirdStudent, 2);
     students.Enqueue(fourthStudent, 0);
     Console.WriteLine(students.Count());
     Console.WriteLine(students.First());
     Console.WriteLine(students.Last());
     students.Dequeue();
     Console.WriteLine(students.Count());
     Console.ReadKey();
     students.Clear();
     students.Dequeue(); // throw exeption with message "Queue is empty. There is no value in queue"
 }
        static void WorkWithPriorityQueue()
        {
            PriorityQueue<int> numbers = new PriorityQueue<int>();
            try
            {
                Console.WriteLine("Count = {0}", numbers.Count());
                //Console.WriteLine(numbers.First());
                //Console.WriteLine(numbers.Last());
                //numbers.Add(10);
               // Print(numbers);
                numbers.Enqueue(1, 5);
                numbers.Enqueue(2, 11);
                numbers.Enqueue(1, 1);
               // Print(numbers);
                numbers.Enqueue(2, 1);
                numbers.Enqueue(3, 1);
                numbers.Enqueue(15, 2);
                numbers.Enqueue(25, 2);
                numbers.Enqueue(21, 2);
              //  Print(numbers);
                numbers.Enqueue(1021, 3);
                numbers.Enqueue(375, 5);
                numbers.Enqueue(124243323, 8);
              //  Print(numbers);
                Console.WriteLine("Count = {0}", numbers.Count());
                Console.WriteLine("Count with priority 1= {0}", numbers.GetCount(1));
                Console.WriteLine("Count with priority 2= {0}", numbers.GetCount(2));
                Console.WriteLine("Count with priority 3= {0}", numbers.GetCount(3));
                Console.WriteLine("Count with priority 4= {0}", numbers.GetCount(4));
                Console.WriteLine("Count with priority 5= {0}", numbers.GetCount(5));
                Console.WriteLine("Count with priority 8= {0}", numbers.GetCount(8));
                Console.WriteLine("Count with priority 11= {0}", numbers.GetCount(11));
                Console.WriteLine(numbers.First());
                Console.WriteLine(numbers.Last());
                Console.WriteLine(numbers.Dequeue());
             //   Print(numbers);
                Console.WriteLine(numbers.Dequeue());
                Console.WriteLine(numbers.Dequeue());
                Console.WriteLine(numbers.Dequeue());
              //  Print(numbers);
                Console.WriteLine(numbers.Dequeue());
                Console.WriteLine(numbers.Dequeue());
                Console.WriteLine(numbers.Dequeue());
             //   Print(numbers);
                Console.WriteLine("Count = {0}", numbers.Count());
                Console.WriteLine("Count with priority 1= {0}", numbers.GetCount(1));
                Console.WriteLine("Count with priority 2= {0}", numbers.GetCount(2));
                Console.WriteLine("Count with priority 3= {0}", numbers.GetCount(3));
                Console.WriteLine("Count with priority 4= {0}", numbers.GetCount(4));
                Console.WriteLine("Count with priority 5= {0}", numbers.GetCount(5));
                Console.WriteLine("Count with priority 8= {0}", numbers.GetCount(8));
                Console.WriteLine("Count with priority 11= {0}", numbers.GetCount(11));
                numbers.Clear();
                Console.WriteLine("Count = {0}", numbers.Count<int>());
             //   Print(numbers);

                numbers.Add(10);
                numbers.Add(11);
                Console.WriteLine(numbers.First());
                Console.WriteLine(numbers.Last());
                Console.WriteLine();
                numbers.Enqueue(1, 5);
                numbers.Add(5);
                Print(numbers);
                Console.WriteLine();
            }
            catch
            {
                Console.WriteLine("Is Exeption");
            }
            Console.ReadKey();
        }
        public static List<Position> FindPath( Engine engine, Unit unit, Tile start, Tile end )
        {
            List<Position> path = null;

            if (!unit.CanMove(end.position))
            {
                foreach (Position pos in BreadthFirst(engine, end.position, -1, 500))
                {
                    if (unit.CanMove(pos))
                    {
                        end = engine.map.tiles[pos.x, pos.y];
                        break;
                    }
                }
            }
            if (!unit.CanMove(end.position))
            {
                return null;
            }

            bool success = false;
            start.pathParent = null;
            start.pathDistance = 0;
            start.pathHeuristic = start.pathDistance + FValue(start, end);
            PriorityQueue<Tile> openSet = new PriorityQueue<Tile>();
            openSet.Enqueue(start);
            int count = 0;

            //generate path
            while ( openSet.Count() > 0 )
            {
                count += 1;
                Tile currentBest = openSet.Dequeue();
                currentBest.pathIndex = pathCounter;
                // if we are at the goal end
                if (currentBest.position.Equals(end.position))
                {
                    success = true;
                    break;
                }
                // Give up if we backtrack too far
                if ((currentBest.pathHeuristic >= start.pathHeuristic*14 &&
                    count > 2000) || count > 4000)
                {
                    break;
                }
                // Take current best, generate all possible nodes, push them onto queue
                foreach (var neighbor in currentBest.neighbors)
                {
                    if (!unit.CanMove(neighbor.position))
                    {
                        continue;
                    }
                    double tentativeCost = currentBest.pathDistance + neighbor.tileType.movementCost;
                    if (neighbor.pathIndex < pathCounter)
                    {
                        neighbor.pathIndex = pathCounter;
                        neighbor.pathParent = currentBest;
                        neighbor.pathDistance = tentativeCost;
                        neighbor.pathHeuristic = neighbor.pathDistance + FValue(neighbor, end);
                        openSet.Enqueue(neighbor);
                    }
                    else if (tentativeCost < neighbor.pathDistance)
                    {
                        // Update costs if the current path is better than the existing one
                        neighbor.pathParent = currentBest;
                        neighbor.pathDistance = tentativeCost;
                        neighbor.pathHeuristic = neighbor.pathDistance + FValue(neighbor, end);
                        openSet.HeapifyUp(neighbor);
                    }
                }
            }
            if ( success )
            {
                // Generate path by following parent from end to start
                path = new List<Position>();
                Tile runner = end;
                while (runner != null)
                {
                    path.Insert(0, runner.position);
                    runner = runner.pathParent;
                }
            }

            pathCounter += 1;

            return path;
        }
 public void Count_length_of_non_empty_queue()
 {
     var sut = new PriorityQueue<string>();
     sut.Enqueue("a", 1);
     Assert.AreEqual(1, sut.Count());
 }
 public void Count_length_of_empty_queue()
 {
     var sut = new PriorityQueue<string>();
     Assert.AreEqual(0, sut.Count());
 }
示例#6
0
        //    Finds the next node to hop to in the route to vertDestination and returns it's handler
        //    if all neighbour nodes are overloaded, than return the source node
        private List<Vertex> findRouteD( int hndlSource, int hndlDestination )
        {
            int numNodes = this.xDim * this.yDim;
            PriorityQueue<int, int> minRouteQ = new PriorityQueue<int, int>( numNodes );
            List<Vertex> pathList = new List<Vertex>( numNodes - 1 );

            //	marking all vertices with infinite distance
            foreach ( Vertex vrt in this.vertexList.TheList )
            {
                minRouteQ.AddToBottom( int.MaxValue, vrt.Handler );
            }

            //	assigning the source vertex 0 distance value
            minRouteQ.SetPriority( hndlSource, 0 );

            int currentHandler = 0;
            int minDistance;

            //	going through all vertices
            while ( minRouteQ.Count() != 0 )
            {
                //	checking what's on top of the priority queue
                minDistance = minRouteQ.PeekTop().Priority;
                currentHandler = minRouteQ.PeekTop().Data;

                //	removing the current element from the queue
                minRouteQ.Dequeue();

                //	all remaining vertices are inaccesible from source
                if ( minDistance == int.MaxValue )
                {
                    break;
                }

                List<int> currentNeightBours = this.incidencyMatrix.GetConnectedVertices( currentHandler );
                int distFromCurrent;

                foreach ( int vHandler in currentNeightBours )
                {
                    distFromCurrent = this.incidencyMatrix.GetDistance( currentHandler, vHandler ) + minDistance;

                    if ( minRouteQ.GetPriority(vHandler) > distFromCurrent )
                    {
                        minRouteQ.SetPriority( vHandler, distFromCurrent );
                        pathList.Add( this.vertexList.Find(vHandler) );
                    }
                }
            }

            //minRouteQ
            return pathList;
        }