Question: Block Extraction - 2 Problem Description Chintu is playing with Lego blocks arranged in a matrix. Each block is a different colour and can only
Block Extraction
Problem Description
Chintu is playing with Lego blocks arranged in a matrix. Each block is a different colour and can only be placed straight, either horizontally or vertically, in a matrix arrangement.
The given matrix contains integers, where each colour of the blocks is represented by a number which is called as colour code of that block. If two or more adjacent cells have the same number, it means one block is occupying all those cells. There will be only one block of each colour.
Given the front view of the matrix, Chintu can only remove blocks from the right side, taking out one whole block at a time. If there is a barrier in front of a block, he must remove the barrier first to access the block he wants.
A block will remain in place even if it has support from just one adjacent cell. Otherwise, it will fall due to gravity.
Can you help Chintu calculate the number of blocks he needs to remove to extract the desired block?
Constraints
N M
colour code
Input
First line consists of two space separated integers N M denoting the number of rows and columns in the matrix.
The next N lines contain M spaceseparated integers that represent the colour codes of the blocks and the cells each block occupies.
Last line consists of an integer denoting the colour code of the block which Chintu wanted to extract.
Output
Print a single number indicating how many blocks Chintu needs to remove to access the specified block.
Time Limit secs
Examples
Example
Input
Output
Explanation
The size of the matrix is rows and columns. It comprises of blocks numbered and Chintu wishes to extract block
To extract the blocks with colour code Chintu can do the following.
First, he will remove the block with colour code followed by the block with colour code and respectively. After that only blocks with colour code can be removed. So blocks will need to be removed in row
In the process, block with colour code in row will fall due to gravity, so those must be removed too. So block will be removed in row
This chain reaction will cause blocks and to fall as well, requiring their removal too. So blocks will be removed in rows and
Thus, in total, Chintu will have to remove blocks before he can extract the block with colour code
Example
Input
Output
Explanation
The size of the matrix is rows and columns. It comprises of blocks numbered and Chintu wishes to extract block
To extract the blocks with colour code Chintu can do the following.
First, he will remove the block with colour code in rows and So this is block.
The block with colour code in row will fall due to gravity, so he must also remove that block.
Next, he will remove the block with colour code followed by and in rows and So this is blocks.
Thus, in total, Chintu will have to remove blocks before he can extract the block with colour code
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
