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. 调用submit()方式提交表单

    今天在看高级程序设计时看到的这样一段话: 在以调用submit()方法的形式提交表单时,不会触发submit事件 写了一个小例子做了下测试,的确如此: <form id="fm&quo ...

  2. Gym - 101147J Whistle's New Car 树上差分

    J. Whistle's New Car time limit per test 15 seconds memory limit per test 512 megabytes input car.in ...

  3. 《javascript设计模式》笔记之第七章:工厂模式

    在读了这章之后,根据我个人现在的理解,工厂模式就是:将一个类或者一个方法称为一个工厂,然后再将一些模块交给这个工厂,让这个工厂按照给它的不同模块产出不同的实例. 下面为正文: 一:简单工厂: 例子: ...

  4. php中socket的使用(重点参考)

    一.开启socket phpinfo();查看是否开启了socket扩展,否则在php.ini中开启. 二.服务器端代码的写法 <?php error_reporting(E_ALL); set ...

  5. curl请求模拟post发送json

    示例:curl -X POST --header "Content-Type:application/json" --data '{"name":"s ...

  6. 我的NopCommerce之旅(1): 系统综述

    一.概述 NopCommerce是一个开源的购物网站,它的特点是Pluggable modular/layered architecture(可插拔模块分层架构) 二.功能特色介绍 1.适配手机端 2 ...

  7. 将Form以强类型Model提交,后台获取不到的问题

    F.TextBoxFor(m => m.Name) 不能自定ID属性

  8. 第六章 设计程序架构 之 设计实现WebSocket策略

    1. 概述 传统网页的通信方式是请求-响应模式,每次请求-响应都是新的连接.连接的建立和断开也是需要消耗资源的. WebSocket是基于TCP协议,实现单个连接上的双向通信. 本章内容包括: 异步读 ...

  9. hihocoder1636 Pangu and Stones

    思路: 区间dp.dp[l][r][k]表示把区间[l, r]的石子合并成k堆所需要的最小代价. 实现: #include <iostream> #include <cstring& ...

  10. 《超实用的HTML代码段》阅读笔记1——HTML5自动聚焦

    在页面加载完成后自动将输入焦点定位到需要的元素,用户就可以直接在改元素中进行输入而不需要手动选择它. 通过autofocus的属性就可以指定这种自动聚焦的功能,示例代码如下: <form nam ...