Hi,
I'm trying to make a program using the BFS algorithm so I put every node in a Queue and once every level is in the Queue I start comparing it to another node to see if they are equal or not, however the problem I'm having is that elements in my Queue are being modified so when I do the Dequeue I never get to an answer and I'm getting a stack overflow.
I'm not sure what part of my code had the problem since I the stack overflow happens all over the place, but I'll post a part of the Queuing and the Dequeuing.
So yeah basically after the second loop it starts messing up everything, it adds more than 1 "b" to my matrix and it modifies my Queue elements.
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;
}
}
I know my code is pretty horrible but I've been trying to figure out that problem for the past 5 hours or so so I've been modifying here and there trying to locate the problem, but I'm getting pretty frustrated so I decided to ask here for help. Btw this is in C#
Thanks in advance