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 ...
随机推荐
- c++ 常用的几种重载操作符
运算符可以作为普通函数,朋友函数或成员函数来重载.下面的经验法则可以帮助您确定哪种形式最适合于给定的情况: 如果你重载了赋值(=),下标([]),函数调用(())或成员选择( - >),那么它就 ...
- 洛谷 P3312 [SDOI2014]数表
式子化出来是$\sum_{T=1}^m{\lfloor}\frac{n}{T}{\rfloor}{\lfloor}\frac{m}{T}{\rfloor}\sum_{k|T}\mu(\frac{T}{ ...
- 解题报告:hdu 3572 Task Schedule(当前弧优化Dinic算法)
Problem Description Our geometry princess XMM has stoped her study in computational geometry to conc ...
- 降低winnt Apache服务的权限,让你的虚拟主机更安全
winnt 安装 Apache 后,在后台的服务默认是以system权限运行的(system是winnt的最高权限),这给服务器带来很大的安全隐患,最近我遇到的就是php的W8C 文件管理既然可以随意 ...
- php url地址栏传中文乱码解决方法集合
php地址栏传中文$_GET下来后乱码,urlencode和urldecode,iconv,base64_encode等方法,整理基本是常用的了. php地址栏传中文$_GET下来后乱码,urlen ...
- 数位dp知识
转自http://blog.csdn.net/zhaoxinfan/article/details/8707605 下面先给出数位DP的背景: •在给定区间[A,B]内,找满足要求的数. •要求一般和 ...
- HttpServletResponse 的状态码
public static final int SC_ACCEPTED 202 public static final int SC_BAD_GATEWAY 502 public static ...
- QWidget标题栏双击事件
widget.h virtual bool event(QEvent *event); widget.cpp bool Widget::event(QEvent *event) { if (event ...
- 1 开发一个注重性能的JDBC应用程序不是一件容易的事. 当你的代码运行很慢的时候JDBC驱动程序并不会抛出异常告诉你。 本系列的性能提示将为改善JDBC应用程序的性能介绍一些基本的指导原则,这其中的原则已经被许多现有的JDBC应用程序编译运行并验证过。 这些指导原则包括: 正确的使用数据库MetaData方法 只获取需要的数据 选用最佳性能的功能 管理连
1 开发一个注重性能的JDBC应用程序不是一件容易的事. 当你的代码运行很慢的时候JDBC驱动程序并不会抛出异常告诉你. 本系列的性能提示将为改善JDBC应用程序的性能介绍一些基本的指导原则,这其中的 ...
- Android(java)学习笔记159:多线程断点下载的原理(Android实现)
之前在Android(java)学习笔记215中,我们从JavaSE的角度去实现了多线程断点下载,下面从Android角度实现这个断点下载: 1. 新建一个Android工程: (1)其中我们先实现布 ...