Oil Deposits
Time limit:1000 ms
Memory limit:32768kb
64bit Io format:% I64d & % i64u
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 in numerous pockets. your job is to determine how many different oil deposits are contained in a grid.
Input
The input file 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
For each grid, output the number of distinct oil deposits. two different pockets are part of the same oil deposit if they are adjacent horizontally, vertically, or diagonally. an oil deposit will not contain in more than 100 pockets.
Sample Input
1 1*3 5*@*@***@***@*@*1 8@@****@*5 5 ****@*@@*@*@**@@@@*@@@**@0 0
Sample output
0122
Question:
@ Indicates the region where the oil is located. A map shows the number of regions. It can be in eight directions.
Solution:
Typical search questions: BFS and DFS.
Code 1 (BFS ):
# Include <iostream> # include <queue> # include <cstdio> # include <cstring> # include <string> # define n 110 using namespace STD; struct node {int X, y; node (INT X0 = 0, int Y0 = 0) {// you need to write the constructor. Otherwise, it will not be compiled when it is used. X = x0; y = y0 ;}}; int directionx [8] = {, 0,-1,-1,-1, 0, 1}, n, m, CNT, X, Y; int directiony [8] = {0, 1, 1, 0,-1,-1,-1}; queue <node> path; bool visited [N] [N]; string map [N]; void BFS (node s) {for (INT I = 0; I <8; I ++) {If (0 <= S. X + directio NX [I] & S. X + directionx [I] <n & 0 <= S. Y + directiony [I] & S. Y + directiony [I] <m) {If (Map [S. X + directionx [I] [S. Y + directiony [I] = '@'&&! Visited [S. X + directionx [I] [S. Y + directiony [I]) {visited [S. X + directionx [I] [S. Y + directiony [I] = true; Path. push (node (S. X + directionx [I], S. Y + directiony [I]) ;}} path. pop (); If (path. size ()> 0) BFS (path. front ();} int main () {While (scanf ("% d", & N, & M )! = EOF & (N | M) {CNT = 0; // bool flag = false; memset (visited, false, sizeof (visited); While (! Path. empty () path. pop (); For (INT I = 0; I <n; I ++) CIN> map [I]; for (INT I = 0; I <N; I ++) {for (Int J = 0; j <m; j ++) {If (Map [I] [J] = '@'&&! Visited [I] [J]) {visited [I] [J] = true; Path. push (node (I, j); CNT ++; BFS (path. front () ;}} printf ("% d \ n", CNT) ;}return 0 ;}
Code 2 (DFS ):
# Include <iostream> # include <cstdio> # include <cstring> # include <string> # define n 110 using namespace STD; struct node {int X, Y; node (INT X0 = 0, int Y0 = 0) {// you need to write the constructor. Otherwise, it will not be compiled when it is used. X = x0; y = y0 ;}}; int directionx [8] = {, 0,-1,-1,-1, 0, 1}, n, m, CNT, X, Y; int directiony [8] = {0, 1, 1, 0,-1,-1,-1}; bool visited [N] [N]; string map [N]; void DFS (node s) {for (INT I = 0; I <8; I ++) {If (0 <= S. X + directionx [I] & S. X + directionx [I] <n & 0 <= S. Y + directiony [I] & S. Y + directiony [I] <m) {If (Map [S. X + directionx [I] [S. Y + directiony [I] = '@'&&! Visited [S. X + directionx [I] [S. Y + directiony [I]) {visited [S. X + directionx [I] [S. Y + directiony [I] = true; DFS (node (S. X + directionx [I], S. Y + directiony [I]) ;}}} int main () {While (scanf ("% d", & N, & M )! = EOF & (N | M) {CNT = 0; memset (visited, false, sizeof (visited); For (INT I = 0; I <N; I ++) CIN> map [I]; for (INT I = 0; I <n; I ++) {for (Int J = 0; j <m; j ++) {If (Map [I] [J] = '@'&&! Visited [I] [J]) {visited [I] [J] = true; CNT ++; DFS (node (I, j ));}}} printf ("% d \ n", CNT);} return 0 ;}