Counting the number of elements of blocks with same consecutive elements

0

I'm having the following problem:

A park that have the form of a m x n board. There are k kinds of trees (1 <= k <= 100). The park is divided into m x n cells and each cell, they'll plant a tree. Now, on the map, each cell of the park have an integer i inside if the i-th kind of tree is planted in it, or a 0 if no tree is planted in it. A line of cells is considered "good" if it has at least t trees of same types, (and they must be on the same either line or column). Count the number of trees that is not in a "good" line.

Input: Integers m, n, t and an m x n array of integers represent the map.

Output: Number of trees that is not in a "good" line.

Example:

Input: 5 6 3

1 3 3 3 3 4

1 2 3 2 0 4

3 2 2 2 4 4

1 0 0 2 4 0

1 2 3 0 4 4

Output: 10

Explanation: The bold numbers are the trees that is not in a good line.

1 3 3 3 3 4

1 2 3 2 0 4

3 2 2 2 4 4

1 0 0 2 4 0

1 2 3 0 4 4

My idea is to check for each element in the array. If it is satisfied then I'll move to the nearest element outside the "good" line. Else, it will just move to the next element on the same line, or if the line is ended then the next element on the column.

Here is my code

#include <stdio.h>

#define maxn 120

int a[maxn][maxn], m, n, t;

int check(int *i, int *j){
    int k, cnt_r, cnt_c;
    cnt_r = 0;
    //jump to the nearest cell that is not in good line
    for(k = *i + 1; k < m; k++){
        if(a[*i][*j] == a[k][*j]) cnt_r++;
        if(cnt_r >= t){
            *i = k;
            return 1;
        }
    }
    cnt_c = 0;
    for(k = *j + 1; k < n; k++){
        if(a[*i][*j] == a[*i][k]) cnt_c++;
        if(cnt_c >= t){
            *j = k;
            return 1;
        }
    }
    return 0;
}
//check if this is the last square or not
int lastSq(int r, int c){
    return (r == n - 1 && c == n);
}

int main(){
    int res = 0, i, j, pos_r = 0, pos_c = 0;
    scanf("%d%d%d", &m, &n, &t);
    for(i = 0; i < m; i++)
        for(j = 0; j < n; j++)
            scanf("%d", &a[i][j]);

    while(!lastSq(pos_r, pos_c)){
        if(a[pos_r][pos_c] == 0){
            if(pos_c < n - 1) pos_c++;
            else if(pos_r < n - 1){
                pos_c = 0;
                pos_r++;
            }
        }
        if(!check(&pos_r, &pos_c)){
            res++;
            if(pos_c < n - 1) pos_c++;
            else{
                pos_c = 0;
                pos_r++;
            }
        }
    }

    printf("%d", res);
}

But it doesn't print any output. The only thing I have is 0xC0000005. Can someone please check where did I make a mistake and provide me a direction? Thanks.

c
arrays
asked on Stack Overflow Apr 2, 2019 by yliu123123

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0