Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

You are given a matrix consisting of N rows and M columns. Each cell of the matrix contains one digit (0-9). Cells are adjacent if








You are given a matrix consisting of N rows and M columns. Each cell of the matrix contains one digit (0-9). Cells are adjacent if they share a commdn edge. It is possible to move from one cell to another directly only if they are adjacent. Find the largest group of cells such that: you can get from any cell in the group to any other by moving only through cells that belong to the group; the difference between the largest and the smallest values of the cells in the group is at most 1 Write a function: int solution (vector< vectorints > &A) : that, given a matrix A of N rows and M columns containing integers from the range 0-9, returns the maximal size of the group, fulfilling the above criteria.

Examples:

1. Given the following matrix: A[0][0] = 3

A[0][1]= 4A[0] [2]= 6 A[1][0] = 2A[1][1] = 7 A[1][2] = 6

your function should return 3. The biggest area, which is made of cells with values 6 and 7, is marked in the illustration above. 2. Given the following matrix: A[0][0] = 3 A [0][1] = 3 A[0][2]= 5 A[0][3]=6

A[1][0] = 6 2 A[1][1] = 7 A[1][2]=2 A[1][3]= 2

A[2][0] = 5 A[2][1] = 2 A[2][2]=3A[2][3]=8

A[3][0] = 5 A[3][1]= 9 A[3][2] = 2 A[3][3]= 3

A[4][0]= 1 A[4][1] = 2 A[4][2]= 3 A[4][3] =4

your function should return 8. The biggest area, which is made of cells with values 2 and 3, is marked in the illustration above. 3. Given the following matrix:

A[0][0] = 4 A[0][1]=4 A[0][2] = 2 A[0][3] = 4 A[0][4] = 4 A[0][5]=4

your function should return 3. Notice that we cannot include the two cells with value 4 on the left, as we would also have to include the cell containing value 2, thereby increasing the difference between the smallest and largest values to greater than 1 4. Given the following matrix:

A[0] [0]=0 A[1][0] =3 A[2][0]=5

your function should return 1. The values of all pairs of adiacent cells in the matrix differ by at least 2. come up with efficient algorithm for the following assumptions: A is a matrix with N rows and M columns; . N and M are integers within the range [1. 300]; each element of matrix A is an integer within the range [0..9].


Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introduction to Algorithms

Authors: Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest

3rd edition

978-0262033848

More Books

Students also viewed these Algorithms questions

Question

When are objects on the periphery of your vision most noticeable?

Answered: 1 week ago