PKU 1562/HDU 1241 oil deposits (number of regions of Crude Oil --- BFs, DFS)

Source: Internet
Author: User



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 ;}




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.