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. 最耗资源的10条sql

    ----当前最耗资源的10个cpu select * from (select address,hash_value, round(cpu_time/1000000) cpu_time_s, roun ...

  2. (转)Unity3D中常用的数据结构总结与分析

    http://www.cnblogs.com/murongxiaopifu/p/4161648.html#array   1.几种常见的数据结构 常碰到的几种数据结构:Array,ArrayList, ...

  3. centos7安装mysql5.7 使用yum

    https://blog.csdn.net/z13615480737/article/details/78906598 使用yum,比较简单,不用考虑版本依赖问题

  4. SpringBoot 2.x (9):整合Mybatis注解实战

    SSM框架再熟悉不过了,不过以前通常是使用XML写SQL语句 这里用SpringBoot整合Mybatis并且使用注解进行开发 依赖: <!-- Mybatis --> <depen ...

  5. MySQL存储引擎问题

    一.什么是存储引擎 数据库就是一种对数据进行管理和存储的软件,而它能够实现存储和管理数据的原因就是因为存储引擎的存在,存储引擎就是对存储数据.为存储的数据建立索引以及更新.查询数据等技术的实现方法:在 ...

  6. (十一)maven之安装nexus私服

    安装nexus私服 前面的文章中对项目引入jar依赖包的时候,maven一般先是在本地仓库找对应版本的jar依赖包,如果在本地仓库中找不到,就上中央仓库中下载到本地仓库. 然而maven默认提供的中央 ...

  7. hdu 6058 Kanade's sum (计算贡献,思维)

    题意: 给你一个全排列,要你求这个序列的所有区间的第k大的和 思路:比赛的时候一看就知道肯定是算贡献,也知道是枚举每个数,然后看他在多少个区间是第K大,然后计算他的贡献就可以了,但是没有找到如何在o( ...

  8. Asp.Net Core 入门(五)—— 布局视图_Layout.cshtml

    布局视图和我们在Asp.Net MVC一样,布局视图_Layout.cshtml使得所有视图保持一致的外观变得更加容易,因为我们只有一个要修改的布局视图文件,更改后将立即反映在整个应用程序的所有视图中 ...

  9. javascript单元测试框架mochajs详解(转载)

    章节目录 关于单元测试的想法 mocha单元测试框架简介 安装mocha 一个简单的例子 mocha支持的断言模块 同步代码测试 异步代码测试 promise代码测试 不建议使用箭头函数 钩子函数 钩 ...

  10. C++值传递、引用传递和指针传递

    #include<iostream> using namespace std; //值传递 void change1(int n){ cout<<"值传递--函数操作 ...