You are viewing a single comment's thread. Return to all comments →
// create a layer of zeroes to given matrix to make recursion process easy int rec(int **arr,int i,int j,int count){
if (arr[i][j] !=0) { count++; arr[i][j] = 0; count = rec(arr,i-1,j,count); count = rec(arr,i+1,j,count); count = rec(arr,i,j+1,count); count = rec(arr,i,j-1,count); count = rec(arr,i-1,j-1,count); count = rec(arr,i-1,j+1,count); count = rec(arr,i+1,j-1,count); count = rec(arr,i+1,j+1,count); } return count;
} int connectedCell(int matrix_rows, int matrix_columns, int** matrix) {
int **arr =(int**) malloc((matrix_rows+2)*sizeof(int*)); for(int i= 0;i<matrix_rows+2;i++) arr[i] =(int*) calloc(matrix_columns+2, sizeof(int)); int max = INT_MIN; for (int i=1; i<matrix_rows+1; i++) { for (int j=1; j<matrix_columns+1; j++) { for (int k=0;k<matrix_rows;k++) { for (int l=0;l<matrix_columns;l++) arr[k+1][l+1] = matrix[k][l]; } int count =0; count = rec(arr,i,j,count); if (count>max){ max = count; } } } free(arr); return max;
}
Seems like cookies are disabled on this browser, please enable them to open this website
Connected Cells in a Grid
You are viewing a single comment's thread. Return to all comments →
code in c
// create a layer of zeroes to given matrix to make recursion process easy int rec(int **arr,int i,int j,int count){
} int connectedCell(int matrix_rows, int matrix_columns, int** matrix) {
}