DFS:POJ3620-Avoid The Lakes(求最基本的联通块)
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
- 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
好久没写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(求最基本的联通块)的更多相关文章
- 引爆炸弹——DFS&&联通块
题目 链接 在一个$n \times m$方格地图上,某些方格上放置着炸弹.手动引爆一个炸弹以后,炸弹会把炸弹所在的行和列上的所有炸弹引爆,被引爆的炸弹又能引爆其他炸弹,这样连锁下去. 现在为了引爆地 ...
- 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【DFS找联通块】
Avoid The Lakes Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6826 Accepted: 3637 D ...
- [深度优先搜索] POJ 3620 Avoid The Lakes
Avoid The Lakes Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8173 Accepted: 4270 D ...
- Avoid The Lakes
Avoid The Lakes Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) To ...
- 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 ...
- 【紫书】Oil Deposits UVA - 572 dfs求联通块
题意:给你一个地图,求联通块的数量. 题解: for(所有还未标记的‘@’点) 边dfs边在vis数组标记id,直到不能继续dfs. 输出id及可: ac代码: #define _CRT_SECURE ...
- 利用DFS求联通块个数
/*572 - Oil Deposits ---DFS求联通块个数:从每个@出发遍历它周围的@.每次访问一个格子就给它一个联通编号,在访问之前,先检查他是否 ---已有编号,从而避免了一个格子重复访问 ...
- 【bzoj2115】[Wc2011] Xor DFS树+高斯消元求线性基
题目描述 输入 第一行包含两个整数N和 M, 表示该无向图中点的数目与边的数目. 接下来M 行描述 M 条边,每行三个整数Si,Ti ,Di,表示 Si 与Ti之间存在 一条权值为 Di的无向边. 图 ...
随机推荐
- Gym - 101810H ACM International Collegiate Programming Contest (2018)
bryce1010模板 http://codeforces.com/gym/101810 #include <bits/stdc++.h> using namespace std; #de ...
- bzoj2818: Gcd懵逼乌斯反演
由于是单组数据,强行不分块O(n)过 线性筛部分非常神奇,用了一个奇妙的推导(懒得写了) #include <bits/stdc++.h> using namespace std; ],f ...
- python入门之正则表达式
正则 通过re模块实现 eg:>>>import re >>>re.findall('abc',str_name) 在strname里面完全匹配字符串 ...
- 18113 Secret Book of Kungfu 按位DFS
http://acm.scau.edu.cn:8000/uoj/mainMenu.html 18113 Secret Book of Kungfu 该题有题解 时间限制:1000MS 内存限制:65 ...
- C#私有的构造函数的作用
C#私有的构造函数的作用:当类的构造函数是私有的时候,也已防止C1 c1=new C1();实例化类.常见的应用是工具类和单例模式. using System;using System.Collect ...
- 学习typescript(一)
环境 必装软件 node,推荐 node 8.0 npm,推荐 npm 5.0 git, 最新版 vscode, 编绎器 必装包 tsc: npm install -g typescript typi ...
- vim编辑器高级应用
1. vim主要模式介绍 命令模式.命令行模式.编辑模式 字符操作:i 当前插入, I行首插入, a当前字符之后插入,A行首插入, ESC退出当前模式 2. vim命令模式 3. vim插入模式 4. ...
- Windows下Apache+PHP+MySQL开发环境的搭建(WAMP)
准备工作: 1.下载apache服务器安装包,官网http://www.apache.org/,下载地址:http://httpd.apache.org/download.cgi 2.下载MySQL, ...
- hihocoder1636 Pangu and Stones
思路: 区间dp.dp[l][r][k]表示把区间[l, r]的石子合并成k堆所需要的最小代价. 实现: #include <iostream> #include <cstring& ...
- 学习express(一)
菜鸟教程简介:Express 是一个简洁而灵活的 node.js Web应用框架, 提供了一系列强大特性帮助你创建各种 Web 应用,和丰富的 HTTP 工具. 使用 Express 可以快速地搭建一 ...