链接:http://poj.org/problem?id=2386
大意——给你一个n*m的矩形网格,每个格子里面要么是‘W’,要么是‘.’,它们分别表示水和旱地。现在要你计算出有多少个池塘。每个池塘由若干个水组成,水能连接的方向有8个,只要是能连接到的水都属于一个池塘。
思路——一个简单的DFS题。我们将所有的网格全部遍历一次,假如我们找到一个池塘的源头,就可以进行一次计数,并且将目前找到的池塘源头标记为‘.’,那么下一次就不会重复访问了,再从八个方向进行深搜,将能到达相邻的‘W’全部标记,以免重复访问。这样最后得到的计数就是答案。
复杂度分析——时间复杂度:O(n*m),空间复杂度:O(n*m)
附上AC代码:
#include <iostream> #include <cstdio> #include <string> #include <cmath> #include <iomanip> #include <ctime> #include <climits> #include <cstdlib> #include <cstring> #include <algorithm> #include <queue> #include <vector> #include <set> #include <map> //#pragma comment(linker, "/STACK:102400000, 102400000") using namespace std; typedef unsigned int li; typedef long long ll; typedef unsigned long long ull; typedef long double ld; const double pi = acos(-1.0); const double e = exp(1.0); const double eps = 1e-8; const int maxn = 105; const int dir[8][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}, {-1, -1}, {1, -1}, {-1, 1}, {1, 1}}; // 8个方向 int n, m; // 矩形的行列 char mat[maxn][maxn]; // 矩形 void dfs(int x, int y); // 深度优先搜索 int main() { ios::sync_with_stdio(false); while (~scanf("%d%d", &n, &m)) { int cnt = 0; for (int i=0; i<n; i++) scanf("%s", mat[i]); for (int i=0; i<n; i++) for (int j=0; j<m; j++) if (mat[i][j] == 'W') { // 找到池塘源头,计数并深搜 cnt++; dfs(i, j); } printf("%d\n", cnt); } return 0; } void dfs(int x, int y) { mat[x][y] = '.'; // 访问过了,标记 for (int i=0; i<8; ++i) // 从八个方向找相邻的 if (x+dir[i][0]>=0 && x+dir[i][0]<n && y+dir[i][1]>=0 && y+dir[i][1]<m && mat[x+dir[i][0]][y+dir[i][1]]=='W') dfs(x+dir[i][0], y+dir[i][1]); // 找到相邻的,继续深搜 }