As in the link there was a more efficient solution than the recursion
http://en.wikipedia.org/wiki/Flood_fill
Flood-fill (node, target-color, replacement-color):
1. If target-color is equal to replacement-color, return.
2. Set Q to the empty queue.
3. Add node to the end of Q.
4. While Q is not empty:
5. Set n equal to the last element of Q.
6. Remove last element from Q.
7. If the color of n is equal to target-color:
8. Set the color of n to replacement-color.
9. Add west node to end of Q.
10. Add east node to end of Q.
11. Add north node to end of Q.
12. Add south node to end of Q.
13. Return.
This is my first attempt at transforming the pseudo code to C++:
std::deque<char> myqueue;
void Region(int x,int y,char newC,char c)
{
char n;
if(c==newC)return;
myqueue.empty();
myqueue.push_back(arr[y][x]);
while (myqueue.size()!=0)
{
n=myqueue.back();
myqueue.pop_back();
if(n==c)
{
n=newC;
if(x-1>0) myqueue.push_back(arr[y][x-1]);
if(y-1>0) myqueue.push_back(arr[y-1][x]);
if(x+1<=M) myqueue.push_back(arr[y][x+1]);
if(y+1<=N) myqueue.push_back(arr[y+1][x]);
}
}
}