Oil Deposits
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 14628   Accepted: 7972

Description

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.

Input

The input 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.

Output

are adjacent horizontally, vertically, or diagonally. An oil deposit will not contain more than 100 pockets.

Sample Input

  1. 1 1
  2. *
  3. 3 5
  4. *@*@*
  5. **@**
  6. *@*@*
  7. 1 8
  8. @@****@*
  9. 5 5
  10. ****@
  11. *@@*@
  12. *@**@
  13. @@@*@
  14. @@**@
  15. 0 0

Sample Output

  1. 0
  2. 1
  3. 2
  4. 2
  5. 方法1:并查集
    收获:二维数组转一维数组公式:i*m(为列数)+j;
  1. #include <cstdio>
  2. #include <iostream>
  3. #include <cstdlib>
  4. #include <algorithm>
  5. #include <ctime>
  6. #include <cmath>
  7. #include <string>
  8. #include <cstring>
  9. #include <stack>
  10. #include <queue>
  11. #include <list>
  12. #include <vector>
  13. #include <map>
  14. #include <set>
  15. using namespace std;
  16.  
  17. const int INF=0x3f3f3f3f;
  18. const double eps=1e-;
  19. const double PI=acos(-1.0);
  20. #define maxn 500
  21. #define maxm 100000
  22. int n, m;
  23. char mp[maxn][maxn];
  24. int root[maxm];
  25. bool solve(int x, int y)
  26. {
  27. if(x < || x >= n || y < || y >= m)
  28. return true;
  29. return false;
  30. }
  31. int find_root(int x)
  32. {
  33. if(x != root[x])
  34. root[x] = find_root(root[x]);
  35. return root[x];
  36. }
  37. void uni(int a, int b)
  38. {
  39. int x = find_root(a);
  40. int y = find_root(b);
  41. if(x != y)
  42. {
  43. root[y] = x;
  44. }
  45. }
  46. void judge(int x, int y)
  47. {
  48. for(int i = -; i <= ; i++)
  49. for(int j = -; j <= ; j++)
  50. {
  51. if(i != || j != )
  52. {
  53. int dx = x + i;
  54. int dy = y + j;
  55. if(mp[dx][dy] == '@' && !solve(dx, dy))
  56. {
  57. int p = x * m + y;
  58. int q = dx * m + dy;
  59. uni(p, q);
  60. }
  61. }
  62. }
  63. }
  64. int main()
  65. {
  66. while(~scanf("%d%d", &n, &m) && (n+m) != )
  67. {
  68. memset(root, -, sizeof root);
  69. for(int i = ; i < n; i++)
  70. scanf("%s", mp[i]);
  71. for(int i = ; i < n; i++)
  72. {
  73. for(int j = ; j < m; j++)
  74. {
  75. if(mp[i][j] == '@')
  76. root[i*m+j] = i * m +j;
  77. }
  78. }
  79.  
  80. for(int i = ; i < n; i++)
  81. for(int j = ; j < m; j++)
  82. {
  83. if(mp[i][j] == '@')
  84. {
  85. judge(i, j);
  86. }
  87. }
  88. int ans = ;
  89. for(int i = ; i < n ; i++)
  90. for(int j = ; j < m; j++)
  91. if(root[i*m+j] == i*m+j)
  92. ans++;
  93. printf("%d\n", ans);
  94. }
  95. return ;
  96. }
  1. 方法二:
    DFS入门题
    收获:做搜索的题目调试的时候可以用打印中间路径的方法来调试。
  1. #include <cstdio>
  2. #include <iostream>
  3. #include <cstdlib>
  4. #include <algorithm>
  5. #include <ctime>
  6. #include <cmath>
  7. #include <string>
  8. #include <cstring>
  9. #include <stack>
  10. #include <queue>
  11. #include <list>
  12. #include <vector>
  13. #include <map>
  14. #include <set>
  15. using namespace std;
  16.  
  17. const int INF=0x3f3f3f3f;
  18. const double eps=1e-;
  19. const double PI=acos(-1.0);
  20. #define maxn 500
  21. int n, m;
  22. char mp[maxn][maxn];
  23. int vis[maxn][maxn];
  24. bool solve(int x, int y)
  25. {
  26. if(x < || x >= n || y < || y >= m)
  27. return true;
  28. return false;
  29. }
  30. void dfs(int x, int y)
  31. {
  32. for(int i = -; i <= ; i++)
  33. for(int j = -; j <= ; j++)
  34. {
  35. if(i != || j != )
  36. {
  37. int dx = x + i;
  38. int dy = y + j;
  39. if(!vis[dx][dy] && mp[dx][dy] == '@' && !solve(dx, dy))
  40. {
  41. vis[dx][dy] = ;
  42. dfs(dx, dy);
  43. }
  44. }
  45. }
  46. }
  47. int main()
  48. {
  49. while(~scanf("%d%d", &n, &m) && (n+m) != )
  50. {
  51. for(int i = ; i < n; i++)
  52. scanf("%s", mp[i]);
  53. int cnt = ;
  54. memset(vis, , sizeof vis);
  55. for(int i = ; i < n; i++)
  56. for(int j = ; j < m; j++)
  57. {
  58. if(mp[i][j] == '@' && !vis[i][j])
  59. {
  60. vis[i][j] = ;
  61. ++cnt;
  62. dfs(i, j);
  63. }
  64. }
  65. printf("%d\n", cnt);
  66. }
  67. return ;
  68. }

