栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

用C语言编写的有效非递归泛洪算法?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

用C语言编写的有效非递归泛洪算法?

这是一些可以满足您需求的C ++代码。它使用队列,并且在插入队列中效率更高。

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;}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/637491.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号