POJ 3620 Avoid The Lakes(简单DFS求连通分量)
来源:互联网 发布:网络词boom是什么意思 编辑:程序博客网 时间:2024/06/05 20:58
POJ 3620 Avoid The Lakes(简单DFS求连通分量)
http://poj.org/problem?id=3620
题意:
有一个N行M列的网格,其中有干的格子和有水的格子,现在要你求出网格中最大的全部由有水格子组成的4连通分量包含多少个格子?
分析:
直接用DFS做,从每一个未被标记的水格开始做一次DFS,记录此次DFS所走过的所有水格数cnt,然后用cnt更新ans即可.(如果用BFS做也是基本类似的做法)
AC代码:
<span style="font-size:18px;">#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int maxn=100+5;int maze[maxn][maxn];int R,C,K;int cnt,ans;int dr[]={-1,1,0,0};//上,下,左,右int dc[]={0,0,-1,1};void dfs(int r,int c){ if(r<1||r>R ||c<1||c>C || maze[r][c]==0) return ; maze[r][c]=0; cnt++; for(int d=0;d<4;d++) dfs(r+dr[d],c+dc[d]);}int main(){ while(scanf("%d%d%d",&R,&C,&K)==3) { for(int i=1;i<=R;i++) for(int j=1;j<=C;j++) maze[i][j]=0; while(K--) { int i,j; scanf("%d%d",&i,&j); maze[i][j]=1; } ans=0; for(int i=1;i<=R;i++) for(int j=1;j<=C;j++) if(maze[i][j]==1) { cnt=0; dfs(i,j); ans=max(ans,cnt); } printf("%d\n",ans); } return 0;}</span>
0 0
- POJ 3620 Avoid The Lakes(简单DFS求连通分量)
- POJ 3620 Avoid The Lakes(连通分量,DFS)
- poj 3620 Avoid The Lakes 深搜dfs
- POJ 3620--Avoid The Lakes【DFS】
- POJ 3620 Avoid The Lakes(dfs)
- poj 3620 Avoid The Lakes 【DFS】
- poj 3620 Avoid The Lakes(DFS)
- POJ 3620 Avoid The Lakes【DFS】
- poj 3620 avoid the lakes(dfs)
- POJ 3620--Avoid The Lakes【DFS】
- POJ 3620 Avoid The Lakes(DFS)
- poj--3620--Avoid The Lakes(dfs)
- POJ 3620 Avoid The Lakes dfs
- POJ 3620 Avoid The Lakes(DFS)
- POJ Problem 3620 Avoid The Lakes 【DFS】
- poj-3620-Avoid The Lakes【DFS】
- 【POJ】-3620-Avoid The Lakes(DFS)
- POJ 3620 Avoid The Lakes (DFS)
- Apache+PHP+Mysql OS X 10.9 Mavericks WEB 服务器配置
- 由Q_OBJECT引起的undefined reference to vtable for "xxx::xxx"问题
- dom4j对xml进行读写操作
- Ubuntu下Aircrack-ng工具包的使用
- NGUI 3.5教程(一)安装NGUI 3.5.8
- POJ 3620 Avoid The Lakes(简单DFS求连通分量)
- 动态规划
- 为日渐成熟的Android设计App
- iOS Safari 将网页「添加到主屏幕」的图标路径设置
- “儿子,长大娶媳妇不?”
- 在JFrame上添加背景图片
- tcp nio 远程主机强迫关闭了一个现有的连接
- 九度 1113 关于二叉树节点的个数问题
- HDU 4283 You Are the One (区间DP)