POJ 1562 Oil Deposits (并查集 OR DFS求联通块)的更多相关文章

  1. [POJ] 1562 Oil Deposits (DFS)

    Oil Deposits Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 16655   Accepted: 8917 Des ...

  2. (简单) POJ 1562 Oil Deposits,BFS。

    Description The GeoSurvComp geologic survey company is responsible for detecting underground oil dep ...

  3. HDU - 1241 POJ - 1562 Oil Deposits DFS FloodFill漫水填充法求连通块问题

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

  4. POJ 1562 Oil Deposits (HDU 1241 ZOJ 1562) DFS

    现在,又可以和她没心没肺的开着玩笑,感觉真好. 思念,是一种后知后觉的痛. 她说,今后做好朋友吧,说这句话的时候都没感觉.. 我想我该恨我自己,肆无忌惮的把她带进我的梦,当成了梦的主角. 梦醒之后总是 ...

  5. poj 1562 Oil Deposits (广搜,简单)

    题目 简单的题目,只是测试案例的输入后面可能有空格,所以要注意一下输入方式. #define _CRT_SECURE_NO_WARNINGS //题目的案例输入n,m后面有些貌似有空格... #inc ...

  6. POJ 1562 Oil Deposits

    转载请注明出处:http://blog.csdn.net/a1dark 大规模的图论切题之旅正式开始了.由于今天停了一天的电.所以晚上才开始切题.直到昨晚才把图论大概看了一遍.虽然网络流部分还是不怎么 ...

  7. HDU - 1213 dfs求联通块or并查集

    思路:给定一个无向图,判断有几个联通块. AC代码 #include <cstdio> #include <cmath> #include <algorithm> ...

  8. [洛谷P1197/BZOJ1015][JSOI2008]星球大战Starwar - 并查集,离线,联通块

    Description 很久以前,在一个遥远的星系,一个黑暗的帝国靠着它的超级武器统治者整个星系.某一天,凭着一个偶然的机遇,一支反抗军摧毁了帝国的超级武器,并攻下了星系中几乎所有的星球.这些星球通过 ...

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

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

随机推荐

  1. CEF中文教程(google chrome浏览器控件) -- Windows下编译Chromium

    CEF中文教程(google chrome浏览器控件) -- CEF简介 2013-04-10 16:48 42928人阅读 评论(4) 收藏 举报  分类: CEF(2)    目录(?)[+]   ...

  2. Python常用模块 (2) (loging、configparser、json、pickle、subprocess)

    logging 简单应用 将日志打印到屏幕 import logging logging.debug('debug message') logging.info('info message') log ...

  3. IEEE发布2016年度编程语言排行榜

    C 语言占据榜首,但大数据类是最大赢家. IEEE Spectrum 的第三次"最受欢迎编程语言"交互式排行榜新鲜出炉.因为不可能顾及到每一个程序员的想法,Spectrum 使用多 ...

  4. ZOJ问题(坑死了)

    ZOJ问题 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  5. Eclipse SVN插件的帐号、password改动

    问题描写叙述: Eclipse的SVN插件Subclipse做得非常好,在svn操作方面提供了非常强大丰富的功能.但到眼下为止,该插件对svn用户的概念极为淡薄,不但不能方便地切换用户,并且一旦用户的 ...

  6. Flex布局实践

    介绍常见布局的Flex写法. 你会看到,不管是什么布局,Flex往往都可以几行命令搞定. 我只列出代码,详细的语法解释请查阅<Flex布局教程:语法篇>.我的主要参考资料是Landon S ...

  7. 关于继承扩展ASP.NET控件(以Textbox为例)

    以下是一个相对简陋的扩展, 主要是针对金额显示的Textbox扩展. using System; using System.Collections.Generic; using System.Linq ...

  8. javascript高级知识点——memoization

    memoization是一种非常有用的优化技术,它缓存特定输入产生的相应结果.这样麻烦的查找和迭代计算可以尽可能的减少. 它基本的思想是针对特定的输入,已经计算过的结果都是通过缓存当中的数据直接返回而 ...

  9. SQL语言学习-数据操纵语言

    一般而言,数据库中数据的生命周期包括数据插入以及更新.数据删除3个阶段.首先需要用户或者系统将数据插入表.然后,对数据的使用,包括数据的检索以及数据的更新.最后,如果数据已经没有使用价值,则将数据删除 ...

  10. Hadoop: HDFS 格式化时,出现 “ERROR namenode.NameNode: java.io.IOException: Cannot create directory /usr/hadoop/tmp/dfs/name/current”

    原因是 没有设置 /usr/hadoop/tmp 的权限没有设置, 将之改为: chown –R hadoop:hadoop /usr/hadoop/tmp 查看: