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

  1. Line 1: Three space-separated integers: N, M, and K
  2. 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

  1. 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(求最基本的联通块)的更多相关文章

  1. 引爆炸弹——DFS&&联通块

    题目 链接 在一个$n \times m$方格地图上,某些方格上放置着炸弹.手动引爆一个炸弹以后,炸弹会把炸弹所在的行和列上的所有炸弹引爆,被引爆的炸弹又能引爆其他炸弹,这样连锁下去. 现在为了引爆地 ...

  2. poj 3620 Avoid The Lakes【简单dfs】

    Avoid The Lakes Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6795   Accepted: 3622 D ...

  3. POJ 3620 Avoid The Lakes【DFS找联通块】

    Avoid The Lakes Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6826   Accepted: 3637 D ...

  4. [深度优先搜索] POJ 3620 Avoid The Lakes

    Avoid The Lakes Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8173   Accepted: 4270 D ...

  5. Avoid The Lakes

    Avoid The Lakes Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) To ...

  6. 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 ...

  7. 【紫书】Oil Deposits UVA - 572 dfs求联通块

    题意:给你一个地图,求联通块的数量. 题解: for(所有还未标记的‘@’点) 边dfs边在vis数组标记id,直到不能继续dfs. 输出id及可: ac代码: #define _CRT_SECURE ...

  8. 利用DFS求联通块个数

    /*572 - Oil Deposits ---DFS求联通块个数:从每个@出发遍历它周围的@.每次访问一个格子就给它一个联通编号,在访问之前,先检查他是否 ---已有编号,从而避免了一个格子重复访问 ...

  9. 【bzoj2115】[Wc2011] Xor DFS树+高斯消元求线性基

    题目描述 输入 第一行包含两个整数N和 M, 表示该无向图中点的数目与边的数目. 接下来M 行描述 M 条边,每行三个整数Si,Ti ,Di,表示 Si 与Ti之间存在 一条权值为 Di的无向边. 图 ...

随机推荐

  1. oracle dual是个什么表

    这几天一直在研究oracle,常常会用到dual这个系统表,dual表到底是一个什么表?带着疑问查了百度了一下,现在总结一下:DUAL是Oracle与数据字典一起自动创建的一个表,它只有一列:DUMM ...

  2. MODBUS移植的参考文章

    https://github.com/armink/FreeModbus_Slave-Master-RTT-STM32 http://www.360doc.com/content/14/0906/09 ...

  3. NUP2201MR

    NUP2201MR:双总线保护IC(瞬态抑制二极管),常用于USB总线的保护.

  4. #52. 【UR #4】元旦激光炮 (交互式题)

    链接:http://uoj.ac/problem/52 刚刚越过绝境长城,只见天空中出现了炫目的光芒 —— 圣诞老人出现了. 元旦三侠立刻进入战斗.生蛋侠.圆蛋侠和零蛋侠分别有 na,nb,ncna, ...

  5. 開玩樹莓派(一):安裝Raspbian系統

    目錄: 開玩樹莓派(一):安裝Raspbian系統 開玩樹莓派(二):配置IP,實現無顯示器局域網內Putty連接和RDP遠程 開玩樹莓派(三):Python編程 開玩樹莓派(四):GPIO控制和遠程 ...

  6. Java之构造方法及this、super关键字

    有关构造方法的理解: 需要对对象的数据进行初始化,则创建一个构造方法,此方法名字和类名一样,但是没有返回值(类型和具体的值都没,但是可以写return;).构造方法是用来创建对象的,所以是不能被对象调 ...

  7. 11.JAVA-Object类之finalize(),clone(),toString()等方法覆写

    1.Object介绍 Object类是一个特殊的类,是所有类(包括数组,接口 )的父类,如果一个类没有用extends明确指出继承于某个类,那么它默认继承Object类,所以可以通过向上转型的方法使用 ...

  8. 查看Windows激活信息

    使用 Windows + R组合快捷键打开运行命令框 1.运行: slmgr.vbs -dlv 可以查询到Win10的激活信息,包括:激活ID.安装ID.激活截止日期等信息. 2.运行: slmgr. ...

  9. EL1008E: Property or field 'timestamp' cannot be found on object of type 'java.util.HashMap

    2018-06-22 09:50:19.488  INFO 20096 --- [nio-8081-exec-2] o.a.c.c.C.[Tomcat].[localhost].[/]       : ...

  10. 找出指定文件夹中的所有以txt结尾的文件,包括所有嵌套的子文件夹

    # coding:utf-8 import os, re for i in os.walk('d:'+os.sep):     for txt in i[2]:         try:        ...