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 ...
随机推荐
- 51Nod 1116 K进制下的大数(暴力枚举)
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> us ...
- Codeforces 1108F(克鲁斯卡尔的理解)
最小生成树会多样的情况是:两个或多个边等长且连通同样的两个并查集块. 所以可以跑一遍克鲁斯卡尔,每次把当前等长的边数出来,注意不要边找边并查,因为有一部分边是正常跑生成树我们也不会要他的,这种直接跳了 ...
- JSP与Servlet的编解码
一.java web中涉及编解码的地方 (1)浏览器端向后台发起请求时:URL.Cookie.Parameter: (2)后台响应返回数据时:页面编码,数据库数据编码:
- Netbeans自定义折叠代码
只需要在模块开始注释以//<editor-fold>开始, 在模块结束行以 //</editor-fold>结束即可 Can I Create Custom Code Fold ...
- solr查询优化【转】filtercache
solr查询优化(实践了一下效果比较明显) 什么是filtercache? solr应用中为了提高查询速度有可以利用几种cache来优化查询速度,分别是fieldValueCache,queryRes ...
- AJPFX总结内部类
内部类:内部类的访问规则:1. 内部类可以直接访问外部类中的成员,包括私有 原因是内部类中持有了一个外部类的引用,格式:外部类.this2. 外部类要访问内部类,必须建立内部类对象访问格式:1. ...
- SpringBoot 2.x (7):拦截器
类似以前SpringMVC的拦截器,但也有一些区别 SpringBoot的拦截器有两种方式: 第一种方式:过时的方式,适用于SpringBoot1.x的方式 package org.dreamtech ...
- cordova应用使用手机调试
对于cordova应用的调试,最方便调试方式还是作为h5应用在浏览器来调试,调试好了再打包cordova应用和打包apk.然而h5应用时的效果跟最终在安卓手机运行还有少数情况会不一样,因此,也需要有能 ...
- android studio 生成 jniLibs 目录
现在一般的项目都会加入第三方jar包,第三方jar包我们会新建一个文件夹:libs,然后jar包都放在这个文件夹中. 但我们会发现,只是新建一个文件加之后,在AndroidStudio的左侧并不会出现 ...
- EditText输入手机号自动带空格
xml: <EditText android:id="@+id/edit_main" android:layout_width="match_parent" ...