This is equivalent to finding the number of connected subgraphs in the graph. The number of connected subgraphs in the graph is eight neighboring areas according to the question (top, bottom, left, right, and four diagonal)
Idea: give priority to depth, search for the eight neighborhoods of each vertex in the graph in sequence, and mark the access. Otherwise, the consequences will be very serious.
AC code: 46 MS 416 K speed is not slow, it may be because of many obstacles in the figure.
[Cpp]
# Include <iostream>
# Include <string>
# Include <cstdio>
# Include <cmath>
# Include <vector>
# Include <algorithm>
# Include <sstream>
# Include <cstdlib>
# Include <fstream>
# Include <queue>
Using namespace std;
Bool visit [110] [110]; // repetitive Access Control
Char maze [110] [110]; // Map
Int dir [8] [2] = {-}, {0,-1}, {-1,-1 }, {-}, {1,-1}, {}; // eight directions
Int sum, m, n, sx, sy;
Bool isbound (int a, int B) {// judge the Boundary
If (a <1 | a> m | B <1 | B> n) return true;
Return false;
}
Void dfs (int sx, int sy) {// perform a deep search for each vertex
For (int I = 0; I <8; I ++)
{
If (maze [sx + dir [I] [0] [sy + dir [I] [1] = '*') continue; // obstacle return
If (isbound (sx + dir [I] [0], sy + dir [I] [1]) continue; // return the Boundary
If (visit [sx + dir [I] [0] [sy + dir [I] [1]) continue; // accessed, return
Visit [sx + dir [I] [0] [sy + dir [I] [1] = 1; // mark accessed to prevent repeated access
Dfs (sx + dir [I] [0], sy + dir [I] [1]);
}
}
Int main ()
{
While (cin> m> n)
{
If (m = 0) break;
Memset (visit, 0, sizeof (visit ));
For (int I = 1; I <= m; I ++ ){
For (int j = 1; j <= n; j ++ ){
Cin> maze [I] [j];
}
}
Sum = 0;
For (int I = 1; I <= m; I ++) {// The conditions for Deep Search for each vertex are:
// 1: not an obstacle; 2: not accessed
For (int j = 1; j <= n; j ++ ){
If (maze [I] [j] = '@'&&! Visit [I] [j]) {visit [I] [j] = 1; dfs (I, j); sum ++ ;}
}
}
Cout <sum <endl;
}
Return 0;
}
# Include <iostream>
# Include <string>
# Include <cstdio>
# Include <cmath>
# Include <vector>
# Include <algorithm>
# Include <sstream>
# Include <cstdlib>
# Include <fstream>
# Include <queue>
Using namespace std;
Bool visit [110] [110]; // repetitive Access Control
Char maze [110] [110]; // Map
Int dir [8] [2] = {-}, {0,-1}, {-1,-1 }, {-}, {1,-1}, {}; // eight directions
Int sum, m, n, sx, sy;
Bool isbound (int a, int B) {// judge the Boundary
If (a <1 | a> m | B <1 | B> n) return true;
Return false;
}
Void dfs (int sx, int sy) {// perform a deep search for each vertex
For (int I = 0; I <8; I ++)
{
If (maze [sx + dir [I] [0] [sy + dir [I] [1] = '*') continue; // obstacle return
If (isbound (sx + dir [I] [0], sy + dir [I] [1]) continue; // return the Boundary
If (visit [sx + dir [I] [0] [sy + dir [I] [1]) continue; // accessed, return
Visit [sx + dir [I] [0] [sy + dir [I] [1] = 1; // mark accessed to prevent repeated access
Dfs (sx + dir [I] [0], sy + dir [I] [1]);
}
}
Int main ()
{
While (cin> m> n)
{
If (m = 0) break;
Memset (visit, 0, sizeof (visit ));
For (int I = 1; I <= m; I ++ ){
For (int j = 1; j <= n; j ++ ){
Cin> maze [I] [j];
}
}
Sum = 0;
For (int I = 1; I <= m; I ++) {// The conditions for Deep Search for each vertex are:
// 1: not an obstacle; 2: not accessed
For (int j = 1; j <= n; j ++ ){
If (maze [I] [j] = '@'&&! Visit [I] [j]) {visit [I] [j] = 1; dfs (I, j); sum ++ ;}
}
}
Cout <sum <endl;
}
Return 0;
}