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-2
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
1<= N, M <=50
1<= colour code <=100
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 space-separated 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)
1
Examples
Example 1
Input
55
1512231313
151214810
5111189
56677
11234
1
Output
8
Explanation
The size of the matrix is 5 rows and 5 columns. It comprises of blocks numbered {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 and 23}. Chintu wishes to extract block 1.
To extract the blocks with colour code 1, Chintu can do the following.
First, he will remove the block with colour code 4, followed by the block with colour code 3 and 2 respectively. After that only blocks with colour code 1 can be removed. So,3 blocks will need to be removed in row 5.
In the process, block with colour code 7 in row 4 will fall due to gravity, so those must be removed too. So,1 block will be removed in row 4.
This chain reaction will cause blocks 8,9,10, and 13 to fall as well, requiring their removal too. So,4 blocks will be removed in rows 1,2 and 3.
Thus, in total, Chintu will have to remove 8 blocks (3+1+4) before he can extract the block with colour code 1.
Example 2
Input
64
1234
5566
7889
10121315
10121315
11121415
12
Output
4
Explanation
The size of the matrix is 6 rows and 4 columns. It comprises of blocks numbered {1,2,3,4,5,6,7,8,9,10,11,12,13,14 and 15}. Chintu wishes to extract block 12.
To extract the blocks with colour code 12, Chintu can do the following.
First, he will remove the block with colour code 15 in rows 4,5 and 6. So, this is 1 block.
The block with colour code 9 in row 3 will fall due to gravity, so he must also remove that block.
Next, he will remove the block with colour code 14 followed by 13 and 9 in rows 3,4,5 and 6. So, this is 3 blocks.
Thus, in total, Chintu will have to remove 4 blocks (1+3) before he can extract the block with colour code 12.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!