Poj 1562 oil deposits

Source: Internet
Author: User
Oil Deposits

|Time limit:1000 ms |Memory limit:10000 k |
|Total submissions:21879 |Accepted:11397 |

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

Are adjacent horizontally, vertically, or diagonally. An oil deposit will not contain more than 100 pockets.

Sample Input

1 1

  • 3 5
    @@*
    @
    @@*
    1 8
    @@****@*
    5 5
    ****@
    @@@
    *@**@
    @@@*@
    @@**@
    0 0
Sample output

0
1
2
2

Source

Mid-Central USA 1997

Question

For the question of @ connection block, if @ is found in the upper left, lower left, upper right, lower left, and lower right directions, count the number of connection blocks. It can be solved with DFS. The Code is as follows:

#include <iostream>#include <cstdio>using namespace std ;const int MAXN = 1000 ;char arr[MAXN][MAXN] ;int n , m ;void dfs( int i , int j ){    if ( i >= n || j >= m || i < 0 || j < 0 ) return ;    if ( arr[i][j] != ‘@‘ ) return ;    arr[i][j] = ‘*‘ ;    for ( int a = -1 ; a <= 1 ; a ++ ){        for ( int b = -1 ; b <= 1 ; b ++ ){            if ( a != 0 || b != 0 ){                dfs( i + a , b + j ) ;            }        }    }}int main(){    while ( cin >> n >> m && n && m ){        for ( int i = 0 ; i < n ; i ++ ){            for ( int j = 0 ; j < m ; j ++ ){                cin >> arr[i][j] ;            }        }        int ans = 0 ;        for ( int i = 0 ; i < n ; i ++ ){            for ( int j = 0 ; j < m ; j ++ ){                if ( arr[i][j] == ‘@‘ ){                    dfs( i , j ) ;                    ans ++ ;                }            }        }        cout << ans << endl ;    }    return 0 ;}

Poj 1562 oil deposits

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.