Oil Deposits
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 40849 Accepted Submission(s): 23680
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
Source
Recommend
Statistic | Submit | Discuss | Note
思路还是很简单的:就是搜索,dfs,bfs都能写,思路都是,标记已经访问,利用坐标搜索;
dfs代码:
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
char k[105][105];
int n,m,gg;
int x[8]={-1,-1,-1,0,0,1,1,1};
int y[8]={-1,0,1,-1,1,-1,0,1};
void dfs(int ix,int iy)
{
k[ix][iy]='*';//已经访问将@变为*
for(int g=0;g<8;g++){
int sx=x[g]+ix;
int sy=y[g]+iy;
if(sx<0||sx>=n||sy<0||sy>=m) continue;
if(k[sx][sy]=='@') dfs(sx,sy);//符合条件,接着搜索
}
}
int main()
{
while(scanf("%d%d",&n,&m),n&&m)
{
gg=0;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
cin>>k[i][j];//用scanf的话,需要注意getchar()吸收空格和换行
}
}
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(k[i][j]=='@'){//符合条件,进行搜索
gg++;
dfs(i,j);
}
}
}
cout<<gg<<endl;
}
}
bfs代码:
#include<iostream>
#include<cstdio>
#include<queue>
#include<string>
#include<cstring>
using namespace std;
const int maxn=110;
struct node
{
int x,y;
}stp;
int n,m;
char matrix[maxn][maxn];
bool inq[maxn][maxn];
int x[8]={0,0,1,-1,-1,1,-1,1};
int y[8]={1,-1,0,0,-1,-1,1,1};
bool judge(int a,int b)
{
if(a>=n||a<0||b>=m||b<0) return false;
if(matrix[a][b]=='*'||inq[a][b]==true) return false;
return true;
}
void bfs(int a,int b)
{
queue<node>q;
stp.x=a,stp.y=b;
q.push(stp);//入队列
inq[a][b]=true;
while(!q.empty())
{
node top=q.front();
q.pop();
for(int i=0;i<8;i++)
{
int newx=top.x+x[i];
int newy=top.y+y[i];
if(judge(newx,newy))//符合条件进行搜索
{
stp.x=newx,stp.y=newy;
q.push(stp);
inq[newx][newy]=true;//标记为已经访问
}
}
}
}
int main()
{
while(cin>>n>>m,n||m)
{
memset(matrix,0,sizeof(matrix));
memset(inq,0,sizeof(inq));
for(int i=0;i<n;i++) cin>>matrix[i];//用scanf的话,注意用getchar吸收空格和换行
int ans=0;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(matrix[i][j]=='@'&&inq[i][j]==false)//符合条件进行搜索
{
ans++;
bfs(i,j);
}
}
}
cout<<ans<<endl;
}
}