Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=1241
Oil deposits
Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 18297 Accepted Submission (s): 10548
Problem Description
The 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.
Input
The 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 is either *‘, representing the absence of oil, or @ ', representing a oil pocket.
Output
For 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.
Main topic:
Given two numbers m and N, which represent m rows, n columns, and then give a character matrix, find several @ components, a total of several ....
Problem-Solving ideas: BFS Search
#include <iostream>#include <cstdio>using namespace STD;Const intmaxn= $;Char Map[MAXN] [MAXN];intdir[8][2]={{1,0},{-1,0},{0,1},{0,-1},{1,1},{-1,1},{1,-1},{-1,-1}};intM,n,ans;voidDfsintXintY) { for(intI=0; i<8; i++) {intnx=x+dir[i][0];intny=y+dir[i][1];if(nx>=0&& nx<n && ny>=0&& ny<m &&Map[NX] [ny]==' @ ') {Map[NX] [ny]=' * '; DFS (NX, NY); } }}intMain () { while(~scanf("%d%d", &n,&m), m,n) {ans=0;/*getchar (); for (int. I=1; i<=n; i++) for (int j=1; j<=m; j + +) scanf ("%c", &map[i][j]); */ for(intI=0; i<n; i++)scanf('%s ',Map[i]); for(intI=0; i<n; i++) { for(intj=0; j<m; J + +) {if(MapI [j] = =' @ ') {MapI [j]=' * '; ans++;//1cout<<ans<<endl;DFS (I,J); } } }printf("%d\n", ans); }return 0;}/*sample Input1 1*3 5*@*@***@***@*@*1 8@@****@*5 5 ****@*@@*@*@**@@@@*@@@**@0 0 Sample output0122*/
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Hdu 1241 Oil Deposits