Flood fill种子填充(DFS求连通快)
2015-03-27 21:04
357 查看
Flood fill算法是从一个区域中提取若干个连通的点与其他相邻区域区分开(或分别染成不同颜色)的经典算法。因为其思路类似洪水从一个区域扩散到所有能到达的区域而得名。算法分为四路算法(不考虑对角线方向的节点)和八路算法(考虑对角线方向的节点)。
例题:HDOJ 1241 Oil Deposits
例题:HDOJ 1241 Oil Deposits
#include<cstdio> #include<cstring> int n, m; char map[102][102]; int mark[102][102]; int dis[8][2] = { { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 }, { 1, -1 }, { -1, 1 }, { 1, 1 }, { -1, -1 } }; void bfs(int x, int y, int sum) { mark[x][y] = 1; int tx, ty,i; for (i = 0; i < 8; i++) { tx = x + dis[i][0]; ty = y + dis[i][1]; if (tx >= 0 && tx < n&&ty >= 0 && ty < m&&!mark[tx][ty]&&map[tx][ty]=='@') bfs(tx, ty, sum); } } int main() { int i, j, ans; while (scanf("%d %d", &n, &m), n + m) { memset(mark, 0, sizeof(mark)); ans = 0; for (i = 0; i < n; i++) scanf("%s", map[i]); for (i = 0; i < n;i++) for (j = 0; j < m; j++) { if (map[i][j] == '@'&&!mark[i][j]) bfs(i, j, ++ans); } printf("%d\n", ans); } return 0; }
相关文章推荐
- UVA572 Oil Deposits(DFS求连通块(floodfill 种子填充))
- UVa852 - Deciding victory in Go(DFS,种子填充法)
- UVA 784 Maze Exploration (DFS || 种子填充)
- 二值图像连通区域标记:Seed Filling(种子填充法)
- Uva572种子填充<dfs>
- UVa 572 Oil Desposits (dfs 图 染色 八连通种子填充)
- opengl 区域填充之种子填充线扫描法
- 算法系列之十二:多边形区域填充算法--扫描线种子填充算法 .
- 图遍历算法——DFS、BFS、A*、B*和Flood Fill 遍历算法串串烧
- 计算机图形学(四)多边形的种子填充算法讲解与源代码
- poj 2386 Lake Counting(搜索,种子填充法)
- 多边形区域填充算法一--递归种子填充
- Android多边形区域递归种子填充算法的示例代码
- CDOJ 1965 连通域统计【DFS】
- 8天掌握EF的Code First开发系列之3 管理数据库创建,填充种子数据以及LINQ操作详解
- Open gl 的不规则图形的4联通种子递归填充和扫描线种子递归填充算法实现
- 搜索入门(DFS)-Lake Counting(积水连通问题)
- ZOJ1709 || POJ 1562 DFS 水 求强连通的个数
- DFS or BFS --- 连通块