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了有点手生。


  1. #include<stdio.h>
  2. #include<cstring>
  3. const int maxn = 110;
  4. char maps[maxn][maxn];
  5. int dir[4][2] = {1,0,-1,0,0,1,0,-1};
  6. bool vis[maxn][maxn];
  7. int n,m,k,ans,num;
  8. void pre_maps()//自己建立一个地图
  9. {
  10. for(int i=1;i<=n;i++)
  11. for(int j=1;j<=m;j++)
  12. maps[i][j] = '*';
  13. }
  14. bool check(int x,int y)//检查是否跑到了地图外面
  15. {
  16. if(x<1 || y<1 || x>n || y>m)
  17. return false;
  18. return true;
  19. }
  20. void dfs(int x,int y)
  21. {
  22. vis[x][y] = true;
  23. num++;//找到一个算一个
  24. if(num > ans)
  25. ans = num;
  26. for(int i=0;i<4;i++)
  27. 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]])
  28. dfs(x+dir[i][0],y+dir[i][1]);//四个方向直接找,注意标记
  29. return;
  30. }
  31. void DFS()
  32. {
  33. for(int i=1;i<=n;i++)
  34. for(int j=1;j<=m;j++)
  35. if(maps[i][j] == '#' && !vis[i][j])
  36. {
  37. num = 0;
  38. dfs(i,j);
  39. }
  40. }
  41. int main()
  42. {
  43. while(scanf("%d%d%d",&n,&m,&k) != EOF)
  44. {
  45. ans = 0;
  46. memset(vis,0,sizeof(vis));
  47. pre_maps();
  48. while(k--)
  49. {
  50. int a,b;
  51. scanf("%d%d",&a,&b);
  52. maps[a][b] = '#';
  53. }
  54. DFS();
  55. printf("%d\n",ans);
  56. }
  57. return 0;
  58. }

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. Appium禁止appium setting和unlock在设备上重复安装

    1.文件:/Applications/Appium.app/Contents/Resources/node_modules/appium/node_modules/appium-android-dri ...

  2. 安装wamp时出现httpd.exe无法找到组件MSVCR100.dll的解决办法

    很多朋友在安装wamp server时会出现以下错误,安装之后无法启动httpd.exe服务. --------------------------- httpd.exe - 无法找到组件 ----- ...

  3. Enum 枚举类

    目录 Enum 枚举类 基础 定义与用途 基本方法 示例 进阶 实现原理 枚举与Class对象 自定义枚举类和构造方法及toString() Enum中使用抽象方法来实现枚举实例的多态性 Enum与接 ...

  4. django-Haystack库

    本文参考自Haystack官方文档:https://django-haystack.readthedocs.io/en/master/tutorial.html#configuration 简介 Ha ...

  5. 上白泽慧音——tarjian

    题目描述 在幻想乡,上白泽慧音是以知识渊博闻名的老师.春雪异变导致人间之里的很多道路都被大雪堵塞,使有的学生不能顺利地到达慧音所在的村庄.因此慧音决定换一个能够聚集最多人数的村庄作为新的教学地点.人间 ...

  6. 服务器 未能加载文件或程序集“XXXX”或它的某一个依赖项。试图加载格式不正确的程序。

    ,本人采用的第一种解决办法解决,已解决 问题2: 在同一个服务器上想要一个IP有两个网址,配置端口号,给新端口号开权限

  7. 如何加快HTML页面加载速度

    1. 页面减肥 a. 页面的肥瘦是影响加载速度最重要的因素. b. 删除不必要的空格.注释. c. 将inline的script和css移到外部文件. d. 可以使用HTML Tidy来给HTML减肥 ...

  8. 学习笔记——Paint 1(MaskFilter)

    对于Paint没有很好的深入的学习过,在工作之余再巩固巩固. 1.Paint的BlurMaskFilter(模糊效果) 自定义一个View继承View 重写里面的onDraw方法.这里直接上代码了: ...

  9. uvm_base——打好你的基础

    uvm_base 是个很有意思的文件,这是UVM很巧妙的设计,将所有在base中包含的文件都包含在uvm_base.svh, 这样很方便管理各个文件直接的关系,而且还可以看出一些我之前没看过的东西,比 ...

  10. MDI和在TabPage

    无奈的.net探索 MDI和在TabPage中增加Form分页? MDI(Multiple Document Interface)是一种在窗口中嵌套窗口的接口, 与之对应的是SDI(Single Do ...