Oil Deposits

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 43487    Accepted Submission(s): 25275

Problem 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 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.
 
Output
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.
 
Sample Input
1 1
*
3 5
*@*@*
**@**
*@*@*
1 8
@@****@*
5 5
****@
*@@*@
*@**@
@@@*@
@@**@
0 0
 
Sample Output
0
1
2
2
 
Source
 
 
 
 
代码(DFS):
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. const int N=+;
  4. char a[N][N];
  5. int dis[][]={{,},{-,},{,},{,-},{,},{-,},{,-},{-,-}};
  6. int n,m,num;
  7. void DFS(int x,int y){
  8. int xx,yy;
  9. for(int i=;i<;i++){
  10. xx=dis[i][]+x;
  11. yy=dis[i][]+y;
  12. if(xx>=&&yy>=&&xx<n&&yy<m){
  13. if(a[xx][yy]=='@'){
  14. a[xx][yy]='*';
  15. DFS(xx,yy);
  16. }
  17. }
  18. }
  19. }
  20. int main(){
  21. while(~scanf("%d%d",&n,&m)){
  22. memset(a,,sizeof(a));
  23. if(n==&&m==)break;
  24. num=;
  25. for(int i=;i<n;i++){
  26. for(int j=;j<m;j++)
  27. cin>>a[i][j];
  28. }
  29. for(int i=;i<n;i++){
  30. for(int j=;j<m;j++){
  31. if(a[i][j]=='@'){
  32. num++;
  33. a[i][j]='*';
  34. DFS(i,j);
  35. }
  36. }
  37. }
  38. printf("%d\n",num);
  39. }
  40. return ;
  41. }

代码(BFS):

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<queue>
  5. using namespace std;
  6. const int N=+;
  7. char mat[N][N];
  8. int dir[][]={{,},{-,},{,},{,-},{,},{-,},{,-},{-,-}};
  9. int n,m,sum;
  10. struct Node{
  11. int x,y;
  12. };
  13. void BFS(int x,int y){
  14. queue<Node>q;
  15. Node node;
  16. node.x=x;
  17. node.y=y;
  18. q.push(node);
  19. while(!q.empty()){
  20. Node cur=q.front();
  21. Node next;
  22. q.pop();
  23. for(int i=;i<;i++){
  24. next.x=cur.x+dir[i][];
  25. next.y=cur.y+dir[i][];
  26. if(mat[next.x][next.y]=='@'){
  27. mat[next.x][next.y]='*';
  28. q.push(next);
  29. }
  30. }
  31. }
  32. }
  33. int main(){
  34. while(scanf("%d%d",&n,&m)){
  35. if(n==&m==)break;
  36. memset(mat,,sizeof(mat));
  37. sum=;
  38. int cur=;
  39. for(int i=;i<=n;i++)
  40. scanf("%s",mat[i]+);
  41. for(int i=;i<=n;i++){
  42. for(int j=;j<=m;j++){
  43. if(mat[i][j]=='@'){
  44. sum++;
  45. mat[i][j]='*';
  46. BFS(i,j);
  47. }
  48. }
  49. }
  50. printf("%d\n",sum);
  51. }
  52. return ;
  53. }

