The basic DFS question, read the Challenge Program Design book to have a preliminary understanding of this algorithm, through constant search for continuous changes, find the connected block, compile error was reported twice when the poj was submitted. The first time the compiler was not modified, it may be the reason why the question was not done for a long time. Then, it is necessary to add a loop when deciding whether to input it, as a result, while has one more bracket, and I didn't find this bug in codeblocks. Forget it. I 'd better practice ACM.
# Include <iostream>
# Include <cstdio>
Using namespace STD;
Const int max_n = 101, max_m = 101;
Int n, m;
Char field [max_n] [max_m];
Void DFS (int x, int y ){
Field [x] [Y] = '.';
For (INT xn =-1; XN <= 1; XN ++ ){
For (int yn =-1; YN <= 1; YN ++ ){
Int xx = xn + X;
Int YY = YN + Y;
If (0 <= XX & XX <= N & 0 <= YY & YY <= M & field [XX] [YY] = 'W ')
DFS (XX, YY );
}
}
}
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 ++;
}
}
}
Printf ("% d \ n", Res );
}
Int main (){
Cin> N> m;
// Char field [max_n] [max_n + 1];
For (INT I = 0; I <n; I ++ ){
For (Int J = 0; j <m; j ++ ){
Cin> Field [I] [J];
}
}
Solve ();
Return 0;
}
Poj Lake counting 2386