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.
就是BFS了,水题吧。
代码如下:
#include<iostream>
#include<cstring> using namespace std; int vis[][];
int ans;
int M,N;
int fir,las,que[]; bool judge(int x,int y)
{
if(x<=||y<=||x>M||y>N)
return ; if(vis[x][y]==)
return ; return ;
} void bfs(int x,int y)
{
int temp,t1,t2; que[las++]=x*+y;
vis[x][y]=; while(las-fir)
{
temp=que[fir++]; t1=temp/;
t2=temp%; --t1;
if(judge(t1,t2))
{
vis[t1][t2]=;
que[las++]=t1*+t2;
}
t1+=;
if(judge(t1,t2))
{
vis[t1][t2]=;
que[las++]=t1*+t2;
}
--t1;
--t2;
if(judge(t1,t2))
{
vis[t1][t2]=;
que[las++]=t1*+t2;
}
t2+=;
if(judge(t1,t2))
{
vis[t1][t2]=;
que[las++]=t1*+t2;
}
t2-=;
--t1;
if(judge(t1,t2))
{
vis[t1][t2]=;
que[las++]=t1*+t2;
}
t1+=;
if(judge(t1,t2))
{
vis[t1][t2]=;
que[las++]=t1*+t2;
}
t2+=;
if(judge(t1,t2))
{
vis[t1][t2]=;
que[las++]=t1*+t2;
}
t1-=;
if(judge(t1,t2))
{
vis[t1][t2]=;
que[las++]=t1*+t2;
}
}
} void slove()
{
ans=;
fir=las=; for(int i=;i<=M;++i)
for(int j=;j<=N;++j)
if(vis[i][j])
{
++ans;
bfs(i,j);
}
} int main()
{
ios::sync_with_stdio(false); char c; for(cin>>M>>N;M;cin>>M>>N)
{
for(int i=;i<=M;++i)
for(int j=;j<=N;++j)
{
cin>>c;
vis[i][j]=c=='@'?:;
} slove();
cout<<ans<<endl;
} return ;
}