Total passenger garlic - the best grass

Topic Address:

https://nanti.jisuanke.com/t/T1141

Cows Bessie plan and enjoy soft new spring grass. The new distribution of grass in the pasture's R rows and C columns. It wants to calculate the number of pasture grass.
In the pasture map, each grass or a single '#', or is there a common edge of two adjacent '#'. Given ranch maps, calculate how many bushes.
For example, consider the following five rows of six maps ranch

 

The ranch has 5 Ge grass: one in the first row, a span of two, three lines, one in the third row, a span of four or five in the fourth row in the second column, the last one in the fifth row .

Input Format

The first line contains two integers R and C, separated by a single space.
Next R lines of C characters, describing ranch map. Only the characters '#' or '' two kinds.

Output Format

Output an integer representing the number of grass.

 

 

 

Code:

#include<stdio.h>
int a[100][100];
int main(){
    int x,y;
    scanf("%d %d",&x,&y);getchar();
    for(int i = 0; i < x;i++){
        for(int j = 0;j < y;j++){
            char p = getchar();
            if(p == '.')
                a[i][j] = 0;
            else
                a[i][j] = 1;
        }
        getchar();
    }
    int sum = 0;
    int jq = 0;
    for(int i = 0; i < x;i++){
        for(int j = 0;j < y;j++){
            if(a[i][j] == 1){
                sum++;
                if(a[i][j+1] == 1 || a[i+1][j] == 1 ){
                    jq++;
                }
            }
        }
    }
    printf("%d",sum - jq);
    return 0;
}

 

Item difficulty is not large, is the idea of ​​feeling a little mean, the first character will be converted into an array of 01 matrices, convenient operation, directly after a hard count recursion would be more trouble, so directly:

Total # - # right and below each of the adjacent grass # number = Number

 

Guess you like

Origin www.cnblogs.com/expedition/p/11777580.html