POJ 3620 Avoid The Lakes (求连接最长的线)(DFS)
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
给出一串坐标,连在一起的算一个,计算每个由多少个坐标组成,输出最大值。
#include<cstdio>
#include<string.h>
int dx[]={-,,,};
int dy[]={,,-,};
int n,m,map[][],x,y,i,max,ans,k,j;
void f(int x,int y)
{
int i,nx,ny;
for(i = ; i < ; i++)
{
nx=x+dx[i];
ny=y+dy[i];
if(nx > && ny > && nx <= n && ny <=m && map[nx][ny] == )
{
ans++;
map[nx][ny]=;
f(nx,ny);
}
}
}
int main()
{
while(scanf("%d %d %d",&n,&m,&k)!=EOF)
{
max=;
memset(map,,sizeof(map));
for(i = ; i < k ; i++)
{
scanf("%d %d",&x,&y);
map[x][y]=; //1表示没走过
}
for(i = ; i <=n ; i++)
{
for(j = ; j <= m ; j++)
{ if(map[i][j] == )
{
ans=;
map[i][j]=;
f(i,j);
max=max>ans?max:ans;
} } }
printf("%d\n",max);
}
}
POJ 3620 Avoid The Lakes (求连接最长的线)(DFS)的更多相关文章
- 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
Avoid The Lakes Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8173 Accepted: 4270 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
http://poj.org/problem?id=3620 DFS 从任意一个lake出发 重置联通的lake 并且记录 更新ans #include <iostream> #inclu ...
- POJ 3620 Avoid The Lakes(dfs算法)
题意:给出一个农田的图,n行m列,再给出k个被淹没的坐标( i , j ).求出其中相连的被淹没的农田的最大范围. 思路:dfs算法 代码: #include<iostream> #inc ...
- poj 3620 Avoid The Lakes(广搜,简单)
题目 找最大的一片湖的面积,4便有1边相连算相连,4角不算. runtime error 有一种可能是 数组开的太小,越界了 #define _CRT_SECURE_NO_WARNINGS #incl ...
- poj 1523Tarjan算法的含义——求取割点可以分出的连通分量的个数
poj 1523Tarjan算法的含义——求取割点可以分出的连通分量的个数 题目大意:如题目所示 给你一些关系图——连通图,想要问你有没有个节点,损坏后,可以生成几个互相独立的网络(也就是连通分量), ...
- Avoid The Lakes
Avoid The Lakes Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) To ...
- DFS:POJ3620-Avoid The Lakes(求最基本的联通块)
Avoid The Lakes Time Limit: 1000MS Memory Limit: 65536K Description Farmer John's farm was flooded i ...
随机推荐
- 分布式集群环境下,如何实现session共享三(环境搭建)
这是分布式集群环境下,如何实现session共享系列的第三篇.在上一篇:分布式集群环境下,如何实现session共享二(项目开发)中,准备好了一个通过原生态的servlet操作session的案例.本 ...
- zabbix被动模式和主动模式
1 被动模式 zabbix默认采用被动模式.就是agent等待server采集数据. 在items中,type为zabbix agent就是指被动模式. 流程为:agent周期性收集数据,server ...
- magento layout xml 小结
基础概念: http://magebase.com/magento-tutorials/demystifying-magentos-layout-xml-part-1/ 调试方案函数: $this-& ...
- 494 Target Sum 目标和
给定一个非负整数数组,a1, a2, ..., an, 和一个目标数,S.现在你有两个符号 + 和 -.对于数组中的任意一个整数,你都可以从 + 或 -中选择一个符号添加在前面.返回可以使最终数组和为 ...
- [已读]响应式web设计
去年冲着响应式这三个字买的,很快就读完了,因为说实话都挺浅显的内容.真正涉及到响应式的是第二和第三章(媒体查询 em 百分比图片),其他的h5与css3关系不大.
- RHEL 7.2 源码安装Python 3.6.2报错
报错代码:zipimport.ZipImportError: can't decompress data; zlib not available 一条命令解决:yum install zlib-dev ...
- Spark MLlib编程API入门系列之特征选择之卡方特征选择(ChiSqSelector)
不多说,直接上干货! 特征选择里,常见的有:VectorSlicer(向量选择) RFormula(R模型公式) ChiSqSelector(卡方特征选择). ChiSqSelector用于使用卡方检 ...
- leetcode410 Split Array Largest Sum
思路: dp. 实现: class Solution { public: int splitArray(vector<int>& nums, int m) { int n = nu ...
- 【学习笔记】深入理解js原型和闭包(15)——闭包
前面提到的上下文环境和作用域的知识,除了了解这些知识之外,还是理解闭包的基础. 至于“闭包”这个词的概念的文字描述,确实不好解释,我看过很多遍,但是现在还是记不住. 但是你只需要知道应用的两种情况即可 ...
- Smart SVN的使用
最近项目使用了SVN,为管理代码起到了很好的作用!但是,对于很多初步使用着,还是非常不容易! 公司使用的是Smart SVN 客户端. Smart SVN 这个工具总体还是挺不错的! 在代码的提交和获 ...