Note that there are some explanatory texts on larger screens.

plurals
  1. PORecursive floodFill function? C++
    text
    copied!<p>So recursion is not my strong point, and I have been challenged to make a recursive floodFill function that fills a vector of a vector of ints with 1's if the value is zero. My code keeps segfaulting for reasons beyond me. Perhaps my code will make that sound more clear. </p> <p><strong>This is the grid to be flood filled:</strong></p> <pre><code>vector&lt;vector&lt;int&gt; &gt; grid_; </code></pre> <p>It belongs to an object I created called "Grid" that is basically a set of functions to help manipulate the vectors. The grid's values are initialized to all zeros. </p> <p><strong>This is my flood fill function:</strong></p> <pre><code>void floodFill(int x, int y, Grid &amp; G) { if (G.getValue(x,y)) { G.setValue(x,y,1); if(x &lt; G.getColumns()-1 &amp;&amp; x &gt;= 0 &amp;&amp; y &lt; G.getRows()-1 &amp;&amp; y &gt;= 0) floodFill(x+1,y,G); if(x &lt; G.getColumns()-1 &amp;&amp; x &gt;= 0 &amp;&amp; y &lt; G.getRows()-1 &amp;&amp; y &gt;= 0) floodFill(x,y+1,G); if(x &lt; G.getColumns()-1 &amp;&amp; x &gt;= 0 &amp;&amp; y &lt; G.getRows()-1 &amp;&amp; y &gt;= 0) floodFill(x-1,y,G); if(x &lt; G.getColumns()-1 &amp;&amp; x &gt;= 0 &amp;&amp; y &lt; G.getRows()-1 &amp;&amp; y &gt;= 0) floodFill(x,y-1,G); } } </code></pre> <p>The intention here is to have the function check if a point's value is zero, and if it is, change it to one. Then it should check the one above it for the same. It does this until it either finds a 1 or hits the end of the vector. Then it tries another direction and keeps going until the same conditions as above and so on and so forth until its flood filled. </p> <p>Can anyone help me fix this? Maybe tell me whats wrong?</p> <p>Thanks!</p>
 

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