Lake Counting
#include "stdafx.h" #include<iostream> #define MAX 500 using namespace std; char imap[MAX][MAX]; int n, m; void dfs(int x, int y) { imap[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 (nx >= 0 && nx <= n&&ny >= 0 && ny <= m&&imap[nx][ny] == 'W') dfs(nx, ny); } } int solve() { int res = 0; for (int i = 0; i < n; i++) for (int j = 0; j < m;j++) if (imap[i][j] == 'W') { dfs(i, j); res++; } return res; } int main() { while (cin >> n >> m) { for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) cin >> imap[i][j]; cout << solve() << endl; } }
版权声明:本文为博主原创文章,未经博主允许不得转载。