题意:找出地图上的油层数,每个油层由多个油藏组成,这些油藏相邻,常规的搜索问题,每次向八个方向搜寻油藏。

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1241

友情链接:点这里。

    此处为DFS代码:

#include<stdio.h>
#include<queue>
#include<iostream>
#include<string.h>
using namespace std;
int m,n;
char mp[105][105];
void dfs(int a,int b)
{mp[a][b]='*';//找到了油田就把它改成*避免重复查找 for(int dx=-1;dx<=1;dx++){for(int dy=-1;dy<=1;dy++){int nx=a+dx;int ny=b+dy;if(nx>=1&&nx<=m&&ny>=1&&ny<=n&&mp[nx][ny]=='@')dfs(nx,ny);//最关键的地方,递归找到的新油田。 }}
}
int main(void)
{while(cin>>m>>n&&m&&n)//这里当m和m为0的时候就退出循环 {int sum=0;//每次初始化sum为0。 for(int i=1;i<=m;i++){for(int j=1;j<=n;j++){cin>>mp[i][j];}}for(int i=1;i<=m;i++){for(int j=1;j<=n;j++){if(mp[i][j]=='@'){dfs(i,j);sum++;//每找到一个油藏sum就++。 }}}printf("%d\n",sum);}return 0;
}

当然这种题用bfs也可以,只是觉得DFS写着简便易懂一点,bfs就不详细论述了,毕竟我这题用bfs做ML了好几次,,,,

#include<iostream>
#include<queue>
#include<stdio.h>
#include<string.h>
#define N 100
using namespace std;
int r,c,ans;
char mp[N][N];
bool vis[N][N];
int fx[8]={1,0,-1,0,-1,1,-1,1};
int fy[8]={0,1,0,-1,-1,-1,1,1};
struct node
{int x,y;
};bool check(int x,int y)
{if(!vis[x][y] && mp[x][y]=='@' && x>=0 && x<r && y>=0 && y<c )return 1;return 0;
}void bfs(int x,int y)
{queue<node> q;q.push({x,y});while(q.size()){node now=q.front();q.pop();for(int i=0;i<8;i++){int nextx=now.x + fx[i];int nexty=now.y + fy[i];if(check(nextx,nexty)){vis[nextx][nexty]=1;q.push({nextx,nexty});}}}return ;} int main()
{ios_base::sync_with_stdio;while(cin>>r>>c && r){ans=0;memset(vis,0,sizeof vis);memset(mp,0,sizeof mp);for(int i=0;i<r;i++){for(int j=0;j<c;j++){cin>>mp[i][j];}}for(int i=0;i<r;i++){for(int j=0;j<c;j++){if(mp[i][j]=='@' && !vis[i][j]){bfs(i,j);ans++;}}}cout<<ans<<endl;}return 0;
}

小结一下:此题推荐使用深搜,每次搜索都能将一片油藏搜索出来,不过这两种算法最好都要掌握。

Oil Deposits(油田问题)相关推荐

  1. 【DFS】【水】HDU1241 Oil Deposits 油田资源

    GeoSurvComp地质调查公司负责探测地下石油储藏. GeoSurvComp现在在一块矩形区域探测石油,并把这个大区域分成了很多小块.他们通过专业设备,来分析每个小块中是否蕴藏石油.如果这些蕴藏石 ...

  2. 百-寒-进-16-油田Oil Deposits

    Oil Deposits 题目描述 GeoSurvComp地质调查公司负择探测地下石油矿床.该公司一次处理一个大的矩形区域,并创建一个网格,将土地划分为许多正方形地块.然后,它分别分析每个地块,使用传 ...

  3. L - Oil Deposits HDU - 1241

    L - Oil Deposits HDU - 1241 注意写check的时候要先判x,y是不是越界了,不然直接用数组可能RE, 也可能直接WA了 #include<cstdio> #in ...

  4. UVa572 Oil Deposits DFS求连通块

    技巧:遍历8个方向 for(int dr = -1; dr <= 1; dr++)for(int dc = -1; dc <= 1; dc++)if(dr != 0 || dc != 0) ...

  5. HDU-1241 Oil Deposits (DFS)

    Oil Deposits Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total ...

  6. 【POJ - 1562】Oil Deposits (dfs搜索,连通块问题)

    题干: The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. G ...

  7. 【HDU - 1241】Oil Deposits (连通块问题 属于求大海中的岛屿个数 类似问题)

    题干: The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. G ...

  8. L - Oil Deposits

    题目描述 The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. ...

  9. NUC1397 Oil Deposits【DFS】

    Oil Deposits 时间限制: 1000ms 内存限制: 65535KB 问题描述 The GeoSurvComp geologic survey company is responsible ...

最新文章

  1. UITextField总结--博主总结的真好
  2. linux c send recv MSG_NOSIGNAL参数
  3. 其他团队对本团队评价的总结
  4. crontab 日志_Nginx日志切割并计划任务自动上传到FTP服务器
  5. 编码设置过滤的注意点 2021-04-27
  6. 数据解析(XML,JSON)
  7. c语言自动按次序创建文件,读取文件建立顺序表实现增,删,查,取(C语言)...
  8. 5G NR标准协议架构-5G协议应该怎么看?
  9. 不卡顿成用户购机第一要素,Mate 9深得人心
  10. 星星之火,可以燎原|”分阶段“裂变带来1周1万+垂直户
  11. 贝壳DMP平台建设实践
  12. CHARACTERISTIC DEFINITION
  13. mysql中on用法_详解mysql中的Using与On的用法
  14. unsupported GNU version! gcc versions later than 6 are not supported!
  15. 密码打马赛克已经不安全了!这款开源的去“马赛克“工具一秒还原
  16. 大三学生——要不要上研究生?
  17. 对于“2017面向对象程序设计(Java)第五周工作总结”存在问题的反馈及本周教学计划...
  18. 被讨厌的勇气读书笔记
  19. 【读书笔记】打开心智
  20. idea创建web项目运行报404错误_IDEA JavaWeb项目启动运行后出现404错误的解决方法...

热门文章

  1. ubuntu 16.04 创建新用户
  2. NGINX+PHP+ZABBIX,推荐
  3. POJ3261 Milk Patterns 【后缀数组】
  4. Spring 创建代理类流程跟踪
  5. UI基础(四)之tableView (cell重用、原型cell、静态cell)/xib注意事项
  6. Linux-iptables初识
  7. 伪响应式开发(PC和Mobile分离)
  8. JS 学习笔记--8---Function类型
  9. poj 2516(最小费用最大流)
  10. context-param标签含义及与init-param标签的异同(转)