Online Judge Problem Set Authors Online Contests User Web Board Home Page F.A.Qs Statistical Charts Problems Submit Problem Online Status Prob.ID: Register Update your info Authors ranklist Current Contest Past Contests Scheduled Contests Award Contest 657973052 Log Out Mail:0(0) Login Log Archive Language:DefaultOil Deposits Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 9777 Accepted: 5357 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 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 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 Mid-Central USA 1997 [Submit] [Go Back] [Status] [Discuss] Home Page Go Back To top -------------------------------------------------------------------------------- All Rights Reserved 2003-2011 Ying Fuchen,Xu Pengcheng,Xie Di Any problem, Please Contact Administrator
#include <iostream> #include<string.h> #include <stdio.h> using namespace std; const int Nmax =102; int n,m; int a[Nmax][Nmax]; bool visit[Nmax][Nmax]; struct Dir { int x; int y; }dir[8]={{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}}; void DFS(int i,int j) { visit[i][j]=1; for(int k=0;k<8;k++) { int newi=i+dir[k].x; int newj=j+dir[k].y; if(newi>0&&newi<=n&&newj>0&&newj<=m) { if(!visit[newi][newj]&&a[newi][newj]) DFS(newi,newj); } } } int main() { int i,j,cnt; char ch; while(scanf("%d%d",&n,&m),n||m) { cnt=0; memset(visit,0,sizeof(visit)/sizeof(bool)); for(i=1; i<=n; i++) { for(j=1; j<=m; j++) { cin>>ch; if('*'==ch) a[i][j]=0; else a[i][j]=1; } } for(i=1; i<=n; i++) { for(j=1; j<=m; j++) { if(a[i][j]&&!visit[i][j]) { DFS(i,j); cnt++; } } } printf("%d\n",cnt); } return 0; } /*优化的代码。visit巧妙的优化了*/ #include<stdio.h> #include <cstdlib> #include <iostream> using namespace std; char map[101][101]; int ans,m,n,newx,newy; //定义方向结构体 struct Dir { int x; int y; }d[8]={{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}};//该题为8个方向 void find(int a,int b){ //搜索函数 map[a][b]='*'; for(int k=0;k<8;k++){ newx=a+d[k].x; newy=b+d[k].y; if(newx>=0 && newx<m && newy>=0 && newy<n && map[newx][newy]=='@'){ find(newx,newy); } } } int main(int argc, char *argv[]){ int i; while(scanf("%d%d",&m,&n),m||n){ ans=0; for(i=0;i<m;i++) cin>>map[i]; for(i=0;i<m;i++) for(int j=0;j<n;j++) if(map[i][j]=='@'){ ans++; find(i,j); } cout<<ans<<endl; } return EXIT_SUCCESS; }
//显然用BFS更麻烦一些 #include <iostream> #include<queue> #include<string.h> #include <stdio.h> using namespace std; const int Nmax =102; int n,m; char a[Nmax][Nmax]; struct Dir { int x; int y; } dir[8]= {{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}}; struct POS { int i; int j; } pos; void BFS(int i,int j) { int newi,newj; queue<POS> q; pos.i=i; pos.j=j; q.push(pos); while(!q.empty()) { pos=q.front(); q.pop(); i=pos.i; j=pos.j; a[i][j]='*'; for(int k=0; k<8; k++) { newi=i+dir[k].x; newj=j+dir[k].y; if(newi>0&&newi<=n&&newj>0&&newj<=m&&'@'==a[newi][newj]) { a[newi][newj]='*'; pos.i=newi; pos.j=newj; q.push(pos); } } } } int main() { int i,j,cnt; while(scanf("%d%d",&n,&m),n||m) { cnt=0; for(i=1; i<=n; i++) for(j=1; j<=m; j++) cin>>a[i][j]; for(i=1; i<=n; i++) for(j=1; j<=m; j++) if('@'==a[i][j]) { BFS(i,j); cnt++; } printf("%d\n",cnt); } return 0; }