foodfill 八连块问题

Description

Due to recent rains, water has pooled in various places in Farmer John‘s field, which is represented by a rectangle of N x M (1 <= N <= 100; 1 <= M <= 100) squares. Each square contains either water (‘W‘) or dry land (‘.‘). Farmer John would like to figure out how many ponds have formed in his field. A pond is a connected set of squares with water in them, where a square is considered adjacent to all eight of its neighbors.

Given a diagram of Farmer John‘s field, determine how many ponds he has.

Input

* Line 1: Two space-separated integers: N and M

* Lines
2..N+1: M characters per line representing one row of Farmer John‘s
field. Each character is either ‘W‘ or ‘.‘. The characters do not have
spaces between them.

Output

* Line 1: The number of ponds in Farmer John‘s field.

Sample Input

10 12
W........WW.
.WWW.....WWW
....WW...WW.
.........WW.
.........W..
..W......W..
.W.W.....WW.
W.W.W.....W.
.W.W......W.
..W.......W.

Sample Output

3

Hint

OUTPUT DETAILS:

There are three ponds: one in the upper left, one in the lower left,and one along the right side.

解题思路:

此题与二叉树遍历类似,图也有DFS和BFS遍历,这个问题用的是DFS找连通块:从每个"W“格子出发,递归遍历它周围的”W"格子。每次访问一个格子时就给它写上一个“连通分量编号”即下面代码的q数组“,这样就可以在访问之前检查它是否已经有了编号,从而避免同一个格子重复访问。

下面代码是用一个二重循环来找到当前格子的相邻8个格子,也可以用常量数组或者写8条调用语句,

程序代码:

#include <cstdio>
#include <cstring>
using namespace std;

const int maxn=100+5;
char p[maxn][maxn];
int m,n,q[maxn][maxn];

void fld(int i,int j,int w)
{
    if(i<0||i>=m||j<0||j>=n)    return ;
    if(q[i][j]>0||p[i][j]!=‘W‘)  return;
    q[i][j]=w;
    for(int r=-1;r<=1;r++)
        for(int c=-1;c<=1;c++)
            if(c==0&&r==0)  continue;
            else  fld(r+i,j+c,w);

}
int main()
{
    while(scanf("%d%d",&m,&n)==2&&m&&n)
    {
        for(int i=0;i<m;i++)    scanf("%s",p[i]);
        memset(q,0,sizeof(q));
        int cf=0;
        for(int i=0;i<m;i++)
            for(int j=0;j<n;j++)
            if(q[i][j]==0&&p[i][j]==‘W‘)    fld(i,j,++cf);
        printf("%d\n",cf);
    }
    return 0;
}
时间: 2024-08-05 05:12:04

foodfill 八连块问题的相关文章

CNUOJ 0079 20612统计八连块

20612统计八连块 难度级别:B: 运行时间限制:1000ms: 运行空间限制:51200KB: 代码长度限制:2000000B 试题描述 输入一个m行n列的字符矩阵,统计字符“@”组成多少个八连块.如果两个字符“@”所在的格子相邻(横竖或者对角线方向),就说它们属于同一个八连块.例如,下图中有两个八连块.                         输入 第一行包括两个正整数m和n,由空格隔开,接下来的m行,每行n个字符,字符只包括“*”和“@”. 输出 一个自然数,表示八连块的个数.

20612统计八连块

50237242海岛帝国:神圣之日 [试题描述] 输入一个m行n列的字符矩阵,统计字符“@”组成多少个八连块.如果两个字符“@”所在的格子相邻(横竖或者对角线方向),就说它们属于同一个八连块.例如,下图中有两个八连块. [输入要求] 第一行包括两个正整数m和n,由空格隔开,接下来的m行,每行n个字符,字符只包括“*”和“@”. [输出要求] 一个数,表示八连块的个数 [输入实例] 5 5 ****@ *@@*@ *@**@ @@@*@ @@**@ [输出实例] 2 [其他说明] 数据范围:0<m

黑白图像—————— 八连块

八连块 [题目]:              输入一个n*n的黑白图像(1表示黑色,0表示白色),任务是统计其中八连块的个数.如果两个黑格子有        公共边或者公共顶点,就说它们属于同一个八连块.如下图6-11表示3个八连块. [分析]             通过题目要求,定义8方向数组,运用搜索来确定有几个八连块,搜索查找,     查找过的进行标记,保证不会重复查找到原来的黑格子,另外还要考虑边界问           题,但是下面我的方法就不用考虑边界问题,认真看下面就知道方法了

紫书p199 八连块(BFS,hash)

八连块问题  紫书上的简单搜索  渣渣好久才弄懂 #include<cstdio> #include<cstring> using namespace std; const int M = 1000003; int x[4] = { -1, 1, 0, 0}, y[4] = {0, 0, -1, 1}; int dis[M], h[M], s[M][9], e[9]; int aton(int a[]) { int t = 0; for(int i = 0; i < 9; +

八连块

1 #include <bits/stdc++.h> 2 using namespace std; 3 4 const int MAX = 100; 5 char arr[MAX][MAX]; 6 int n,m; 7 8 void init(); 9 void solve(); 10 void dfs(int x,int y); 11 12 void init(){ 13 cin>>n>>m;//输入行数列数 14 for(int i=0;i<n;i++){ 1

八连块问题:统计图中相相连黑色区域的个数

一个n*n个方块的图,每个方块是白色或者黑色.统计出相连的黑色块区域有多少个 相连:两个黑色块有公共顶点或者公共边输入0代表白色 1代表黑色 测试用例: 1 0 0 1 0 0 0 0 1 0 1 0 0 0 0 0 0 0 1 1 0 0 0 0 1 1 1 0 0 0 0 1 0 1 0 0 答案: 有三块相连的黑色区域 python实现: 1 ''' 2 对每方块进行遍历: 3 1 如果值是0或者-1 不进行操作 4 2 如果值是1 把值改为-1 标记一下 已经遍历过这个点 5 2.2 把

八连块问题

问题描述:共有多少块相邻的空白的区域 注意递归和边界问题的值的设置 源代码如下: #include<stdio.h> #include<stdlib.h> #include<string.h> #define MAX 100 +10 using namespace std; int mat[MAX][MAX] ,vis[MAX][MAX]; void dfs(int x,int y) { if(!mat[x][y] || vis[x][y]) //1代表空白 retur

UVA 572 Oil Deposits油田(DFS求连通块)

UVA 572     DFS(floodfill)  用DFS求连通块 Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Description Due to recent rains, water has pooled in various places in Farmer John's field, which is represented by a rectangle of N x M

DFS入门之二---DFS求连通块

用DFS求连通块也是比较典型的问题, 求多维数组连通块的过程也称为--“种子填充”. 我们给每次遍历过的连通块加上编号, 这样就可以避免一个格子访问多次.比较典型的问题是”八连块问题“.即任意两格子所在位置相邻(上下左右对角共八个方位),则在一个连通块.典型例题:HDU 1241 Oil Deposits 传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1241 题目描述:输入m行n列的字符矩阵, 统计字符“@”组成八连块的个数. 题意分析:读入数据