DFS:POJ3620-Avoid The Lakes(求最基本的联通块)
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&&联通块
题目 链接 在一个$n \times m$方格地图上,某些方格上放置着炸弹.手动引爆一个炸弹以后,炸弹会把炸弹所在的行和列上的所有炸弹引爆,被引爆的炸弹又能引爆其他炸弹,这样连锁下去. 现在为了引爆地 ...
- poj 3620 Avoid The Lakes【简单dfs】
Avoid The Lakes Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6795 Accepted: 3622 D ...
- POJ 3620 Avoid The Lakes【DFS找联通块】
Avoid The Lakes Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6826 Accepted: 3637 D ...
- [深度优先搜索] POJ 3620 Avoid The Lakes
Avoid The Lakes Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8173 Accepted: 4270 D ...
- Avoid The Lakes
Avoid The Lakes Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) To ...
- Codeforces Round #369 (Div. 2) D. Directed Roads dfs求某个联通块的在环上的点的数量
D. Directed Roads ZS the Coder and Chris the Baboon has explored Udayland for quite some time. The ...
- 【紫书】Oil Deposits UVA - 572 dfs求联通块
题意:给你一个地图,求联通块的数量. 题解: for(所有还未标记的‘@’点) 边dfs边在vis数组标记id,直到不能继续dfs. 输出id及可: ac代码: #define _CRT_SECURE ...
- 利用DFS求联通块个数
/*572 - Oil Deposits ---DFS求联通块个数:从每个@出发遍历它周围的@.每次访问一个格子就给它一个联通编号,在访问之前,先检查他是否 ---已有编号,从而避免了一个格子重复访问 ...
- 【bzoj2115】[Wc2011] Xor DFS树+高斯消元求线性基
题目描述 输入 第一行包含两个整数N和 M, 表示该无向图中点的数目与边的数目. 接下来M 行描述 M 条边,每行三个整数Si,Ti ,Di,表示 Si 与Ti之间存在 一条权值为 Di的无向边. 图 ...
随机推荐
- Redis安装(centOS7)
1.安装gcc环境 sudo yum install gcc-c++ 2.安装wget sudo yum install wget 3.下载Redis源码包 wget http://download. ...
- 修复在unix系统里的文件打开不能显示正常的颜色问题
在mac上面看到mysql的配置文件的颜色永远是白色,为了让配置文件的颜色更加分明些,这个时候只需进入到home目录下新建一个.vimrc文件, vim .vimrc set nu syntax o ...
- axios 在vue中使用
下载组件: npm install axios --save npm install qs --save //处理对象防止产生跨域问题 引入: 新建axios文件夹,文件下新建index.js文件 i ...
- css未知宽度水平居中整理
1.text-align 兼容性很好 .wp {text-align: center;} .test {display: inline;} <ul class="wp"> ...
- Codeforces Beta Round #12 (Div 2 Only) D. Ball 树状数组查询后缀、最值
http://codeforces.com/problemset/problem/12/D 这里的BIT查询,指的是查询[1, R]或者[R, maxn]之间的最值,这样就够用了. 设三个权值分别是b ...
- js中对象与函数的关系
首先什么是对象?根据W3C上面的解释JS中所有事物都是对象,对象是拥有属性和方法的数据,由此可以看出基本值类型不是对象(number.string.Boolean.Undefined),剩下的引用类型 ...
- Notepad++ 安装 Zen Coding / Emmet 插件
Zen Coding 插件 ============== 下载: Zen.Coding-Notepad++.v0.7.zip ==Installation== 1. Copy contents of ...
- #118. 【UR #8】赴京赶考
链接:#118. [UR #8]赴京赶考 高中,高中,短暂的三年.NOI是高中结业考试,而高考在每年暑假举行. 高二暑假,这是你最后一次参加高考的机会.你已经为了高考停课很久了,OI的知识很久没管了. ...
- 将Form以强类型Model提交,后台获取不到的问题
F.TextBoxFor(m => m.Name) 不能自定ID属性
- CF1110C Meaningless Operations
思路: 令x为满足2x <= a的最大的x.如果a的二进制表示中包含0,则将b构造为(2x+1 - 1) ^ a即可:否则gcd(a ^ b, a & b) = gcd(2x+1 - 1 ...