DFS:POJ3620-Avoid The Lakes(求最基本的联通块)
来源:互联网 发布:linux 启动脚本 编辑:程序博客网 时间:2024/06/10 00:23
Avoid The Lakes
Time Limit: 1000MS
Memory Limit: 65536K
Description
Farmer John’s farm was flooded in the most recent storm, a fact only aggravated by the information that his cows are deathly afraid of water. His insurance agency will only repay him, however, an amount depending on the size of the largest “lake” on his farm.
The farm is represented as a rectangular grid with N (1 ≤ N ≤ 100) rows and M (1 ≤ M ≤ 100) columns. Each cell in the grid is either dry or submerged, and exactly K (1 ≤ K ≤ N × M) of the cells are submerged. As one would expect, a lake has a central cell to which other cells connect by sharing a long edge (not a corner). Any cell that shares a long edge with the central cell or shares a long edge with any connected cell becomes a connected cell and is part of the lake.
Input
- Line 1: Three space-separated integers: N, M, and K
- Lines 2..K+1: Line i+1 describes one submerged location with two space separated integers that are its row and column: R and C
Output
- Line 1: The number of cells that the largest lake contains.
Sample Input
3 4 5
3 2
2 2
3 1
2 3
1 1
Sample Output
4
好久没写dfs了有点手生。
#include<stdio.h>#include<cstring>const int maxn = 110;char maps[maxn][maxn];int dir[4][2] = {1,0,-1,0,0,1,0,-1};bool vis[maxn][maxn];int n,m,k,ans,num;void pre_maps()//自己建立一个地图{ for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) maps[i][j] = '*';}bool check(int x,int y)//检查是否跑到了地图外面{ if(x<1 || y<1 || x>n || y>m) return false; return true;}void dfs(int x,int y){ vis[x][y] = true; num++;//找到一个算一个 if(num > ans) ans = num; for(int i=0;i<4;i++) if(check(x+dir[i][0],y+dir[i][1]) && maps[x+dir[i][0]][y+dir[i][1]] == '#' && !vis[x+dir[i][0]][y+dir[i][1]]) dfs(x+dir[i][0],y+dir[i][1]);//四个方向直接找,注意标记 return;}void DFS(){ for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) if(maps[i][j] == '#' && !vis[i][j]) { num = 0; dfs(i,j); }}int main(){ while(scanf("%d%d%d",&n,&m,&k) != EOF) { ans = 0; memset(vis,0,sizeof(vis)); pre_maps(); while(k--) { int a,b; scanf("%d%d",&a,&b); maps[a][b] = '#'; } DFS(); printf("%d\n",ans); } return 0;}
- DFS:POJ3620-Avoid The Lakes(求最基本的联通块)
- DFS poj3620 Avoid The Lakes
- poj3620 avoid the lakes【dfs】
- DFS poj3620(Avoid The Lakes)
- POJ3620:Avoid The Lakes
- POJ3620 Avoid The Lakes
- Avoid The Lakes--poj3620
- POJ3620 Avoid The Lakes
- 【poj3620】Avoid The Lakes
- Avoid The Lakes<poj3620>
- Avoid The Lakes(poj3620)
- Avoid The Lakes(DFS)
- POJ 3620 Avoid The Lakes(简单DFS求连通分量)
- dfs求连通路(Avoid The Lakes poj)
- 3620 Avoid The Lakes【dfs】
- Avoid The Lakes(DFS)
- PKU3620 Avoid The Lakes DFS入门题
- poj 3620 Avoid The Lakes 深搜dfs
- 装饰模式
- tomcat加载失败,提示信息:Illegal access: this web application...
- 原生JAVA解析json
- 51nod 1133不重合线段
- 机器学习——简单线性回归(下)
- DFS:POJ3620-Avoid The Lakes(求最基本的联通块)
- 史上最牛的员工激励
- Search Insert Position
- wget命令参数用法总结
- 2017乌鲁木齐网络赛D题 区间dp
- appium并发测试
- jquery操作复选框的总结
- Selenium with Python中文翻译(八)
- js的call方法和apply方法