Cerinta completa

Consider a matrix where each cell contains either a or a . Any cell containing a is called a filled cell. Two cells are said to be connected if they are adjacent to each other horizontally, vertically, or diagonally. In the following grid, all cells marked X are connected to the cell marked Y.

XXX
XYX  
XXX    

If one or more filled cells are also connected, they form a region. Note that each cell in a region is connected to zero or more cells in the region but is not necessarily directly connected to all the other cells in the region.

Given an matrix, find and print the number of cells in the largest region in the matrix. Note that there may be more than one region in the matrix.

For example, there are two regions in the following matrix. The larger region at the top left contains cells. The smaller one at the bottom right contains .

110
100
001

Function Description

Complete the connectedCell function in the editor below.

connectedCell has the following parameter(s):
int matrix[n][m]: represents the row of the matrix

Returns
int: the area of the largest region

Input Format

The first line contains an integer , the number of rows in the matrix.
The second line contains an integer , the number of columns in the matrix.
Each of the next lines contains space-separated integers .

Constraints

Sample Input

STDIN       Function
-----       --------
4           n = 4
4           m = 4
1 1 0 0     grid = [[1, 1, 1, 0], [0, 1, 1, 0], [0, 0, 1, 0], [1, 0, 0, 0]]
0 1 1 0
0 0 1 0
1 0 0 0

Sample Output

5

Explanation

The diagram below depicts two regions of the matrix. Connected regions are filled with X or Y. Zeros are replaced with dots for clarity.

X X . .
. X X .
. . X .
Y . . .

The larger region has cells, marked X.


Limbajul de programare folosit: cpp14

Cod:

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

// Also known as the "Matrix Island Problem", we can use a DFS to visit every "island" of 1's in 
// the matrix. In C++, the syntax for passing a 2d array (pointer to a pointer) to a function can be a 
// little tricky, but basically just remember that since we want to pass our 2d array by REFERENCE and not 
// by VALUE, we need to add an extra dereference * to our recursive functions (i.e visit()) parameters.
// SO link for why we need an extra dereference: 
// - http://stackoverflow.com/questions/12149613/why-does-passing-pointers-from-structs-require-an-extra-dereference

int visit(int ***A, int i, int j, int n, int m, int size) {
    (*A)[i][j] = -1;
    size++;
    if(i-1 >= 0 && j-1 >= 0 && (*A)[i-1][j-1] == 1) {
    	size += visit(A, i-1, j-1, n, m, 0);
    }
    if(i-1 >= 0 && (*A)[i-1][j] == 1) {
        size += visit(A, i-1, j, n, m, 0);
    }
    if(i-1 >= 0 && j+1 < m && (*A)[i-1][j+1] == 1) {
        size += visit(A, i-1, j+1, n, m, 0);
    }
    if(j-1 >= 0 && (*A)[i][j-1] == 1) {
        size += visit(A, i, j-1, n, m, 0);
    }
    if(j+1 < m && (*A)[i][j+1] == 1) {
        size += visit(A, i, j+1, n, m, 0);
    }
    if(i+1 < n && j-1 >= 0 && (*A)[i+1][j-1] == 1) {
        size += visit(A, i+1, j-1, n, m, 0);
    }
    if(i+1 < n && (*A)[i+1][j] == 1) {
        size += visit(A, i+1, j, n, m, 0);
    }
    if(i+1 < n && j+1 < m && (*A)[i+1][j+1] == 1) {
        size += visit(A, i+1, j+1, n, m, 0);
    }
    return size;
}

int main() {
	int n, m;
	cin>>n>>m;
	int **matrix = new int*[n];
	for(int i = 0; i < n; i++) {
		matrix[i] = new int[m];
		for(int j = 0; j < m; j++) {
			cin>>matrix[i][j];
		}
	}
	int maxSize = 0;
	for(int i = 0; i < n; i++) {
		for(int j = 0; j < m; j++) {
			if(matrix[i][j] == 1) {
				int size = visit(&matrix, i, j, n, m, 0);
				if(size > maxSize) {
					maxSize = size;
				}
			}
		}
	}
	cout<<maxSize;
    return 0;
}

Scor obtinut: 1.0

Submission ID: 464602884

Link challenge: https://www.hackerrank.com/challenges/connected-cell-in-a-grid/problem

Connected Cells in a Grid