Ejemplo n.º 1
0
 public BFSPoint(int x, int y, BFSPoint parent, List <string> deps)
 {
     this.x       = x;
     this.y       = y;
     this.parent  = parent;
     Dependencies = deps;
 }
Ejemplo n.º 2
0
        public BFSPoint getPathBFS(int x, int y)
        {
            q.Enqueue(new BFSPoint(x, y, null));

            while (!(q.Count == 0))
            {
                BFSPoint p = q.Dequeue();

                if (arr[p.x, p.y] == 2)
                {
                    Console.WriteLine("Exit is reached!");
                    return(p);
                }

                if (isFree(p.x + 1, p.y))
                {
                    arr[p.x, p.y] = -1;
                    BFSPoint nextP = new BFSPoint(p.x + 1, p.y, p);
                    q.Enqueue(nextP);
                }

                if (isFree(p.x - 1, p.y))
                {
                    arr[p.x, p.y] = -1;
                    BFSPoint nextP = new BFSPoint(p.x - 1, p.y, p);
                    q.Enqueue(nextP);
                }

                if (isFree(p.x, p.y + 1))
                {
                    arr[p.x, p.y] = -1;
                    BFSPoint nextP = new BFSPoint(p.x, p.y + 1, p);
                    q.Enqueue(nextP);
                }

                if (isFree(p.x, p.y - 1))
                {
                    arr[p.x, p.y] = -1;
                    BFSPoint nextP = new BFSPoint(p.x, p.y - 1, p);
                    q.Enqueue(nextP);
                }
                lastPoint = p;
            }
            return(lastPoint);
        }
Ejemplo n.º 3
0
        private static void AnalyzeClosestPoints(BFSPoint start)
        {
            // Find reachable keys
            var reachable = destinationReached[start.origin].FindAll(x => !start.Seen.Contains(x.destination) && x.Dependencies.All(a => start.Inventory.Contains(a.ToLower()))).ToList();

            // Add parent to key
            foreach (var k in reachable)
            {
                // add key to key's inventory
                k.Inventory.Add(k.destination);
                // add parent to seen
                k.Seen.AddRange(start.destination);
                // add step count
                k.stepsNeeded += start.getSteps();

                tempList.Add(k);
            }



            // Repeat for lower level
        }
Ejemplo n.º 4
0
 public BFSPoint(int x, int y, BFSPoint parent)
 {
     this.x      = x;
     this.y      = y;
     this.parent = parent;
 }
Ejemplo n.º 5
0
        private static BFSPoint BFS(int x, int y, string destination, string origin)
        {
            BFSQueue = new Queue <BFSPoint>();
            var index = 0;

            BFSQueue.Enqueue(new BFSPoint(x, y, null, new List <string>()));
            while (!(BFSQueue.Count == 0))
            {
                BFSPoint p = BFSQueue.Dequeue();
                index++;
                if (map[p.y][p.x] == destination)
                {
                    Console.WriteLine("reached Destination");
                    p.stepsNeeded = p.getSteps();
                    p.destination = destination;
                    p.origin      = origin;
                    return(p);
                }

                if (isFree(p.x + 1, p.y))
                {
                    if (Doors.Contains(map[p.y][p.x + 1]))
                    {
                        p.Dependencies.Add(map[p.y][p.x + 1]);
                    }
                    map[p.y][p.x] = "#";
                    BFSPoint nextP = new BFSPoint(p.x + 1, p.y, p, p.Dependencies);
                    BFSQueue.Enqueue(nextP);
                }

                if (isFree(p.x - 1, p.y))
                {
                    if (Doors.Contains(map[p.y][p.x - 1]))
                    {
                        p.Dependencies.Add(map[p.y][p.x - 1]);
                    }
                    map[p.y][p.x] = "#";
                    BFSPoint nextP = new BFSPoint(p.x - 1, p.y, p, p.Dependencies);
                    BFSQueue.Enqueue(nextP);
                }

                if (isFree(p.x, p.y + 1))
                {
                    if (Doors.Contains(map[p.y + 1][p.x]))
                    {
                        p.Dependencies.Add(map[p.y + 1][p.x]);
                    }
                    map[p.y][p.x] = "#";
                    BFSPoint nextP = new BFSPoint(p.x, p.y + 1, p, p.Dependencies);
                    BFSQueue.Enqueue(nextP);
                }

                if (isFree(p.x, p.y - 1))
                {
                    if (Doors.Contains(map[p.y - 1][p.x]))
                    {
                        p.Dependencies.Add(map[p.y - 1][p.x]);
                    }
                    map[p.y][p.x] = "#";
                    BFSPoint nextP = new BFSPoint(p.x, p.y - 1, p, p.Dependencies);
                    BFSQueue.Enqueue(nextP);
                }
            }
            return(null);
        }