POJ 2386 Lake Counting(DFS:求8连通分量的个数)
http://poj.org/problem?id=2386
题意:给你一个N*M的字符网格,要你求该网格中有多少个有字符’w’构造的8连通分量.
分析:直接DFS求出连通分量个数即可.之前很多题目与本题类似.
AC代码:
#include<cstdio>
using namespace std;
const int maxn=100+5;
char grid[maxn][maxn];
int R,C;
int ans;
void dfs(int r,int c)
{
if(r<0||r>=R||c<0||c>=C||grid[r][c]!='W')
return ;
grid[r][c]='.';
dfs(r,c+1);
dfs(r,c-1);
dfs(r+1,c);
dfs(r-1,c);
dfs(r+1,c+1);
dfs(r-1,c-1);
dfs(r+1,c-1);
dfs(r-1,c+1);
}
int main()
{
while(scanf("%d%d",&R,&C)==2)
{
for(int i=0;i<R;i++)
scanf("%s",grid[i]);
ans=0;
for(int i=0;i<R;i++)
for(int j=0;j<C;j++)if(grid[i][j]=='W')
{
ans++;
dfs(i,j);
}
printf("%d\n",ans);
}
return 0;
}
分享到:
相关推荐
poj 2386 Lake Counting.md
poj 2488——dfs深度优先遍历 //给行数列数,求问能否遍历,给出字典序的一种遍历
POJ2186-Popular Cows ...【Tarjan+极大强连通分量+缩点】 解题报告+AC代码 http://hi.csdn.net/!s/BGDH68 附:我所有的POJ解题报告链接 . http://blog.csdn.net/lyy289065406/article/details/6642573
初学者练题开始------在POJ上(注:是百练) 初学者练题开始------在POJ上(注:是百练) 初学者练题开始------在POJ上(注:是百练)
北大POJ3733-Changing Digits【DFS+强剪枝】 解题报告+AC代码
POJ上的DP分类: 容易: 1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1208, 1276, 1322, 1414, 1456, 1458, 1609, 1644, 1664, 1690, 1699, 1740(博弈), 1742, 1887, 1926(马尔科夫矩阵,求...
北大POJ1020-Anniversary Cake 解题报告+AC代码
北大POJ3373-Changing Digits【DFS+强剪枝】 解题报告+AC代码
北大POJ3009-Curling 2.0【DFS+Vector+回溯+剪枝】 解题报告+AC代码
POJ 1300 Door Man:无向图、欧拉定理、gets、sscanf
北大POJ初级题-数据结构:解题报告+AC代码
poj还是leetcode ProgrammingCompetionCareer 大学的竞赛生涯结束了,抽时间整理了一部分曾经在OJ刷过的题,还有一些找不到了,或者是因为杂物太多(除了代码以外还有题解的pdf、题目的测试数据等等),不想整理了就...
NULL 博文链接:https://128kj.iteye.com/blog/1754756
poj 2820 古代密码 http://poj.grids.cn/problem?id=2820 可直接运行
POJ3177-Redundant Paths 【Tarjan-边双连通分量-缩点】 解题报告+AC代码+测试数据 http://hi.csdn.net/!s/GPAY6Z 附:我所有的POJ解题报告链接 . http://blog.csdn.net/lyy289065406/article/details/6642573
poj1002 source code input: The input will consist of one case. The first line of the input specifies the number of telephone numbers in the directory (up to 100,000) as a positive integer alone on ...
POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类
北大POJ1691-Painting A Board 【拓扑+DFS】 解题报告+AC代码
北大POJ1014-Dividing【DFS】【多重背包+二进制优化】 解题报告+AC代码
网上整理的一些poj刷题指南。 poj地址:http://poj.org