How I can find a number of all connecting cells which filled with 1 in matrix NxN? (Python). | SoloLearn: Learn to code for FREE!

+2

# How I can find a number of all connecting cells which filled with 1 in matrix NxN? (Python).

Problem connecting cells. For example matrix 4x4 a = [[1,0,0,1] [0,0,1,1] [0,0,0,0] [1,1,0,0]] In this matrix a number of connecting cells equal 3. 1st: a[0][0] 2nd:a[0][3], a[1][2], a[1][3] 3rd: a[2][0], a[2][1]

8/6/2020 3:05:15 PM

Rustam Bazarbayev

+3

Rustam, the first part adds extra row of 0s and flattens the matrix to one list. the second part searches for all spaces around each node except the space directly above and on the top left. but, why that works i honestly dont know! im sorry! i just kept messing with it until it worked 🤔

+2

Rustam, no need for sorry it's ok 👌 Can you perhaps show another example, maybe a 3x3 matrix or something. I ask because I'm still unsure what output is expected and how to format the output. Actually, I still don't quite understand the term "connecting cells" : )

+2

+2

Oh okay 👌 Looks pretty tough Rustam, maybe I'll get back if I come up with something. Right now, no idea ...

+2

Thanks

+2

there is quite a bit of theory on this problem. it is called “find thd number of islands” problem and there are ideas about depth first search and breadth first search. just fyi! thanks!!!

+1

What you mean connecting cells?!

+1

Rustam find in inet percolation algorithm.

+1

did you want all connecting 1s or only the longest group of them?

+1

i guess i need a clearer question lol

+1

How I can find a number of all connecting cells which filled with 1 in matrix NxN? (Python). I mean , Problem connecting cells. For example matrix 4x4 a = [[1,0,0,1] [0,0,1,1] [0,0,0,0] [1,1,0,0]] In this matrix a number of connecting cells equal 3. 1st: a[0][0] 2nd:a[0][3], a[1][2], a[1][3] 3rd: a[2][0], a[2][1]

+1

oh i gotcha! you want the number of groups of 1s. i’ll work on that. thank you for the challenge. but... i can already see where it will be tough!!!

+1

Rustam, Are you sure about the 3rd a[2][0], a[2][1] a[2] contains zero only ... I think maybe you mean a[3][0], a[3][1] because there are two values of 1 there.

+1

Ipang ok. This is 3x3 matrix [0][1][0] [0][0][1] [1][1][0] In this matrix a number of connected cells are 1. Because all cells which filled with 1 are neighbors each other.

+1

George thank you

+1

Rustam Correct me if I'm wrong understanding ... In 3x3 matrix above, the 3rd row [1, 1, 0] is the only one connected cell because it contains two adjacent value of 1. Did I get it right this time?

+1

Ipang. No you're wrong. Because [2][1] cell is neighbour with [1][2] and [1][2] cell is neighbour with [0][1] cell. All of them filled with 1 and neighbors each other. All of them connected with each other.

+1

he is also counting diagonals it looks like

+1

i am working on it now haha and its rly hard to say the least...

+1

Madeline thank you. It's working. Can you explain me this code?