Oil Deposits

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 11984    Accepted Submission(s): 6952

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
 

题目大意:找出连续相连的@有几条 由于我是英文渣题目都没看清楚就做 错了好久,还理解错了,待会会讲我错哪了;;;;;= 。=

AC代码:

//Oil Deposits
#include <iostream>
#include <stdio.h>
using namespace std; char map[101][101];
int n, m, sum; void dfs(int i, int j)
{
//若该点不可行或越界,返回
if(map[i][j]!='@' || i<0 || j<0 || i>=m || j>=n) return;
else //否则,标记该点为不可行,再往8个方向深搜
{
map[i][j] = '*';
dfs(i-1, j-1);
dfs(i-1, j);
dfs(i-1, j+1);
dfs(i, j-1);
dfs(i, j+1);
dfs(i+1, j-1);
dfs(i+1, j);
dfs(i+1, j+1);
}
} int main()
{
int i, j;
while(cin>>m>>n)
{
if(m==0 || n==0) break;
sum = 0;
for(i = 0; i < m; i++)
for(j = 0; j < n; j++)
cin>>map[i][j];
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
{
if(map[i][j] == '@')
{
dfs(i, j);
sum++;
}
}
}
cout<<sum<<endl;
} return 0;
}

我之前的做法(错的):

错误:一开始没注意到对角线也可以 后来修正了,但是还是WA

错误2:WA的原因就是我多此一举还用hash存 我看错题目以为相同长度的只能算一次 (T_T只看样例去理解题目意思的好桑不起啊~~)

//============================================================================
// Name : 2333.cpp
// Author : zjx
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================ #include <iostream>
using namespace std;
char op[105][105];
bool num[105][105];
bool hash[10010];
int dfs(int n,int m){
if(num[n][m]||op[n][m]=='*'){
return 0;
}
num[n][m]=true;
int sum;
sum=1+dfs(n-1,m)+dfs(n+1,m)+dfs(n,m+1)+dfs(n,m-1)+dfs(n+1,m+1)+dfs(n-1,m-1)+dfs(n+1,m-1)+dfs(n-1,m+1);
return sum;
}
int main()
{
int a,b,i,j;
while(cin>>a>>b){
if(a==0&&b==0)break;
memset(num,false,sizeof(num));
memset(hash,false,sizeof(hash));
for(i=1;i<=a;i++)
for(j=1;j<=b;j++){
cin>>op[i][j];
}
int sum=0;int temp;
for(i=0;i<=a+1;i++){op[i][0]='*';op[i][b+1]='*';}
for(j=0;j<=b+1;j++){op[0][j]='*';op[a+1][j]='*';}
for(i=1;i<=a;i++)
for(j=1;j<=b;j++){
temp=dfs(i,j);
if(temp!=0&&hash[temp]==false){
sum++;hash[temp]=true;
}
}
cout<<sum<<endl;
}
return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

最新文章

  1. C#对.zip 存档读取和写入
  2. overflow
  3. [译]Godot系列教程三 - 场景实例化(续)
  4. ThreadLocal
  5. WebStorm phpStorm 注册码
  6. Brew 编译mod错误Error: L6265E: Non-RWPI Section libspace.o(.bss) cannot be assigned to PI Exec region ER_ZI
  7. 【置换,推理】UVa 1315 - Creaz tea party
  8. Number of Rectangles in a Grid
  9. JSON数据的中文乱码问题
  10. ZooKeeper 分布式锁实现
  11. CSS Clip剪切元素实例
  12. CGI编程完全手册
  13. 一句话输出NGINX日志访问IP前十位排行
  14. 同时处理html+js+jquery+css的插件安装(Spket&amp;Aptana插件安装)
  15. springmvc对于JSON对象的处理
  16. javascript 零碎笔记
  17. [Swift]LeetCode998. 最大二叉树 II | Maximum Binary Tree II
  18. linux日常使用指令总结
  19. SpringMCV跨域
  20. npm ERR! missing script: dev 报错解决

热门文章

  1. thinkPHP写txt日志文件
  2. Python使用代理的方法
  3. MySQL用全库备份数据恢复单表数据
  4. 【转载++】fopen返回0(空指针NULL)且GetLastError是0
  5. IP组播 MulticastChannel接口 DatagramChannel实现
  6. golang基础--Interface接口
  7. 20155224聂小益 2016-2017-2 《Java程序设计》第1周学习总结
  8. 20155315 2016-2017-2《Java程序设计》课程总结
  9. debug 调试原理理解
  10. 【转载】Direct3D纹理映射