2010-09-27 98 views
0

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

我不知道我的代码中有什么部分出现问题,因为堆栈溢出发生在所有地方,但我会发布排队和排队的一部分。

所以是的,基本上在第二次循环后,它开始搞乱一切,它增加了超过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#中

在此先感谢

回答

0

首先,你没有指定什么是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()方法取决于您是否使用具有矩阵或邻接列表的表示。无论如何,很简单,如果你需要知道如何去做,请给我一个味精。

我没有测试的代码,但是我非常肯定它的工作原理(原谅任何一些语法问题!)

希望我能帮助 祝您好运! DvD

+0

非常感谢,我会尝试这个并发回。而我的“Nodo”类只是我制作的一个通用节点类。 – Jadager 2010-09-27 02:31:38

0

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

+0

嗯,我确定如果没有队列会更容易,但BFS使用队列,所以我想这样做。 – Jadager 2010-09-27 02:27:32

+0

对不起,我的意思是.NET队列类(http://msdn.microsoft.com/en-us/library/7977ey2c.aspx)。 – 2010-09-27 06:55:33