title: Flood fill algorithm in c++
body: <p>I'm trying to write code that will do a flood fill of a color on an image, and I'm using Stacks. (Still new to stacks and queues, so I'm not sure which one would be a better idea). Anyway, I think I got the basic idea down, but there's a flaw with my code:</p>
  
  <pre><code>animation DFSfill(BMP&amp; img, int x, int y, colorPicker &amp; fillColor, int tolerance, int frameFreq) {
  
  
  Stack&lt;RGBApixel&gt; s;
  s.push(*img(x,y));
  int actualTol;
  int height, width;
  width=img.TellWidth();
  height=img.TellHeight();
  int counter=1;
  animation finalAnimation;
  RGBApixel top;
  
  bool visited[width][height];
  for(int i=0;i&lt;width;i++)
      for(int j=0;j&lt;height;j++)
          visited[x][y]=false;
  
  
  
  while(!s.isEmpty()){
      top = s.peek();
      s.pop();
  
      actualTol=(top.Red-fillColor(x,y).Red)^2
  +(top.Blue-fillColor(x,y).Blue)^2+(top.Green-fillColor(x,y).Green)^2;
  
      //check 
      if(x&lt;0 || x&gt;=width)
          continue;
      if(y&lt;0 || y&gt;=height)
          continue;
      if (visited[x][y]==true)
          continue;
      if(actualTol&gt;tolerance)
          continue;
  
      visited[x][y]=true;
      img(x,y)-&gt;Red=fillColor(x,y).Red;
      img(x,y)-&gt;Blue=fillColor(x,y).Blue;
      img(x,y)-&gt;Green=fillColor(x,y).Green;
      counter++;
  
      //visit adjacent nodes
  
  
      s.push(*img(x+1, y));//right
  
      s.push(*img(x, y-1));//down
  
      s.push(*img(x-1, y));//left
  
      s.push(*img(x, y+1));//up
  
      if((counter%frameFreq)==0)
          finalAnimation.addFrame(img);
  }
  
  return finalAnimation;  
  </code></pre>
  
  <p>}</p>
  
  <p>So I think the problem, or to me it seems at least, is that when visiting adjacent nodes, I am visiting the same nodes every loop, right? Whats a workaround to this?</p>