Time Limit: 3000MS | Memory Limit: Unknown | 64bit IO Format: %lld & %llu |
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.
1 1 * 3 5 *@*@* **@** *@*@* 1 8 @@****@* 5 5 ****@ *@@*@ *@**@ @@@*@ @@**@ 0 0
0 1 2 2
很简单的一道题目!懂了八方向怎么表示,以及怎么给不同类的块 编号。
#include<iostream> #include<string.h> using namespace std; char cnt[101][101]; int vis[101][101]; int count,m,n; void dfs(int x,int y , int id) { for(int i=-1; i<=1; i++) for(int j=-1; j<=1; j++) { int tx=x+i; int ty=y+j; if(tx>=0&&ty>=0&&tx<m&&ty<n&&cnt[tx][ty]=='@'&&!vis[tx][ty]) { vis[x][y]=id; dfs(tx,ty,id); } } return ; } int main() { while(cin>>m>>n,m) { memset(vis,0,sizeof(vis)); for(int i=0; i<m; i++) for(int j=0; j<n; j++) cin>>cnt[i][j]; int count=0; for(int k=0; k<m; k++) for(int q=0; q<n; q++) { if(!vis[k][q]&&cnt[k][q]=='@') { vis[k][q]=++count; dfs(k,q,count); } } cout<<count<<endl; } return 0; }
原文地址:http://blog.csdn.net/lsgqjh/article/details/45586415