Oil depositsTime limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others) Total submission (s): 13119 Accepted Submission (s): 7600
Problem DescriptionThe Geosurvcomp Geologic Survey company are 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 Numer OUs square plots. It then analyzes each plot separately, using a sensing equipment to determine whether or not of the plot contains oil. A plot containing oil is called a pocket. If the pockets is adjacent, then they is part of the same oil deposit. Oil deposits can is quite large and may contain numerous pockets. Your job is to determine how many different oil deposits be contained in a grid.
Inputthe input file contains one or more grids. Each of the grids 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 <= and 1 <= n <= 100. Following this is m lines of n characters each (not counting the end-of-line characters). Each character corresponds to one plot, and are either ' * ', representing the absence of oil, or ' @ ', representing a oil PO Cket.
Outputfor each grid, output the number of distinct oil deposits. The different pockets is part of the same oil deposit if they is adjacent horizontally, vertically, or diagonally. An oil deposit won't contain more than pockets.
Sample Input Sample Output0122#include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <iostream > #include <algorithm> #include <queue> #include <stack>using namespace Std;char map[110][110];int F[8][2]={{1,0},{-1,0},{0,1},{0,-1},{-1,1},{-1,-1},{1,-1},{1,1}};int n,m;void DFS (int p,int q) {if (p<0| | p>=n| | q<0| | q>=m| | map[p][q]!= ' @ ') return; else { map[p][q]= '. '; for (int i=0;i<8;i++) { DFS (p+f[i][0],q+f[i][1]); } }} int main () {while (~SCANF ("%d%d", &n,&m)) {GetChar (); if (!n &&!m) break;for (int i=0;i<n;i++) { for (int j=0;j<m;j++) {cin>>map[i][j];}} int sum=0;for (int i=0;i<n;i++) {for (int j=0;j<m;j++) {if (map[i][j]== ' @ ') {DFS (i,j); sum++;}}} printf ("%d\n", sum);} return 0;}
|