Created
May 8, 2017 00:22
-
-
Save whatalnk/ea8e1a97ff1acefe9fce42fb235f60be to your computer and use it in GitHub Desktop.
アリ本 2-1 p35 Lake Counting
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <algorithm> | |
#include <string> | |
using namespace std; | |
const int MAX_N = 100; | |
const int MAX_M = 100; | |
int N, M; | |
char field[MAX_N][MAX_M + 1]; | |
void dfs(int x, int y) | |
{ | |
field[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 && field[nx][ny] == 'W') | |
dfs(nx, ny); | |
} | |
} | |
return; | |
} | |
void solve() | |
{ | |
int res = 0; | |
for (int i = 0; i < N; i++) | |
{ | |
for (int j = 0; j < M; j++) | |
{ | |
if (field[i][j] == 'W') | |
{ | |
dfs(i, j); | |
res++; | |
} | |
} | |
} | |
cout << res << endl; | |
} | |
int main() | |
{ | |
ios::sync_with_stdio(false); | |
cin >> N >> M; | |
std::string s; | |
for (int i = 0; i < N; i++) | |
{ | |
cin >> s; | |
for (int j = 0; j < M; j++) | |
{ | |
field[i][j] = s[j]; | |
} | |
} | |
solve(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment