Note that there are some explanatory texts on larger screens.

plurals
  1. POProblem with Queue
    primarykey
    data
    text
    <p>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.</p> <p>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.</p> <p>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.</p> <pre><code>private void BFS(Nodo&lt;string[,]&gt; 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&lt;string[,]&gt;(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&lt;string[,]&gt;(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&lt;string[,]&gt;(datos3); temp3.setCamino("D"); temp3.setPadre(nodo); myq.Enqueue(temp3); fila(); } </code></pre> <p>}</p> <pre><code>private void fila() { Nodo&lt;string[,]&gt; temp5; for (int i = 0; i &lt; myq.Count; i++) { temp5 = null; temp5 = (Nodo&lt;string[,]&gt;)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&lt;string[,]&gt; p, Nodo&lt;string[,]&gt; e) { nodo1 = null; nodo2 = null; bool flag = false; nodo1 = p.getEA(); nodo2 = e.getEA(); for (int i = 0; i &lt; 3; i++) { for (int f = 0; f &lt; 3; f++) { if (nodo1[i, f] != nodo2[i, f]) { flag = true; } } } if (flag) { return false; } else { return true; } } </code></pre> <p>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#</p> <p>Thanks in advance</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload