Exemple #1
0
        public static IState bfs(IState start, IState end)
        {
            Queue <IState> found   = new Queue <IState>();
            Queue <IState> visited = new Queue <IState>();
            IState         temp    = new IState(start.leftPriests, start.leftDevils, start.rightPriests, start.rightDevils, start.boat, null);

            found.Enqueue(temp);

            while (found.Count > 0)
            {
                temp = found.Peek();

                if (temp == end)
                {
                    //Debug.Log("solution path:\n");
                    while (temp.parent != start)
                    {
                        temp = temp.parent;
                    }
                    return(temp);
                }

                found.Dequeue();
                visited.Enqueue(temp);


                // next node
                if (temp.boat)
                {
                    // one move to right
                    if (temp.leftPriests > 0)
                    {
                        IState next = new IState(temp);
                        next.parent = new IState(temp);
                        next.boat   = false;
                        next.leftPriests--;
                        next.rightPriests++;
                        if (next.valid() && !visited.Contains(next) && !found.Contains(next))
                        {
                            found.Enqueue(next);
                        }
                    }
                    if (temp.leftDevils > 0)
                    {
                        IState next = new IState(temp);
                        next.parent = new IState(temp);
                        next.boat   = false;
                        next.leftDevils--;
                        next.rightDevils++;
                        if (next.valid() && !visited.Contains(next) && !found.Contains(next))
                        {
                            found.Enqueue(next);
                        }
                    }
                    // two moves to right
                    if (temp.leftDevils > 0 && temp.leftDevils > 0)
                    {
                        IState next = new IState(temp);
                        next.parent = new IState(temp);
                        next.boat   = false;
                        next.leftPriests--;
                        next.leftDevils--;
                        next.rightPriests++;
                        next.rightDevils++;
                        if (next.valid() && !visited.Contains(next) && !found.Contains(next))
                        {
                            found.Enqueue(next);
                        }
                    }
                    if (temp.leftDevils > 1)
                    {
                        IState next = new IState(temp);
                        next.parent       = new IState(temp);
                        next.boat         = false;
                        next.leftDevils  -= 2;
                        next.rightDevils += 2;
                        if (next.valid() && !visited.Contains(next) && !found.Contains(next))
                        {
                            found.Enqueue(next);
                        }
                    }
                    if (temp.leftPriests > 1)
                    {
                        IState next = new IState(temp);
                        next.parent        = new IState(temp);
                        next.boat          = false;
                        next.leftPriests  -= 2;
                        next.rightPriests += 2;
                        next.parent        = new IState(temp);
                        if (next.valid() && !visited.Contains(next) && !found.Contains(next))
                        {
                            found.Enqueue(next);
                        }
                    }
                }
                else
                {
                    //one move to left
                    if (temp.rightPriests > 0)
                    {
                        IState next = new IState(temp);
                        next.parent = new IState(temp);
                        next.boat   = true;
                        next.rightPriests--;
                        next.leftPriests++;
                        if (next.valid() && !visited.Contains(next) && !found.Contains(next))
                        {
                            found.Enqueue(next);
                        }
                    }
                    if (temp.rightDevils > 0)
                    {
                        IState next = new IState(temp);
                        next.parent = new IState(temp);
                        next.boat   = true;
                        next.rightDevils--;
                        next.leftDevils++;
                        if (next.valid() && !visited.Contains(next) && !found.Contains(next))
                        {
                            found.Enqueue(next);
                        }
                    }
                    //two moves to left
                    if (temp.rightDevils > 0 && temp.rightDevils > 0)
                    {
                        IState next = new IState(temp);
                        next.parent = new IState(temp);
                        next.boat   = true;
                        next.rightPriests--;
                        next.rightDevils--;
                        next.leftPriests++;
                        next.leftDevils++;
                        if (next.valid() && !visited.Contains(next) && !found.Contains(next))
                        {
                            found.Enqueue(next);
                        }
                    }
                    if (temp.rightDevils > 1)
                    {
                        IState next = new IState(temp);
                        next.parent       = new IState(temp);
                        next.boat         = true;
                        next.rightDevils -= 2;
                        next.leftDevils  += 2;
                        if (next.valid() && !visited.Contains(next) && !found.Contains(next))
                        {
                            found.Enqueue(next);
                        }
                    }
                    if (temp.rightPriests > 1)
                    {
                        IState next = new IState(temp);
                        next.parent        = new IState(temp);
                        next.boat          = true;
                        next.rightPriests -= 2;
                        next.leftPriests  += 2;
                        if (next.valid() && !visited.Contains(next) && !found.Contains(next))
                        {
                            found.Enqueue(next);
                        }
                    }
                }
            }
            return(null);
        }
Exemple #2
0
 public IState(int leftPriests, int leftDevils, int rightPriests, int rightDevils, bool boat, IState parent)
 {
     this.leftPriests  = leftPriests;
     this.leftDevils   = leftDevils;
     this.rightPriests = rightPriests;
     this.rightDevils  = rightDevils;
     this.boat         = boat;
     this.parent       = parent;
 }