1. /*
  2. Oil Deposits
  3. Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
  4. Total Submission(s): 37990 Accepted Submission(s): 22039
  5.  
  6. Problem Description
  7. The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works with one large rectangular region of land at a time, and creates a grid that divides the land into numerous square plots. It then analyzes each plot separately, using sensing equipment to determine whether or not the plot contains oil. A plot containing oil is called a pocket. If two pockets are adjacent, then they are part of the same oil deposit. Oil deposits can be quite large and may contain numerous pockets. Your job is to determine how many different oil deposits are contained in a grid.
  8.  
  9. Input:
  10. The input file contains one or more grids. Each grid begins with a line containing m and n, the number of rows and columns in the grid, separated by a single space. If m = 0 it signals the end of the input; otherwise 1 <= m <= 100 and 1 <= n <= 100. Following this are m lines of n characters each (not counting the end-of-line characters). Each character corresponds to one plot, and is either `*', representing the absence of oil, or `@', representing an oil pocket.
  11.  
  12. Output:
  13. For each grid, output the number of distinct oil deposits. Two different pockets are part of the same oil deposit if they are adjacent horizontally, vertically, or diagonally. An oil deposit will not contain more than 100 pockets.
  14.  
  15. Sample Input:
  16. 1 1
  17. *
  18.  
  19. 3 5
  20. *@*@*
  21. **@**
  22. *@*@*
  23.  
  24. 1 8
  25. @@****@*
  26.  
  27. 5 5
  28. ****@
  29. *@@*@
  30. *@**@
  31. @@@*@
  32. @@**@
  33.  
  34. Sample Output:
  35. 0
  36. 1
  37. 2
  38. 2
  39. */
  40.  
  41. #include<cstdio>
  42. #include<cstring>
  43. #include<algorithm>
  44. #include<iostream>
  45. #include<string>
  46. #include<vector>
  47. #include<stack>
  48. #include<bitset>
  49. #include<cstdlib>
  50. #include<cmath>
  51. #include<set>
  52. #include<list>
  53. #include<deque>
  54. #include<map>
  55. #include<queue>
  56. using namespace std;
  57.  
  58. const int M=;
  59. const int N=;
  60.  
  61. char maps[M][N];
  62. int visited[M][N];
  63.  
  64. void dfs(int i,int j)
  65. {
  66. ++visited[i][j];
  67. for(int x=-;x<=;x++)
  68. for(int y=-;y<=;y++)
  69. {
  70. if(maps[i+x][j+y]=='@'&&visited[i+x][j+y]==)
  71. {
  72. dfs(i+x,j+y);
  73. }
  74. }
  75. }
  76.  
  77. int main()
  78. {
  79. int m,n;
  80. while(scanf("%d %d",&m,&n)==&&m&&n)
  81. {
  82. for(int i=;i<m;i++)
  83. scanf("%s",maps[i]);
  84. memset(visited,,sizeof(visited));
  85. int answer=;
  86. for(int i=;i<m;i++)
  87. for(int j=;j<n;j++)
  88. {
  89. if(maps[i][j]=='@'&&visited[i][j]==)
  90. {
  91. answer++;
  92. dfs(i,j);
  93. }
  94. }
  95. cout<<answer<<endl;
  96. }
  97. return ;
  98. }

  比较水的一道题,难度主要在于英语阅读。这道题的思路很简单,对于首先发现是@的格子,我们有理由认为在该格子的八连通区域内很可能还有另外的@(这有点像我玩MineCraft时的挖矿过程,在发现矿产的方块周围的相邻方块很大概率还能发现同类矿产)。所以很容易想到要使用深度优先遍历检索八连通区域内的格子。涉及到dfs则必然使用递归来简化代码结构(不考虑内存限制)。所以主要代码结构就是大循环遍历找@,找到后就dfs,并且用一个标志数组来记录下访问过的格子就行了。因为这道题我第一次写完代码就AC了,所以代码中就不加注释了。

tz@COI HZAU

2018/3/21