HDU 1241.Oil Deposits-求连通块DFS or BFS的更多相关文章

  1. hdu 1241 Oil Deposits(DFS求连通块)

    HDU 1241  Oil Deposits L -DFS Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & ...

  2. DFS(连通块) HDU 1241 Oil Deposits

    题目传送门 /* DFS:油田问题,一道经典的DFS求连通块.当初的难题,现在看上去不过如此啊 */ /************************************************ ...

  3. HDU 1241 Oil Deposits --- 入门DFS

    HDU 1241 题目大意:给定一块油田,求其连通块的数目.上下左右斜对角相邻的@属于同一个连通块. 解题思路:对每一个@进行dfs遍历并标记访问状态,一次dfs可以访问一个连通块,最后统计数量. / ...

  4. HDOJ(HDU).1241 Oil Deposits(DFS)

    HDOJ(HDU).1241 Oil Deposits(DFS) [从零开始DFS(5)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架 ...

  5. HDU 1241 Oil Deposits(石油储藏)

    HDU 1241 Oil Deposits(石油储藏) 00 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)   Probl ...

  6. HDU 1241 - Oil Deposits - [BFS]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1241 题意: 求某块平面上,连通块的数量.一个油田格子若周围八个方向也有一个油田格子,则认为两者相连通 ...

  7. HDU 1241 Oil Deposits (DFS)

    题目链接:Oil Deposits 解析:问有多少个"@"块.当中每一个块内的各个"@"至少通过八个方向之中的一个相邻. 直接从"@"的地方 ...

  8. HDU 1241 Oil Deposits (DFS or BFS)

    链接 : Here! 思路 : 搜索判断连通块个数, 所以 $DFS$ 或则 $BFS$ 都行喽...., 首先记录一下整个地图中所有$Oil$的个数, 然后遍历整个地图, 从油田开始搜索它所能连通多 ...

  9. HDU 1241 Oil Deposits【DFS】

    解题思路:第一道DFS的题目--- 参看了紫书和网上的题解-- 在找到一块油田@的时候,往它的八个方向找,直到在能找到的范围内没有油田结束这次搜索 可以模拟一次DFS,比如说样例 在i=0,j=1时, ...

随机推荐

  1. 求 1 到 n 的所有数的约数和

    求 1 到 n 的所有数的约数和 暴力方法就是枚举每个数,算出他的约数和即可,这样有点慢. 另一种思路,枚举约数,判断他是谁的约数,并记录(即他的倍数有多少个),在乘以他自己. n/i求的是n以内,i ...

  2. MD5碰撞

    if ( $_POST['param1'] !==$_POST['param2'] && md5($_POST['param1']) === md5($_POST['param2']) ...

  3. 对于xss等有关的html,url,unicode编码做的一个小总结。

    参考:http://bobao.360.cn/learning/detail/292.html,算是对前部分作一个总结性的学习. 1<a href="%6a%61%76%61%73%6 ...

  4. Shell脚本直接执行sql语句和不显示列名

    在shell脚本编程的时候,可以通过在mysql连接命令添加-N和-e参数实现查询结果不显示列名和直接执行sql语句操作 demo $(mysql -h ${HOST} -u ${USER} -p${ ...

  5. Python-S9-Day123——爬虫两示例

    01 今日内容回顾 02 内容回顾和补充:面向对象约束 03 爬虫之抽屉新热榜 04 爬虫之抽屉自动登录(一) 05 爬虫之抽屉自动登录(二) 06 爬虫之登录github(一) 07 爬虫之登录gi ...

  6. Python 模块搜索路径

    Python 会在什么地方寻找文件来导入模块? 使用命名为 path 变量的存储在标准 sys 模块 下的一系列目录名和 ZIP 压缩文件. 你可以读取和修改这个列表.下面是在我的 Mac 上 Pyt ...

  7. 混淆矩阵、准确率、召回率、ROC曲线、AUC

    混淆矩阵.准确率.召回率.ROC曲线.AUC 假设有一个用来对猫(cats).狗(dogs).兔子(rabbits)进行分类的系统,混淆矩阵就是为了进一步分析性能而对该算法测试结果做出的总结.假设总共 ...

  8. jquery实现跨域请求(复制)

    很多开发人员在使用jquery在前端和服务器端进行数据交互,所以很容易会认为在前端利用jquery就可以读取任何站点的数据了.近日在进行开 发时,因为要和第三方公司的一个项目进行数据的共享,因为考虑多 ...

  9. AngularJs 特性 之 MVC

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script sr ...

  10. 【bzoj4589】Hard Nim FWT

    题目描述 Claris和NanoApe在玩石子游戏,他们有n堆石子,规则如下: 1. Claris和NanoApe两个人轮流拿石子,Claris先拿. 2. 每次只能从一堆中取若干个,可将一堆全取走, ...