没有任何人有C语言编写的一个工作的非递归算法此时,floodFill?递归、算法、语言、工作

2023-09-10 23:29:20 作者:- 旧人旧事旧心酸 .

在过去的几天我一直在拼命寻找工作此时,floodFill算法。在众多的算法我已经试过只有递归线填充'一的行为正是因为它要与主要的警告,它偶尔吹堆栈。 :(

over the last few days I've been desperately trying to find a working floodfill algorithm. Of the many algorithms i've tried only the 'recursive line fill' one behaves exactly as it should with the major caveat that it occasionally blows the stack. :(

我已经尝试了许多非递归实现,我在不同的网站上找到,他们已经全部格外tempermental:他们要么留在陌生的地方的差距,或者填满整个区域(当它们应该被封闭)。我也试着我的手在实施其中的一些算法自己,但成果有限。我在点现在在哪里,我真的只是需要一些工作的源$ C ​​$ C!如果任何人有一个非递归此时,floodFill写在C(或C ++,是不是太沉重的OOP,我可以理清很轻松了)可以请他们就与我分享?我在舞台上,现在我需要它相当迫切,当然cannnot花了时间在这。 :(

I have tried many non-recursive implementations I've found on various websites and they have all been exceptionally tempermental: either they leave gaps in strange places, or flood the whole area (when they should be enclosed). I have also tried my hand at implementing a few of these algorithms myself but with limited success. I am at the point now where i really just need some working sourcecode! If anyone has a NON-recursive floodfill written in C (or c++ that isn't too heavily OOP and i can disentangle easily enough) can they PLEASE share it with me? I am at the stage now where I need it fairly urgently and certainly cannnot spend anymore time on this. :(

抱歉,是一个白痴,并要求源$ C ​​$ C,但我已经打了一个有点穷途末路的。

sorry to be a dork and ask for the sourcecode but I've hit a bit of a dead-end.

在此先感谢,如果你能帮助!

Thanks in advance if you can help!

推荐答案

下面是一些C ++ code,做你想要的。它使用一个队列,且为约插入到队列中更有效。

Here's some C++ code that does what you want. It uses a queue, and is more efficient about insertions into the queue.

connectedRegion(const Point& source, RegionType& region, const Color target)
{
    Color src_color = color_of(source, region);
    if (region.count(source) == 0 || src_color == target)
    	return;
    std::queue<Point> analyze_queue;
    analyze_queue.push(source);

    while (!analyze_queue.empty())
    {
    	if (color_of(analyze_queue.front()) != src_color)
    	{
    		analyze_queue.pop();
    		continue;
    	}
    	Point leftmost_pt = analyze_queue.front();
            leftmost_pt.col -= 1;
    	analyze_queue.pop();
    	Point rightmost_pt = leftmost_pt;
            rightmost_pt.col += 2;
    	while (color_of(leftmost_pt, region) == src_color)
    		--leftmost_pt.col;

    	while (color_of(rightmost_pt, region) == src_color)
    		++rightmost_pt.col;

    	bool check_above = true;
    	bool check_below = true;
            Point pt = leftmost_pt;
            ++pt.col;
    	for (; pt.col < rightmost_pt.col; ++pt.col)
    	{
    		set_color(pt, region, target);

    		Point pt_above = pt;
                    --pt_above.row;
    		if (check_above)
    		{
    			if (color_of(pt_above, region) == src_color)
    			{
    				analyze_queue.push(pt_above);
    				check_above = false;
    			}
    		}
    		else // !check_above
    		{
    			check_above = (color_of(pt_above, region) != src_color);
    		}

    		Point pt_below = pt;
                    ++pt_below.row;
    		if (check_below)
    		{
    			if (color_of(pt_below, region) == src_color)
    			{
    				analyze_queue.push(pt_below);
    				check_below = false;
    			}
    		}
    		else // !check_below
    		{
    			check_below = (color_of(pt_below, region) != src_color);
    		}
    	} // for 
    } // while queue not empty
    return connected;
}