title: Flood Fill Optimization: Attempting to Using a Queue
body: <p>I am trying to create a filling method which takes the initial coordinate specified by the user, checks the character and then changes it as desired. After doing so, it then checks adjacent squares and repeats the process. After some research, I came across the flood fill algorithm and after trying that (it works but cannot meet my maximum requirement of an array of 250 by 250 characters). </p>
  
  <p>My original flood fill algorithm was as follows:</p>
  
  <pre><code>public static void fillArea (int x, int y, char original, char fill){
  
     if (picture[y-1][x-1] != original){
        return;
     }
     picture[y-1][x-1] = fill;
     fillArea (x-1, y, original, fill);
     fillArea (x+1, y, original, fill);
     fillArea (x, y-1, original, fill);
     fillArea (x, y+1, original, fill);
  
     return;
  }
  </code></pre>
  
  <p>After testing, I began to try the Queue method as explained on <a href="http://en.wikipedia.org/wiki/Flood_fill#Alternative_implementations" rel="nofollow noreferrer">Wikipedia</a> and <a href="https://stackoverflow.com/questions/2783204/flood-fill-using-a-stack">this question asked previously</a> regarding a similar issue. So far, I have come up with the following code:</p>
  
  <pre><code>public static void fillArea (int x, int y, char original, char fill){
    if (x != 0)
       x--;
    if (y!= 0)
       y--;
    Queue&lt;Point&gt; queue = new LinkedList&lt;Point&gt;();
    if (picture[y][x] != original){
        return;
    }
    queue.add(new Point(x, y));
  
    while (!queue.isEmpty()){
        Point p = queue.remove();
        if (picture[p.y][p.x] == original){
           picture[p.y][p.x] = fill;
           queue.add(new Point(p.x-1, p.y));
           queue.add(new Point(p.x+1, p.y));
           queue.add(new Point(p.x, p.y-1));
           queue.add(new Point(p.x, p.y+1));
        }
     }
  
     return;
  }
  </code></pre>
  
  <p>While the first, recursive method was able to work for smaller values, it does not work when the array gets too large. However, the second method is taking me into some sort of infinite loop that I am unable to identify due to my inexperience with Queues. How can I either optimize the first code sample to work with larger samples or fix the second so that it gives results? Can anyone explain how to code any of these two or what I am doing wrong?</p>
  
  <p>Thanks!</p>
  
  <p>EDIT: I was able to find the infinite loop error in the second code (it has been fixed) but the queue based method does not fill the complete area. In fact, it fills less of an area than the recursion-based method.
  EDIT 2: It now works for square arrays. How can I ensure that it works for rectangular arrays as well (the second method).</p>