Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

A rectangular map consisting of N rows and M columns of square areas is given. Each area is painted with a certain color. Two areas

A rectangular map consisting of N rows and M columns of square areas is given. Each area is painted with a certain color.

Two areas on the map belong to the same country if the following conditions are met:

they have the same color; it is possible to travel from one area to the other by moving only north, south, west or east without moving over areas of a different color.

The map can be described by a zero-indexed matrix consisting of N rows and M columns of integers. The color of each area is described by the corresponding element of the matrix. Two areas have the same color if and only if their corresponding matrix elements have the same value.

For example, consider the following matrix A consisting of seven rows and three columns:

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

Matrix A describes a map that is colored with five colors. Areas on the map belong to eleven different countries:

area A[0][0] forms a one-area country; areas A[0][1], A[0][2], A[1][2], A[2][2] belong to the same country; area A[1][0] forms a one-area country; area A[1][1] forms a one-area country; area A[2][0] forms a one-area country; areas A[2][1], A[3][0], A[3][1], A[3][2] belong to the same country; areas A[4][0], A[4][1] belong to the same country; areas A[4][2], A[5][1], A[5][2] belong to the same country; area A[5][0] forms a one-area country; area A[6][0] forms a one-area country; areas A[6][1], A[6][2] belong to the same country.

Write a function

def countries_count(const vector

that, given a zero-indexed matrix A consisting of N rows and M columns of integers, returns the number of different countries that the areas of the map described by matrix A belong to.

Assume that:

N is an integer within the range [1..1,000,000]; M is an integer within the range [1..1,000,000]; the number of elements in matrix A is within the range [1..1,000,000]; each element of matrix A is an integer within the range [-1,000,000,000..1,000,000,000].

For example, given matrix A consisting of seven rows and three columns corresponding to the example above, the function should return 11.

Performance: Focus on time complexity and run-time performance so if the input size grows, execution time does not grow exponentially.

Correctness: The solution should cover edge cases such as minimum or 0 input value, small input, large input, breakpoint. Your solution should consider all possible corner cases and handle large input efficiently.


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

Step: 3

blur-text-image

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 Wireless and Mobile Systems

Authors: Dharma P. Agrawal, Qing An Zeng

4th edition

1305087135, 978-1305087132, 9781305259621, 1305259629, 9781305537910 , 978-130508713

More Books

Students also viewed these Programming questions

Question

Contact person at the organization

Answered: 1 week ago

Question

Explain the factors that determine the degree of decentralisation

Answered: 1 week ago

Question

What Is acidity?

Answered: 1 week ago

Question

Explain the principles of delegation

Answered: 1 week ago

Question

State the importance of motivation

Answered: 1 week ago

Question

Discuss the various steps involved in the process of planning

Answered: 1 week ago