C - Oil Deposits
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.
1 1 * 3 5 *@*@* **@** *@*@* 1 8 @@****@* 5 5 ****@ *@@*@ *@**@ @@@*@ @@**@ 0 0
0 1 2 2
这个题目是直接套用模板,注意标记走过的路就行了;
#include<cstdio> #include<cstring> #include<iostream> using namespace std; const int MAX = 1e2 + 10; int ans,visit[MAX][MAX],m,n; char mapp[MAX][MAX]; int fx[8]={0,0,-1,1,-1,1,-1,1}; int fy[8]={-1,1,0,0,-1,-1,1,1}; void dfs(int x, int y){ visit[x][y]=1;//biaoji for(int i=0; i<8; i++){ int xx=x+fx[i],yy=y+fy[i]; if(xx >= 0 && yy >= 0 && xx < n && yy < m && !visit[xx][yy] && mapp[xx][yy] == '@'){ dfs(xx,yy); } } } int main(){ while(cin >> n >> m,(n||m)){ memset(visit,0,sizeof(visit)); for(int i=0; i<n; i++){ cin >> mapp[i]; } ans=0; for(int i=0; i<n; i++){ for(int j=0; j<m; j++){ if(mapp[i][j]=='@'&&!visit[i][j]){ ans++; dfs(i,j); } } } cout << ans << endl; } return 0; }