本文主要是介绍POJ2396 Lake Counting,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
暴力搜索经典题
挑战程序设计竞赛
题目链接
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn = 110;
char G[maxn][maxn];
int n, m, cnt = 0;
int dx[8] = {1, -1, 0, 0, -1, 1, 1, -1};
int dy[8] = {0, 0, 1, -1, -1, 1, -1, 1};
void dfs(int u, int v){if(u < 0 || v < 0 || u >= n || v >= m || G[u][v] == '.') return;G[u][v] = '.';for(int i = 0; i < 8; i++){int a = u + dx[i], b = v + dy[i];dfs(a, b);}
}
int main(){scanf("%d %d", &n, &m);getchar(); //换行符 for(int i = 0; i< n; i++) {for(int j = 0; j < m; j++)scanf("%c",&G[i][j]);getchar(); //吸收换行符 }for(int i = 0; i< n; i++) for(int j = 0; j < m; j++){if(G[i][j] == '.') continue;dfs(i, j);cnt++;}printf("%d\n", cnt); return 0;
}
这篇关于POJ2396 Lake Counting的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!