【问题标题】:Problem with Queue队列问题
【发布时间】:2010-09-27 01:40:01
【问题描述】:

我正在尝试使用 BFS 算法制作一个程序,因此我将每个节点放入队列中,一旦每个级别都在队列中,我开始将其与另一个节点进行比较以查看它们是否相等,但是问题我的问题是我的队列中的元素正在被修改,所以当我执行 Dequeue 时,我从来没有得到答案,而且我遇到了堆栈溢出。

我不确定我的代码的哪一部分有问题,因为堆栈溢出到处都是,但我会发布队列和出队的一部分。

所以是的,基本上在第二个循环之后它开始搞砸一切,它向我的矩阵添加了超过 1 个“b”并修改了我的队列元素。

private void BFS(Nodo<string[,]> nodo)
{
  Array.Copy(nodo.getEA(), datos5, 9);
  temp = null;
  temp2 = null;
  temp3 = null;
  temp4 = null;
  Array.Copy(datos5, datos, 9);
  //There are a bunch of if and else if so I just posted one
  if (datos[1, 0].Equals("b"))
  {
    Array.Copy(datos, datos2, 9);
    Array.Copy(datos, datos3, 9);
    cont3=3;
    //UP from 1,0 a 0,0
    datos[1, 0] = datos[0, 0];
    datos[0, 0] = "b";
    temp = new Nodo<string[,]>(datos);
    temp.setCamino("U");
    temp.setPadre(nodo);
    myq.Enqueue(temp);
    temp = null;
    //Right from 1,0 a 1,1
    datos2[1, 0] = datos2[1, 1];
    datos2[1, 1] = "b";
    temp2 = new Nodo<string[,]>(datos2);
    temp2.setCamino("R");
    temp2.setPadre(nodo);
    myq.Enqueue(temp2);
    temp = null;
    //Down from 1,0 a 2,0
    datos3[1, 0] = datos3[2, 0];
    datos3[2, 0] = "b";
    temp3 = new Nodo<string[,]>(datos3);
    temp3.setCamino("D");
    temp3.setPadre(nodo);
    myq.Enqueue(temp3);
    fila();
   }

}

private void fila()
{     
  Nodo<string[,]> temp5;
  for (int i = 0; i < myq.Count; i++)
  {
    temp5 = null;
    temp5 = (Nodo<string[,]>)myq.Dequeue();
    if (objetivo(temp5, nodof))
    {
      if (!flag2)
      {
        boxResultado.AppendText("Problem solved");
        flag2 = true;
        break;
      }
      else
      {
        break;
      }          
    }
    else
    {
      if (!flag2)
      {
        BFS(temp5);
      }
      else
      {
        break;
      }
    }
  }
}
private bool objetivo(Nodo<string[,]> p, Nodo<string[,]> e)
{
  nodo1 = null;
  nodo2 = null;
  bool flag = false;
  nodo1 = p.getEA();
  nodo2 = e.getEA();
  for (int i = 0; i < 3; i++)
  {
    for (int f = 0; f < 3; f++)
    {
      if (nodo1[i, f] != nodo2[i, f])
      {
        flag = true;
      }
    }
  }
  if (flag)
  {
    return false;
  }
  else
  {
    return true;
  }
}

我知道我的代码非常糟糕,但在过去 5 个小时左右我一直在试图找出这个问题,所以我一直在到处修改,试图找出问题所在,但我感到非常沮丧所以我决定在这里寻求帮助。 顺便说一句,这是在 C# 中

提前致谢

【问题讨论】:

    标签: c# queue


    【解决方案1】:

    首先,您没有指定 Nodo 类的作用。无论如何,BFS 非常简单。我会告诉你一般算法。假设您在图中有一个 NxN 邻接矩阵,您将有一个大小为 N 的数组,其中包含访问过的节点的标记。初始代码是

    class Graph{
    
        //matrix[i,j] is true if there is a node from i to j
        bool[,] matrix;
    
        private void BFS( int startNode )
        {
            int n = matrix.GetLength(0);
            bool [] marks = new bool[n];
    
            Queue<int> nodes = new Queue();
            nodes.Enqueue(startNode);
    
            while ( !nodes.Empty() )
            {
                int node = nodes.Dequeue();
    
                //set visited
                marks[node] = true;
    
                List<int> adjs = GetAdyacents(node);
    
                foreach ( int adjacent in adjs ){
                    if ( !mark[adjacent] )
                        nodes.Enqueue(adjacent);
                }
    
                Console.WriteLine("Visiting {0}", node);
            }
        }
    
    }
    

    GetAdjacent() 方法取决于您使用的是矩阵表示还是邻接表。反正很简单,如果你需要知道怎么做,给我留言。

    我还没有测试过代码,但我很确定它可以工作(请原谅任何一些语法问题!)

    希望我能帮上忙 祝你好运! 影碟

    【讨论】:

    • 非常感谢,我会试试这个并回帖。而我的“Nodo”类只是我制作的一个通用节点类。
    【解决方案2】:

    我没有深入了解您的代码,但我想知道您是否会从使用 Queue 中受益。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-15
    • 2011-04-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多