示例#1
0
        static void Main(string[] args)
        {
            var            names    = GetNames();
            List <Patient> patients = new List <Patient>();

            for (int i = 0; i < 1000; i++)
            {
                patients.Add(Patient.GenerateRandomPatient(names));
            }

            PriortyQueue <Patient> toServe = new PriortyQueue <Patient>(patients);

            for (int i = 0; i < 10; i++)
            {
                System.Console.WriteLine("{0} is saved!", toServe.GetFirst().Name);
                toServe.Dequeue();
            }
            System.Console.WriteLine("Unfortunately, everyone else died :(");
        }
示例#2
0
    public static TilePath DiscoverPath(Tilemap map, Vector3Int start, Vector3Int end)
    {
        //you will return this path to the user.  It should be the shortest path between
        //the start and end vertices
        TilePath discoveredPath = new TilePath();

        //TileFactory is how you get information on tiles that exist at a particular vector's
        //coordinates
        TileFactory tileFactory = TileFactory.GetInstance();

        //This is the priority queue of paths that you will use in your implementation of
        //Dijkstra's algorithm
        PriortyQueue <TilePath> pathQueue = new PriortyQueue <TilePath>();

        //You can slightly speed up your algorithm by remembering previously visited tiles.
        //This isn't strictly necessary.
        Dictionary <Vector3Int, int> discoveredTiles = new Dictionary <Vector3Int, int>();

        //quick sanity check
        if (map == null || start == null || end == null)
        {
            return(discoveredPath);
        }

        //This is how you get tile information for a particular map location
        //This gets the Unity tile, which contains a coordinate (.Position)
        var startingMapLocation = map.GetTile(start);

        //And this converts the Unity tile into an object model that tracks the
        //cost to visit the tile.
        var startingTile = tileFactory.GetTile(startingMapLocation.name);

        startingTile.Position = start;

        //Any discovered path must start at the origin!
        discoveredPath.AddTileToPath(startingTile);

        //This adds the starting tile to the PQ and we start off from there...
        pathQueue.Enqueue(discoveredPath);
        bool found = false;

        while (found == false && pathQueue.IsEmpty() == false)
        {
            //Get the next path from the queue
            TilePath Path = new TilePath(pathQueue.GetFirst());
            pathQueue.Dequeue();

            //Get newest added tile to process
            Tile       Tile     = new Tile(Path.GetMostRecentTile());
            Vector3Int Position = new Vector3Int(Tile.Position.x, Tile.Position.y, Tile.Position.z);

            //Add neighbors of newest tile to list for processing
            List <Vector3Int> adjacentTiles = new List <Vector3Int>();
            adjacentTiles.Add(new Vector3Int(Position.x, Position.y + 1, Position.z));
            adjacentTiles.Add(new Vector3Int(Position.x, Position.y - 1, Position.z));
            adjacentTiles.Add(new Vector3Int(Position.x + 1, Position.y, Position.z));
            adjacentTiles.Add(new Vector3Int(Position.x - 1, Position.y, Position.z));

            //For each loop processes each neighboring tile in list
            foreach (Vector3Int tile in adjacentTiles)
            {
                //Creates new path with current path as basis
                TilePath   newPath     = new TilePath(Path);
                Vector3Int newPosition = new Vector3Int(tile.x, tile.y, tile.z);

                //Grabs the data from our tile sprite in our tilemap
                var tileData = map.GetTile(newPosition);
                if (tileData == null)
                {
                    continue;
                }

                //Associates the data with an actual tile
                var newTile = tileFactory.GetTile(tileData.name);
                newTile.Position = newPosition;

                //If at end add tile and finish up
                if (newTile.Position == end)
                {
                    newPath.AddTileToPath(newTile);
                    discoveredPath = newPath;
                    found          = true;
                    break;
                }
                //If tile isn't end but hasnt been visited yet
                else if (discoveredTiles.ContainsKey(newPosition) == false)
                {
                    newPath.AddTileToPath(newTile);
                    pathQueue.Enqueue(newPath);
                    discoveredTiles.Add(newPosition, 1);
                }
            }
        }
        return(discoveredPath);
    }
示例#3
0
    private void computeConnection()
    {
        if (this.startNode.testCompatibility(this.endNode) == false)
        {
            this.connectionLength = -1;
        }
        else
        {
            if (this.startNode.getNodeLocation() == this.endNode.getNodeLocation())
            {
                this.connectionLength = this.startNode.getNodeLocation().getLocationLength();
                List <Location> toAdd = new List <Location>();
                toAdd.Add(this.startNode.getNodeLocation());
                this.connectionPath = new ConnectionPath(toAdd, this.connectionLength);
            }
            else
            {
                ConnectionPath discoveredPath                     = new ConnectionPath();
                PriortyQueue <ConnectionPath> pathQueue           = new PriortyQueue <ConnectionPath>();
                Dictionary <Location, int>    discoveredLocations = new Dictionary <Location, int>();

                discoveredPath.addLocation(startNode.getNodeLocation());
                discoveredLocations.Add(startNode.getNodeLocation(), 1);

                pathQueue.Enqueue(discoveredPath);
                bool found = false;

                int count = 0;
                while (found == false && pathQueue.IsEmpty() == false)
                {
                    ConnectionPath current_path = new ConnectionPath(pathQueue.GetFirst());
                    pathQueue.Dequeue();
                    Location currentLocation = current_path.GetMostRecentLocation();

                    for (int i = 0; i < currentLocation.getAdjacentLocations().Count; i++)
                    {
                        ConnectionPath new_path         = new ConnectionPath(current_path);
                        Location       neighborLocation = currentLocation.getAdjacentLocations()[i];


                        if (neighborLocation.getLocationLength() == -1)
                        {
                            continue;
                        }
                        if (neighborLocation.getLocationName() == this.endNode.getNodeLocation().getLocationName())
                        {
                            new_path.addLocation(neighborLocation);
                            discoveredPath = new_path;
                            found          = true;
                            break;
                        }
                        else if (discoveredLocations.ContainsKey(neighborLocation) == false)
                        {
                            new_path.addLocation(neighborLocation);
                            pathQueue.Enqueue(new_path);
                            discoveredLocations.Add(neighborLocation, 1);
                            count++;
                        }
                    }
                }
                this.connectionPath   = discoveredPath;
                this.connectionLength = discoveredPath.getPathWeight();
            }
        }
    }
