Пример #1
0
 public Boolean insert(Spot x)
 {
     if (x == null)
     {
         return(false);
     }
     if (length == 0)
     {
         first = x;
         last  = x;
     }
     else
     {
         if (x.f <= first.f)
         {
             first.previous = x;
             x.next         = first;
             x.previous     = null;
             first          = x;
         }
         else
         {
             Spot tmp = first.next;
             while (tmp != null)
             {
                 if (x.f <= tmp.f)
                 {
                     x.previous        = tmp.previous;
                     x.next            = tmp;
                     tmp.previous.next = x;
                     tmp.previous      = x;
                     break;
                 }
                 tmp = tmp.next;
             }
             if (tmp == null)
             {
                 x.previous = last;
                 last.next  = x;
                 last       = x;
             }
         }
     }
     length++;
     return(true);
 }
Пример #2
0
        static Spot[,] MakeReasonableGrid(int r, int c, int pacman_r, int pacman_c, int food_r, int food_c, String[] grid)
        {
            Spot[,] Grid = new Spot[r, c];
            int j_index = 0;

            foreach (string item in grid)
            {
                char[] helper = item.ToCharArray();
                for (int i = 0; i < helper.Length; i++)
                {
                    if (helper[i].ToString() == "%")
                    {
                        Grid[j_index, i] = new Spot(j_index, i, false);
                        continue;
                    }
                    if (helper[i].ToString() == "-")
                    {
                        Grid[j_index, i] = new Spot(j_index, i, true);
                        continue;
                    }
                    else
                    {
                        Grid[j_index, i] = new Spot(j_index, i, true);
                    }
                }
                j_index++;
            }

            for (var i = 0; i < c; i++)
            {
                for (var j = 0; j < r; j++)
                {
                    if (Grid[j, i].accessible)
                    {
                        Grid[j, i].addNeighbors(Grid, c, r);
                    }
                }
            }

            return(Grid);
        }
Пример #3
0
 public Spot getElement()
 {
     if (first == null)
     {
         return(null);
     }
     else
     {
         length--;
         Spot tmp = first;
         if (tmp.next == null)
         {
             last = null;
             return(tmp);
         }
         else
         {
             first          = tmp.next;
             first.previous = null;
             return(tmp);
         }
     }
 }
Пример #4
0
 public void update(double cost, Spot Parent)
 {
     parent = Parent;
     g      = cost;
     f      = g + h;
 }
Пример #5
0
        static void astar(int r, int c, int pacman_r, int pacman_c, int food_r, int food_c, String[] grid)
        {
            SimplePriorityQue openList = new SimplePriorityQue();
            //List<Spot> openList = new List<Spot>();
            HashSet <Spot> closedList = new HashSet <Spot>();

            Spot[,] Grid = MakeReasonableGrid(r, c, pacman_r, pacman_c, food_r, food_c, grid);

            var start = Grid[pacman_r, pacman_c];
            var end   = Grid[food_r, food_c];

            openList.insert(start);

            Spot current = null;

            while (!openList.isEmpty())
            {
                current = openList.getElement();
                if (!current.accessible)
                {
                    continue;
                }

                if (current == end)
                {
                    break;
                }
                closedList.Add(current);

                foreach (var item in current.neighbors)
                {
                    if (closedList.Contains(item))
                    {
                        continue;
                    }
                    if (!item.accessible)
                    {
                        continue;
                    }
                    double tempG     = current.g + 1;
                    double heuristic = Math.Abs(end.I - current.I) + Math.Abs(end.J - current.J);
                    if (!openList.contains(item))
                    {
                        item.h      = heuristic;
                        item.g      = tempG;
                        item.f      = item.g + item.h;
                        item.parent = current;
                        openList.insert(item);
                    }
                    else
                    {
                        Spot tmp = openList.get(item);
                        if (tmp.g > tempG)
                        {
                            tmp.update(tempG, current);
                        }
                        openList.insert(tmp);
                    }
                }
            }

            List <Spot> path = new List <Spot>();
            Spot        help = current.parent;

            while (help != null)
            {
                path.Add(help);
                help = help.parent;
            }
            path.Reverse();
            Console.WriteLine(path.Count);
            foreach (var item in path)
            {
                Console.WriteLine(item.I + " " + item.J);
            }
        }