1011 Oil Deposits

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. <br>
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 are either ' * ', representing the absence of oil, or ' @ ', representing a oil PO Cket.<br>
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.<br>
Sample Input
1 1<br>*<br>3 5<br>*@*@*<br>**@**<br>*@*@*<br>1 8<br>@@****@*<br> 5 5 <br>****@<br>*@@*@<br>*@**@<br>@@@*@<br>@@**@<br>0 0 <br>
Sample Output
0<br>1<br>2<br>2<br>
Source mid-central USA 1997 Simple test Instructions: a m*n map, where the lattice is either *, or @, for @, diagonally to become a block, now requires writing a program to find out how many @ blocks in total. The process of solving ideas:
This is the most classic deep search topic. The teacher also spoke in class, so the idea naturally has. The DFS function is written out and it's all over. Impression: The example in class is also the most classic part. For the classics, this is a non-reproducible template. AC Code:
#include <iostream>
#include <cstring>
using namespace Std;
BOOL visit[110][110];
Char maze[110][110];
int dir[8][2]={{-1,0},{1,0},{0,1},{0,-1},{-1,-1},{-1,1},{1,-1},{1,1}};
int sum,m,n,sx,sy;
BOOL Isbound (int a,int b) {
if (a<1 | | a>m | | b<1 | | b>n) return true;
return false;
}
void dfs (int sx,int sy) {


for (int i=0;i<8;i++)
{
if (maze[sx+dir[i][0]][sy+dir[i][1]]== ' * ') continue;
if (Isbound (sx+dir[i][0],sy+dir[i][1))) continue;
if (Visit[sx+dir[i][0]][sy+dir[i][1]]) continue;
Visit[sx+dir[i][0]][sy+dir[i][1]]=1;
DFS (sx+dir[i][0],sy+dir[i][1]);
}
}
int main ()
{
while (Cin>>m>>n)
{
if (m==0) break;
memset (visit,0,sizeof (visit));
for (int i=1;i<=m;i++) {
for (int j=1;j<=n;j++) {
cin>>maze[i][j];
}
}
sum=0;
for (int i=1;i<=m;i++) {
for (int j=1;j<=n;j++) {
if (maze[i][j]== ' @ ' &&!visit[i][j]) {visit[i][j]=1; Dfs (i,j); sum++;}
}
}
cout<<sum<<endl;

}

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.