Ejemplo n.º 1
0
    public List <Cell> CalcPath_AStar(Cell start, Cell end)
    {
        priorQueue.Clear();
        ClearWeights();
        CellChain tail = new CellChain(start, null);

        priorQueue.Enqueue(tail, 0);
        SetWeight(start, 1);
        while (priorQueue.Count > 0)
        {
            CellChain current = priorQueue.Dequeue();
            Cell      curcell = current.cell;
            if (curcell.Equals(end))
            {
                tail = current;
                break;
            }
            List <Cell> neights = field.GetNeight(current.cell);
            for (int i = 0; i < neights.Count; i++)
            {
                Cell neight  = neights[i];
                int  newCost = GetWeight(current.cell) + 1;
                if (GetWeight(neight) == 0 || newCost < GetWeight(neight))
                {
                    SetWeight(neight, newCost);
                    CellChain cc    = new CellChain(neight, current);
                    int       prior = newCost + Cell.GetCityLength(neight, end);
                    priorQueue.Enqueue(cc, prior);
                }
            }
        }
        path = new List <Cell>();
        CellChain root = tail;

        while (root != null)
        {
            path.Add(root.cell);
            root = root.from;
        }
        path.Reverse();
        return(path);
    }
Ejemplo n.º 2
0
    public List <Cell> CalcPath_BFS(Cell start, Cell end)
    {
        queue.Clear();

        CellChain tail = new CellChain(start, null);

        queue.Enqueue(tail);

        field.CleanUpAll();
        while (queue.Count > 0)
        {
            var current = queue.Dequeue();
            field.SetVisited(current.cell);
            foreach (Cell x in field.GetNeight(current.cell))
            {
                if (!field.isVisited(x))
                {
                    var cc = new CellChain(x, current);
                    if (end.i == x.i && end.j == x.j)
                    {
                        tail = cc;
                        break;
                    }
                    queue.Enqueue(cc);
                }
            }
        }
        path = new List <Cell>();
        CellChain root = tail;

        while (root != null)
        {
            path.Add(root.cell);
            root = root.from;
        }
        path.Reverse();
        return(path);
    }
Ejemplo n.º 3
0
 public CellChain(Cell cur, CellChain from)
 {
     this.cell = cur;
     this.from = from;
 }