You are given a rectangular binary matrix (contains only zerosand ones) of R rows and C columns. Print the number of differentregions in the given matrix. A separate region is a collection ofones interconnected horizontally or vertically. A region can haveholes in it. Please find the below screenshot for the completeproblem.
Please share the Answer in Java and Pythonseparately
No of Regions You are given a rectangular binary matrix (contains only zeros and ones) of R rows and C columns. Print the number of different regions in the given matrix. A separate region is a collection of ones. interconnected horizontally or vertically. A region can have holes in it. 1 1 1 1 0 0 Input 2 1 Output 0 0 0 6 0 0 0 0 Explanation 1 and the matrix 0 Sample Input 1 1 Given R = 6 C = 10 0 0 The first line of output is an integer. 1 1 1 1 0 1 1 0 1 1 1 0 0 0 0 0 0 1 1 1 1 0 1 1 0 0 0 1 0 1 1 0 1 1 0 1 0 1 1 1 0 0 0 0 0 1 1 0 0 1 0 0 0 0 1 1 1 0 1 0 Sample Output 1 0112 Sample Input 2 55 1 1 1 10 0 1 1 0 1 0 1 1 1 0 1 0 0 1 1 1 1 1 1 1 6 10 1 1 1 1 0 1 1 0 1 1 1 0 0 0 0 0 0 1 1 1 1 0 1 1 0 0 0 1 0 1 10 11 00 0 0 0000 1 0 1 1 1 1 0 0 1 1 1 0 1 0 Sample Output 2 0 0 0 0 0 1 0 1 0 0 1 1 In the above image, different regions in the matrix are represented by different colors. The first line of input contains two space-separated integers, R and C The next R lines contain C space-separated integers. 1 1 0 0 0 1 Different regions of this matrix are highlighted with different colors in the above image. 1 As a region is a collection of ones interconnected horizontally or vertically, but not diagonally, the regions green, yellow, purple, red are not considered as single region as highlighted in the above image. 0 Also, notice that the yellow region has a hole in it. As it can be observed from the image, there are a total of 6 regions (blue, green, yellow, purple, red, pink). So the output is 6 0 0 0 0 1 0
You are given a rectangular binary matrix (contains only zeros and ones) of R rows and C columns. Print the number of di
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am