Hdoj/hdu 1241 Oil Deposits (classic Dfs)

Source: Internet
Author: User

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.

Sample Input

1 1*3 5*@*@***@***@*@*1 8@@****@*****@*@@*@*@*

Sample Output
0
1
2
2

Test instructions
* represents Wasteland, @ represents oilfield. The top and bottom of the @, there are 4 diagonal if there is a @, it means that they are an oilfield, asked-given the figure, how many oilfields.

Analysis:
The topic with DFS (deep search) can be a good solution to the problem!

#include <iostream>#include <stdio.h>#include <string.h>using namespace STD;Char Map[ About][ About];intdf[ About][ About];intCon,n,m;voidDfsintXintYintT) {if(df[x][y]==1|| x<0|| y<0|| x>=n| | Y&GT;=M) {return; }if(Map[x] [y]==' * '){return; }if(t==1&&Map[x] [y]==' @ ') {con++;//printf ("con=%d\n", con);}//printf ("%d%d\n", x, y);    if(Map[x] [y]==' @ '){Map[x] [y]=' * '; df[x][y]=1; DFS (x+1, y+1,0); DFS (X-1, Y1,0); DFS (x+1Y0); DFS (x,y+1,0); DFS (X-1Y0); DFS (x,y-1,0); DFS (x+1, Y1,0); DFS (X-1, y+1,0); df[x][y]=0; }}intMain () { while(~scanf("%d%d", &n,&m), (n| | m)) {memset(DF,0,sizeof(DF)); con=0; GetChar (); for(intI=0; i<n;i++) { for(intj=0; j<m;j++) {scanf("%c",&MapI            [j]);        } getchar (); } for(intI=0; i<n;i++) { for(intj=0; j<m;j++) {DFS (i,j,1); }        }printf("%d\n", con); }return 0;}

Hdoj/hdu 1241 Oil Deposits (classic Dfs)

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.