Aoj 0118 Property Distribution (DFS)

Source: Internet
Author: User
Tags pear

http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0118

Meaning

In the rectangular orchard of H * W, there are three kinds of fruit trees of apple, pear and Mandarin, adjacent (up and down) of the same fruit trees belong to the same area, give out the distribution of fruit trees in the orchard, ask how many regions in total. (The original picture of the apple as リ, pear for カ, mandarin Orange for ミ, a total of 10 regions)

Input:

Multiple sets of data, the first behavior of each group of data two integers h,w (h <=, W <=), h = 0 and W = 0 represents the end of input. The following H Line W column indicates orchard fruit tree distribution, Apple is @, pear is #, Mandarin is *.

This article URL address: http://www.bianceng.cn/Programming/sjjg/201410/45524.htm

Output:

For each set of data, the number of its regions is output.

Complete code:

/*0ms,1360kb*/
    
#include <cstdio>  
#include <cstring>  
    
char grid[105][105], flag;  
    
BOOL Dfs (int i, int j)  
{  
    if (!grid[i][j] | | grid[i][j]!= flag) return false;  
    GRID[I][J] = 0;  
    DFS (I-1, j); DFS (i + 1, j); DFS (i, j-1); DFS (I, j + 1);  
    return true;  
}  
    
int main ()  
{  
    int x, Y, I, J, CNT;  
    while (scanf ("%d%d\n", &y, &x), x)  
    {  
        memset (grid, 0, sizeof (GRID));  
        CNT = 0;  
        for (i = 1; I <= y; ++i)  
            gets (Grid[i] + 1);  
        for (i = 1; I <= y; ++i) for  
            (j = 1; j <= x; ++j)  
            {  
                flag = grid[i][j];  
                if (Dfs (i, j)) ++cnt  
            }  
        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.