Skip to content

Instantly share code, notes, and snippets.

@whatalnk
Created May 8, 2017 00:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save whatalnk/ea8e1a97ff1acefe9fce42fb235f60be to your computer and use it in GitHub Desktop.
Save whatalnk/ea8e1a97ff1acefe9fce42fb235f60be to your computer and use it in GitHub Desktop.
アリ本 2-1 p35 Lake Counting
#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