题目:
题解:
#include<stdio.h>
int n, m;
char arr[110][110]; //元数据数组
int count = 0; //计数器
int dx[8] = {1,1,1,-1,-1,-1,0,0};
int dy[8] = {-1,0,1,-1,0,1,1,-1};
int t[110][110]; //判断是否被选择
void dfs(int x, int y)
{
for (int i = 0; i < 8; i++)
{
int a = x + dx[i], b = y + dy[i];
if (a<0 || a>n || b<0 || b>m || t[a][b] || arr[a][b] != 'W')
{
continue;
}
t[a][b] = 1;
dfs(a,b);
}
}
int main()
{
scanf("%d %d", &n, &m);
for (int i = 0; i < n; i++)
{
scanf("%s", arr[i]);
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
if (arr[i][j] == 'W'&&t[i][j]==0) //寻找符合条件且未被选择的空间
{
dfs(i, j);
count++;
}
}
}
printf("%d", count);
return 0;
}
标签:count,arr,int,++,dfs,Lake,Flood,110,Counting From: https://blog.csdn.net/2301_79580018/article/details/137182056