洛谷 P1596 [USACO10OCT]Lake Counting S
https://www.luogu.com.cn/problem/P1596
很水的一题,我比较菜,不能很快的写出来。
每次遇到W的时候,就搜索一下,把搜到的W全部变成" . ",一共搜索了几次,就是有多少的水坑。
#include <bits/stdc++.h>
using namespace std;
const int N = 110;
char g[N][N];
int n, m;
bool vis[N][N];
int dx[8] = {-1, -1, -1, 0, 1, 1, 1, 0};
int dy[8] = {-1, 0, 1, 1, 1, 0, -1, -1};
int res = 0;
void dfs(int x, int y)
{
g[x][y] = '.';
for (int i = 0; i < 8 ; i ++ )
{
int tx = dx[i] + x, ty = dy[i] + y;
if (tx < 0 || tx > n || ty < 0 || ty > m) continue;
if (g[tx][ty] == 'W')
{
dfs(tx, ty);
}
}
return;
}
int main()
{
cin >> n >> m;
for (int i = 0; i < n ; i ++ )
{
for (int j = 0; j < m ; j ++ )
{
cin >> g[i][j];
}
}
for (int i = 0; i < n ; i ++ )
{
for (int j = 0; j < m ; j ++ )
{
if (g[i][j] == 'W')
{
dfs(i, j);
res ++ ;
}
}
}
cout << res << endl;
return 0;
}
继续学啊,这都不会,完了呀!