Ejemplo n.º 1
0
 // Returns true if there are complete messages to retrieve
 public bool HasData()
 {
     if (recvQueue.Count() > 0)
     {
         return(true);
     }
     else
     {
         return(false);
     }
 }
Ejemplo n.º 2
0
 private bool DoesPathExist(byte u, byte v, byte N)
 {
     if (u == v)
     {
         return(true);
     }
     System.Collections.Generic.Queue <byte> que = new System.Collections.Generic.Queue <byte>(N);
     que.Enqueue(u);
     while (que.Count() != 0)
     {
         byte curr = que.Dequeue();
         if (curr == v)
         {
             return(true);
         }
         if (graph.ContainsKey(curr))
         {
             if (graph[curr].Contains(v))
             {
                 return(true);
             }
         }
         if (graph.ContainsKey(v))
         {
             if (graph[v].Contains(curr))
             {
                 return(true);
             }
         }
         if (!visited[curr])
         {
             visited[curr] = true;
             if (graph.ContainsKey(curr))
             {
                 var enumerator = graph[curr].GetEnumerator();
                 while (enumerator.MoveNext())
                 {
                     que.Enqueue(enumerator.Current);
                 }
             }
         }
     }
     return(false);
 }
Ejemplo n.º 3
0
 /// <summary>
 /// run prim's algorithm
 /// </summary>
 /// <param name="g"></param>
 /// <param name="s"></param>
 private void prim(EdgeWeightedGraph g, int s)
 {
     scan(g, s);
     while (!pq.IsEmpty() && mst.Count() < g.V - 1)//better to stop when mst has v-1 edge
     {
         Edge e = pq.Pop();
         int  v = e.Either();
         int  w = e.Other(v);
         if (marked[v] && marked[w])
         {
             continue;//lazy, both v and w already scanned
         }
         mst.Enqueue(e);
         weight += e.Weight();
         if (!marked[v])
         {
             scan(g, v);//v becomes part of tree
         }
         if (!marked[w])
         {
             scan(g, w);//w becomes part of tree
         }
     }
 }
Ejemplo n.º 4
0
        public bool MakeStep(int direction)
        {
            switch (direction)
            {
            case UP:
                if ((headY < N) && ((cell[headX, headY + 1] == FREE) || (cell[headX, headY + 1] == APPLE)))
                {
                    cell[headX, headY + 1] = BODY;
                    position HeadPosition = new position();
                    HeadPosition.x = headX;
                    HeadPosition.y = headY++;

                    //C# does not have a insert(0,xxx) method or push_front method, so have to hack like this
                    if (cell[headX, headY] == APPLE)
                    {
                        Queue <position> newSnake = new Queue <position>();
                        newSnake.Enqueue(HeadPosition);
                        foreach (position pos in snake)
                        {
                            newSnake.Enqueue(pos);
                        }
                        snake = newSnake;
                    }
                    if (cell[headX, headY] == FREE)
                    {
                        Queue <position> newSnake = new Queue <position>();
                        newSnake.Enqueue(HeadPosition);
                        int c = snake.Count();
                        foreach (position pos in snake)
                        {
                            if (c-- > 1)      // add all but last one
                            {
                                newSnake.Enqueue(pos);
                            }
                        }
                        snake = newSnake;
                    }
                    return(true);
                }
                else
                {
                    return(false);
                }
                break;

            case RIGHT:
                if ((headX < N) && ((cell[headX + 1, headY + 1] == FREE) || (cell[headX + 1, headY + 1] == APPLE)))
                {
                    cell[headX + 1, headY] = BODY;
                    position HeadPosition = new position();
                    HeadPosition.x = headX++;
                    HeadPosition.y = headY;

                    //C# does not have a insert(0,xxx) method or push_front method, so have to hack like this
                    if (cell[headX, headY] == APPLE)
                    {
                        Queue <position> newSnake = new Queue <position>();
                        newSnake.Enqueue(HeadPosition);
                        foreach (position pos in snake)
                        {
                            newSnake.Enqueue(pos);
                        }
                        snake = newSnake;
                    }
                    if (cell[headX, headY] == FREE)
                    {
                        Queue <position> newSnake = new Queue <position>();
                        newSnake.Enqueue(HeadPosition);
                        int c = snake.Count();
                        foreach (position pos in snake)
                        {
                            if (c-- > 1)      // add all but last one
                            {
                                newSnake.Enqueue(pos);
                            }
                        }
                        snake = newSnake;
                    }
                    return(true);
                }
                else
                {
                    return(false);
                }
                break;

            case DOWN:
                if ((headY > 0) && ((cell[headX, headY - 1] == FREE) || (cell[headX, headY - 1] == APPLE)))
                {
                    cell[headX, headY + 1] = BODY;
                    position HeadPosition = new position();
                    HeadPosition.x = headX;
                    HeadPosition.y = headY--;

                    //C# does not have a insert(0,xxx) method or push_front method, so have to hack like this
                    if (cell[headX, headY] == APPLE)
                    {
                        Queue <position> newSnake = new Queue <position>();
                        newSnake.Enqueue(HeadPosition);
                        foreach (position pos in snake)
                        {
                            newSnake.Enqueue(pos);
                        }
                        snake = newSnake;
                    }
                    if (cell[headX, headY] == FREE)
                    {
                        Queue <position> newSnake = new Queue <position>();
                        newSnake.Enqueue(HeadPosition);
                        int c = snake.Count();
                        foreach (position pos in snake)
                        {
                            if (c-- > 1)      // add all but last one
                            {
                                newSnake.Enqueue(pos);
                            }
                        }
                        snake = newSnake;
                    }
                    return(true);
                }
                else
                {
                    return(false);
                }
                break;

            case LEFT:
                if ((headX > 0) && ((cell[headX - 1, headY] == FREE) || (cell[headX - 1, headY] == APPLE)))
                {
                    cell[headX - 1, headY + 1] = BODY;
                    position HeadPosition = new position();
                    HeadPosition.x = headX--;
                    HeadPosition.y = headY;

                    //C# does not have a insert(0,xxx) method or push_front method, so have to hack like this
                    if (cell[headX, headY] == APPLE)
                    {
                        Queue <position> newSnake = new Queue <position>();
                        newSnake.Enqueue(HeadPosition);
                        foreach (position pos in snake)
                        {
                            newSnake.Enqueue(pos);
                        }
                        snake = newSnake;
                    }
                    if (cell[headX, headY] == FREE)
                    {
                        Queue <position> newSnake = new Queue <position>();
                        newSnake.Enqueue(HeadPosition);
                        int c = snake.Count();
                        foreach (position pos in snake)
                        {
                            if (c-- > 1)      // add all but last one
                            {
                                newSnake.Enqueue(pos);
                            }
                        }
                        snake = newSnake;
                    }
                    return(true);
                }
                else
                {
                    return(false);
                }
                break;
            }
            return(false);
        }