示例#4
0
    public static TilePath DiscoverPath(Tilemap map, Tilemap objects, Vector3Int start, Vector3Int end)
    {
        //you will return this path to the user.  It should be the shortest path between
        //the start and end vertices
        TilePath discoveredPath = new TilePath();

        //TileFactory is how you get information on tiles that exist at a particular vector's
        //coordinates
        TileFactory tileFactory = TileFactory.GetInstance();

        //This is the priority queue of paths that you will use in your implementation of
        //Dijkstra's algorithm
        PriortyQueue <TilePath> pathQueue = new PriortyQueue <TilePath>();

        //You can slightly speed up your algorithm by remembering previously visited tiles.
        //This isn't strictly necessary.
        Dictionary <Vector3Int, int> discoveredTiles = new Dictionary <Vector3Int, int>();

        if (objects == null)
        {
            Debug.Log("Objects layer is null");
        }

        //quick sanity check
        if (map == null || objects == null || start == null || end == null)
        {
            return(discoveredPath);
        }

        //This is how you get tile information for a particular map location
        //This gets the Unity tile, which contains a coordinate (.Position)
        var startingMapLocation = map.GetTile(start);
        var startingMapObsticle = objects.GetTile(start);
        //And this converts the Unity tile into an object model that tracks the
        //cost to visit the tile.
        var startingTile = tileFactory.GetTile(startingMapLocation.name);

        if (startingMapObsticle != null)
        {
            var startingObject = tileFactory.GetTile(startingMapObsticle.name);
            startingTile.Position = start;
            startingTile.Weight   = startingTile.Weight + startingObject.Weight;
        }
        else
        {
            int startingObjWeight = 0;
            startingTile.Position = start;
            startingTile.Weight   = startingTile.Weight + startingObjWeight;
            Debug.Log(startingTile.Weight);
        }

        //Any discovered path must start at the origin!
        discoveredPath.AddTileToPath(startingTile);

        //This adds the starting tile to the PQ and we start off from there...
        pathQueue.Enqueue(discoveredPath);
        bool found = false;

        int count = 0;

        while (found == false && pathQueue.IsEmpty() == false)
        {
            //TODO: Implement Dijkstra's algorithm!

            // Dequeue Top,
            TilePath current_path = new TilePath(pathQueue.GetFirst());
            pathQueue.Dequeue();
            Tile       current_tile     = new Tile(current_path.GetMostRecentTile());
            Vector3Int current_position = new Vector3Int(current_tile.Position.x, current_tile.Position.y, current_tile.Position.z);

            List <Vector3Int> adjacent_tiles = new List <Vector3Int>();
            adjacent_tiles.Add(new Vector3Int(current_position.x, current_position.y + 1, current_position.z));
            adjacent_tiles.Add(new Vector3Int(current_position.x, current_position.y - 1, current_position.z));
            adjacent_tiles.Add(new Vector3Int(current_position.x + 1, current_position.y, current_position.z));
            adjacent_tiles.Add(new Vector3Int(current_position.x - 1, current_position.y, current_position.z));
            for (int i = 0; i < adjacent_tiles.Count; i++)
            {
                TilePath   new_path          = new TilePath(current_path);
                Vector3Int neighbor_position = new Vector3Int(adjacent_tiles[i].x, adjacent_tiles[i].y, adjacent_tiles[i].z);
                //Get Neighbor node and set equal to visited_node
                int objectWeight          = 0;
                var visited_node_location = map.GetTile(neighbor_position);
                var visited_node_object   = objects.GetTile(neighbor_position);
                if (visited_node_location == null)
                {
                    continue;
                }
                if (visited_node_object != null)
                {
                    var visited_object = tileFactory.GetTile(visited_node_object.name);
                    objectWeight = visited_object.Weight;
                    if (objectWeight == -1)
                    {
                        continue;
                    }
                }
                var visited_node = tileFactory.GetTile(visited_node_location.name);
                if (visited_node.Weight == -1)
                {
                    continue;
                }
                visited_node.Position = neighbor_position;
                visited_node.Weight   = visited_node.Weight + objectWeight;
                if (visited_node.Position == end)
                {
                    new_path.AddTileToPath(visited_node);
                    discoveredPath = new_path;
                    found          = true;
                    break;
                }
                else if (discoveredTiles.ContainsKey(neighbor_position) == false)
                {
                    new_path.AddTileToPath(visited_node);
                    pathQueue.Enqueue(new_path);
                    discoveredTiles.Add(neighbor_position, 1);
                    count++;
                }
            }
        }
        Debug.Log(discoveredPath.Weight);
        return(discoveredPath);
    }