题目:

The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvCompworkswithonelargerectangularregionoflandatatime,andcreatesagridthatdivides 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 containsone 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

题意:

和迷宫的输入差不多,‘*’是墙,‘@’是油田,以一个油田为中心,如果它的东南西北一个各个角落,一共八个方向也有油田的话,这些油田就算作一个油田。

分析:

查找到‘@’的位置,找到一个,油田加1,对找到的‘@’的四周进行DFS查找,刚开始我以为和迷宫的做法差不多,我把查找过的地方用一个数组标记一下,后来测试结果不对,

才发现在某一次存在题意中的相邻油田时,在主函数中会加1,而实际上这块油田我已经计算进去了,这样就重复了。正确的做法应该是把计算进去的那块油田变为‘*’,这样

就不存在重复计算了!!!

AC代码:

#include<iostream>
#include<cstring>
#include<cstdio>
char a[][];
int row,col;
int dir[][]=
{
{,},
{,},
{,-},
{,-},
{,},
{-,},
{-,},
{-,-}
};
using namespace std;
void dfs(int i,int j)
{
a[i][j]='*';
for (int k=;k<;k++)
{
int x=i+dir[k][];
int y=j+dir[k][];
if (x>=&&x<=row&&y>=&&y<=col&&a[x][y]=='@')
dfs(x,y);
}
return ;
}
int main()
{
while ((cin>>row>>col)&&(row!=||col!=))
{
int c=;
getchar();
for (int i=;i<=row;i++)
for (int j=;j<=col;j++)
cin>>a[i][j];
for (int i=;i<=row;i++)
for (int j=;j<=col;j++)
if (a[i][j]=='@')
{
dfs(i,j);
c++;
}
cout << c << endl;
}
return ;
}

最新文章

  1. Excel 函数VLOOKUP初学者使用指南
  2. 常用的sql数据库语句
  3. offer
  4. iOS 一个控件只能拥有一个父类
  5. 【URAL 1917】Titan Ruins: Deadly Accuracy(DP)
  6. nessus网页报错: Scans can not be saved without a policy. Please create a policy before proce
  7. (spring-第8回【IoC基础篇】)BeanDefinition在IoC容器中的注册
  8. 添加iPhone设备的udid之后,重新生成开发证书(Development)
  9. 解决vsftpd 530 Permission denied报错
  10. javascript实现倒计时程序
  11. Mysql高级之主从复制
  12. Hyperledger Fabric 1.0 从零开始(五)——运行测试e2e
  13. GIT入门笔记(10)- 多种撤销修改场景和对策
  14. EasyPopup
  15. adt安装----只为测试使用adb命令,故无需安装过于复杂
  16. 国内优秀MVC开源框架jfinal简介
  17. Data truncation: Truncated incorrect DOUBLE value:
  18. 快速近似最近邻搜索库 FLANN - Fast Library for Approximate Nearest Neighbors
  19. CSS中的onmouseover和hover有什么区别
  20. Discovering Gold LightOJ - 1030 (概率dp)

热门文章

  1. SRS|Stratified sampling|系统抽样|Cluster sampling|multistage sampling|
  2. 第2章 Innodb 存储引擎
  3. fiddler抓包可以抓到电脑数据抓不到手机上的数据------防火墙问题
  4. Thymeleaf标签学习
  5. pom配置项目build jar 包含源码的配置
  6. [LC] 285. Inorder Successor in BST
  7. T-shirt
  8. XiaoXiao
  9. nodejs快速测试
  10. [LC] 83. Remove Duplicates from Sorted List