Lake counting
Time Limit: 1000MS |
|
Memory Limit: 65536K |
Total Submissions: 22736 |
|
Accepted: 11457 |
Description
Due to recent rains, water have pooled in various places in Farmer John ' s field, which was represented by a rectangle of N x M (1 <= N <=; 1 <= M <=) squares. Each square contains the either water (' W ') or dry land ('. '). Farmer John would like to figure out how many ponds has formed in his field. A pond is a connected set of squares with water in them, where a square was considered adjacent to all eight of its NEIGHBO Rs.
Given a diagram of Farmer John ' s field, determine how many ponds he had.
Input
* Line 1:two space-separated integers:n and M
* Lines 2..n+1:m characters per line representing one row of Farmer John ' s field. Each character is either ' W ' or '. '. The characters does not have spaces between them.
Output
* Line 1:the number of ponds in Farmer John ' s field.
Sample Input
12W ..... Ww.. WWW.....WWW....WW...WW..........WW..........W....W......W...W.W.....WW.W.W.W.....W..W.W......W...W.......W.
Sample Output
3
Test instructions: It's probably the W where the puddles are, and there are a few puddles up and down and a slash direction.
Key points: Deep search for connected areas are directly labeled as the place to be searched. That's it. Finally, after several deep searches, there are a few puddles.
#include <iostream> #include <cstdio> #include <cstring>using namespace std;const int maxn = 110;int N, M ; Long width char FI[MAXN][MAXN]; Map void dfs (int x, int y); int main () {scanf ("%d%d", &n, &m); for (int i = 0; i < N; ++i) {scanf ("%s", Fi[i]); } int ans = 0; Answer for (int i = 0, i < N; ++i) {for (int j = 0; j < M; ++j) {if (fi[i][j] = = ' W ') {//Search to a new W Zone Dfs (I, j); Enter Search ++ans; Answer +1}}} printf ("%d\n", ans); return 0;} void Dfs (int x, int y) {fi[x][y] = '. '; The points you have searched for are marked with. for (int dx =-1; DX <= 1; ++dx) {//search upper and lower left slash direction for (int dy =-1; dy <= 1; ++dy) {int NX = x + dx int ny = y +dy; if (0 <= NX && NX < N && 0 <= ny && NY < M && Fi[nx][ny] = = ' W ') {//Meet requirements Continue search Cable DFS (NX, NY); } } }}
"POJ2386" Lake counting