题目大意
解题思路
- 遇到一个
W
,ans++
并且利用深搜将该联通区域的所有W
去掉。
代码
#include<iostream>
using namespace std;
const int MAX = 105;
char grad[MAX][MAX];
int ans;
int N, M;
bool out_of_edge(int x, int y)
{
if(x < 0 || x >= N || y < 0 || y >= M)
return true;
return false;
}
int dir[8][2] = {{1,0},{-1,0},{0,1},{0,-1},{1,1},{-1,-1},{-1,1},{1,-1}};
void dfs(int x, int y)
{
grad[x][y] = '.';
for(int i=0; i<8; i++)
{
int nx = x + dir[i][0];
int ny = y + dir[i][1];
if(out_of_edge(nx, ny))
continue;
if(grad[nx][ny] == '.')
continue;
dfs(nx, ny);
}
return;
}
int main()
{
cin >> N >> M;
for(int i=0; i<N; i++)
for(int j=0; j<M; j++)
cin >> grad[i][j];
ans = 0;
for(int i=0; i<N; i++)
{
for(int j=0; j<M; j++)
{
if(grad[i][j] == 'W')
{
ans++;
dfs(i, j);
}
}
}
cout << ans << endl;
return 0;
}