ACM:油田(Oil Deposits,UVa 572)的更多相关文章

  1. Oil Deposits UVA - 572

    The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSu ...

  2. 油田 (Oil Deposits UVA - 572)

    题目描述: 原题:https://vjudge.net/problem/UVA-572 题目思路: 1.图的DFS遍历 2.二重循环找到相邻的八个格子 AC代码: #include <iostr ...

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

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

  4. [C++]油田(Oil Deposits)-用DFS求连通块

    [本博文非博主原创,均摘自:刘汝佳<算法竞赛入门经典>(第2版) 6.4 图] [程序代码根据书中思路,非独立实现] 例题6-12 油田(Oil Deposits,UVa572) 输入一个 ...

  5. 油田 Oil Deposits

    油田 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=84562#problem/L 题意: 输入一个m行n列的字符矩形,统计字符 ...

  6. 洛谷 题解 UVA572 【油田 Oil Deposits】

    这是我在洛谷上的第一篇题解!!!!!!!! 这个其实很简单的 我是一只卡在了结束条件这里所以一直听取WA声一片,详细解释代码里见 #include<iostream> #include&l ...

  7. UVA 572 Oil Deposits油田(DFS求连通块)

    UVA 572     DFS(floodfill)  用DFS求连通块 Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format: ...

  8. UVA 572 -- Oil Deposits(DFS求连通块+种子填充算法)

    UVA 572 -- Oil Deposits(DFS求连通块) 图也有DFS和BFS遍历,由于DFS更好写,所以一般用DFS寻找连通块. 下述代码用一个二重循环来找到当前格子的相邻8个格子,也可用常 ...

  9. uva 572 oil deposits——yhx

    Oil Deposits  The GeoSurvComp geologic survey company is responsible for detecting underground oil d ...

随机推荐

  1. JS控制音频顺序播放

    做一项目,用到“叫号功能”,网页上有一“叫号”按钮,点击后就读数据库中存的号码,如123号, 然后就发声音出来, 思路是网上下载0123456789的叫号声音,然后按钮点击事件里就在JS里写用那个HT ...

  2. 译:9.使用Redis进行消息传递

    本指南引导您完成使用Spring Data Redis发布和订阅通过Redis发送的消息的过程.Messaging with Redis 1. 我们将构建什么? 您将构建一个使用StringRedis ...

  3. 1. RNN神经网络模型原理

    1. RNN神经网络模型原理 2. RNN神经网络模型的不同结构 3. RNN神经网络-LSTM模型结构 1. 前言 循环神经网络(recurrent neural network)源自于1982年由 ...

  4. javascript form提交 不执行onsubmit事件解决方案

    转载自:https://www.cnblogs.com/lorgine/archive/2011/03/30/2000284.html 今天做项目过程中,需要用到javascript提交form到后台 ...

  5. AtomicInteger 源码阅读

    Package java.util.concurrent.atomic 这是一个小工具包,它的实际作用是提供了很多个无阻塞的线程安全的变量操作工具. 无阻塞的线程安全:其含义就是不使用 synchro ...

  6. 设置Git用户信息

    $ git config --global user.name "leehongee" //给自己起个用户名 $ git config --globla user.email &q ...

  7. [Laravel] 07 - Project: functions in Controller

    故事背景 一.项目预览 From: https://www.imooc.com/video/12521 表单操作 一.新增信息 既然是操作,自然会想到:控制器. 控制器  [1] 路由 ----> ...

  8. 使用 PHP_CodeSniffer 检查 代码 是否 符合 编码规范

    服务端部署:PHP_CodeSniffer HG 服务端部署篇 1.下载PHP_CodeSniffer 前往 https://github.com/squizlabs/PHP_CodeSniffer ...

  9. Sysfs文件系统接口调试

    首先需要初始化操作: s32 gtp_sysfs_init(void) { s32 ret ; debug_kobj = kobject_create_and_add("gtp", ...

  10. 从Elasticsearch来看分布式系统架构设计

    分布式系统类型多,涉及面非常广,不同类型的系统有不同的特点,批量计算和实时计算就差别非常大.这篇文章中,重点会讨论下分布式数据系统的设计,比如分布式存储系统,分布式搜索系统,分布式分析系统等. 我们先 ...