Simple Deep Search is enough. When someone says what to use and query the set, it is a small use of big algorithms.
Because it can be searched in depth without backtracking, the efficiency is O (n * m.
The trick is to add a sign P. Every time a pond is searched, there is a W letter, so it is considered that a pond is searched, and the P value is true.
Do not repeat the searched pond. Therefore, every time you walk through the pond, you can change it to another letter, such as '@' or.
Then, search in 8 directions.
# Include <stdio. h> # include <vector> # include <string. h> # include <algorithm> # include <iostream> # include <string> # include <limits. h >#include <stack >#include <queue >#include <set> # include <map> using namespace STD; const int max_n = 101; char pond [max_n] [max_n]; const char vis = '@'; int n, m; bool P; inline bool islegal (int r, int C) {return 0 <= R & 0 <= C & R <n & C <M & pond [r] [c] = 'W ';} void getpond (int r, Int c) {If (! Islegal (R, c) return; P = true; pond [r] [c] = vis; getpond (R + 1, C); getpond (R-1, C ); getpond (R, C + 1); getpond (R, C-1); getpond (R + 1, C + 1); getpond (R + 1, C-1); getpond (R-1, c + 1); getpond (R-1, C-1); // eight direction search} int main () {While (~ Scanf ("% d", & N, & M) {getchar (); For (INT I = 0; I <n; I ++) {gets (pond [I]) ;}int ans = 0; For (INT I = 0; I <n; I ++) {for (Int J = 0; j <m; j ++) {P = false; getpond (I, j); ans + = P ;}} printf ("% d \ n", ANS );} return 0 ;}