Oil Deposits

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

————————————————————————————————————————————————————

DFS求连通块入门题,直接把地图跑一边,找到符合点计数一下,dfs遍历所有的连通块变成普通的。


#include<iostream>
#include<cmath>
using namespace std;
char map[105][105];
int m, n, t;
int dir[8][2] = { { -1, -1 }, { -1, 0 }, { -1, 1 }, { 0, -1 }, { 0, 1 }, { 1, -1 }, { 1, 0 }, {1,1} }; void dfs(int si, int sj)
{
if (si <= 0 || sj <= 0 || si > m || sj > n)
return;
for (int i = 0; i < 8; i++)
{
if (map[si + dir[i][0]][sj + dir[i][1]] != '*')
{
map[si + dir[i][0]][sj + dir[i][1]] = '*';
dfs(si + dir[i][0], sj + dir[i][1]);
}
}
return;
}
int main()
{
while (cin >> m >> n&&(m||n))
{
for (int i = 1; i <= m;i++)
for (int j = 1; j <= n; j++)
cin >> map[i][j];
t = 0;
for (int i = 1; i <= m; i++)
for (int j = 1; j <= n; j++)
{
if (map[i][j] == '@')
{
map[i][j] = '*';
t++;
dfs(i, j);
}
}
printf("%d\n", t);
}
return 0; }

最新文章

  1. 第9章 Shell基础(4)_Bash的运算符及环境变量配置文件
  2. EntityFramework left join
  3. Dom方式解析XML
  4. Workflow Mailer Notifications设置
  5. ASP+Access UTF-8 网页乱码问题解决办法
  6. [Angular 2] @Input &amp; @Output Event with ref
  7. Looper Handler 多线程
  8. CSS 响应式设计
  9. FragmentCustomAnimation实现Fragment的界面切换
  10. Struts2学习笔记③
  11. ehcache memcache redis 区别
  12. iOS使用自签名证书实现HTTPS请求
  13. RC terms.
  14. android 自定义Button,抛弃写shape文件
  15. js判断一个变量是数组还是对象
  16. JAVA自学笔记04
  17. php获取指定月份月初和月末的时间戳
  18. CentOS6.5下搭建Samba服务实现与Windows系统之间共享文件资源
  19. confluence 新tab 页面打开 kibana short link
  20. java调用微信扫一扫

热门文章

  1. mongodb first
  2. How a non-windowed component can receive messages from Windows
  3. dubbo dubbo.xsd 报错
  4. wireshark使用相关问题
  5. mysql 导出sql结果成csv文件
  6. Ubuntu 分辨率更改 xrandr Failed to get size of gamma for output default
  7. python之递归锁【Rlock】
  8. Fragment----静态创建碎片
  9. win 下 nginx 与 php的配置
  10. win7系统administrator用户提示没有管理员权限,造装驱动安装错误,软件无法使用