Oil Deposits
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 4 Accepted Submission(s) : 3
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
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
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
* *@*@*
**@**
*@*@* @@****@* ****@
*@@*@
*@**@
@@@*@
@@**@
Sample Output
0
1
2
2
Source
Mid-Central USA 1997
这是道经典dfs搜索题,难度属于入门级,没有太难的地方。
下面是代码:
#include <iostream>
using namespace std;
char a[][];
int dx[] = {,,,,,-,-,-};
int dy[] = {,,,-,-,-,,};
int m,n;
void dfs(int x,int y) //将(x,y)坐标有联系的所有坐标都标记为 *
{
a[x][y]='*';
for(int i=;i<;i++){
int nx = x+dx[i];
int ny = y+dy[i];
if( nx< || ny< || nx>m || ny>n) //如果越界
continue;
if(a[nx][ny]!='@') //如果这样走的下一步不是 @
continue;
dfs( nx , ny );
}
}
int main()
{
while(cin>>m>>n,m!=){
int _count=;
for(int i=;i<=m;i++)
for(int j=;j<=n;j++)
cin>>a[i][j];
for(int i=;i<=m;i++)
for(int j=;j<=n;j++){
if(a[i][j]=='@'){
_count++;
dfs(i,j);
}
}
cout<<_count<<endl;
}
return ;
}
Freecode : www.cnblogs.com/yym2013