Skip to content

Instantly share code, notes, and snippets.

@kenjiSpecial
Created May 26, 2020 00:44
Show Gist options
  • Save kenjiSpecial/48e0f808b9346a1ebd7c9cf341b3609e to your computer and use it in GitHub Desktop.
Save kenjiSpecial/48e0f808b9346a1ebd7c9cf341b3609e to your computer and use it in GitHub Desktop.
Lake Counting
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
const int INF = 1001001001;
void dfs(vector<vector<string>> &vect, int x, int y, int N, int M) {
vect[x][y] = ".";
for (int dx = -1; dx <= 1; dx++) {
for (int dy = -1; dy <= 1; dy++) {
int nx = x + dx;
int ny = y + dy;
if (0 <= nx && nx < N && 0 <= ny && ny < M) {
if (vect[nx][ny] == "W") {
dfs(vect, nx, ny, N, M);
}
}
}
}
}
int main() {
int N;
int M;
cin >> N >> M;
vector<vector<string>> data(N, vector<string>(M));
// rep(i, vect.size()) cout << vect[i] << endl;
rep(i, N) {
string inputS;
cin >> inputS;
rep(j, M) { data.at(i).at(j) = inputS.at(j); }
}
int res = 0;
rep(i, N) {
rep(j, M) {
if (data.at(i).at(j) == "W") {
dfs(data, i, j, N, M);
res++;
}
}
}
rep(i, N) {
rep(j, M) { cout << data[i][j]; }
}
cout << res << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment