Ultraviolet A 572 oil deposits (DFS calculates 8 connected blocks)

Source: Internet
Author: User

In the N * M matrix, the number of connected blocks is '@', which belongs to a connected block in a jiugong lattice.

When the most basic DFS encounters @, it recursively scans the eight surrounding nodes and marks that the current grid has been accessed, and then gets the answer.

#include<cstdio>using namespace std;const int N = 110;char mat[N][N];int dfs(int r, int c){    if(mat[r][c] != '@') return 0;    else    {        mat[r][c] = '*';        for(int i = -1; i <= 1; ++i)            for(int j = -1; j <= 1; ++j)                if(mat[r + i][c + j] == '@')                    dfs(r + i, c + j);    }    return 1;}int main(){    int n, m;    while(scanf("%d%d", &n, &m) , m)    {        for(int i = 1; i <= n; ++i)            scanf("%s", mat[i] + 1);        int ans = 0;        for(int i = 1; i <= n; ++i)            for(int j = 1; j <= m; ++j)                ans += dfs(i, j);        printf("%d\n", ans);    }    return 0;}

Oil Deposits

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 and. 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



Ultraviolet A 572 oil deposits (DFS calculates 8 connected blocks)

Related Article

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.