soroushamdg
soroushamdg

Reputation: 211

How do I trace a path in matrix?

I have matrix like below:

[[0,0,0,0,0,0,0,0,0,0,0,0,0,0],
 [0,0,0,0,0,0,1,0,0,1,1,1,1,0],
 [0,0,0,0,0,0,1,0,0,1,0,0,1,0],
 [0,0,0,0,0,0,1,0,0,1,0,0,1,0],
 [0,0,0,0,0,0,1,0,0,1,0,0,1,0],
 [0,0,0,0,0,0,1,0,0,1,1,1,1,0],
 [0,0,0,0,0,0,1,0,0,1,0,0,0,0],
 [0,0,0,0,0,0,1,0,0,1,0,0,0,0],
 [0,0,0,0,0,0,1,1,1,1,1,1,1,0],
 [0,0,0,0,0,0,0,0,0,0,0,0,0,0]]

How can I find a path to trace 1's in the matrix? As you can see the matrix's 1s represent something like the word "U2", I want to return a path something like pixels it should follow to draw it. It can move back a path if it has already moved forward, but it should not jump a 0, it should always move on the 1s.

Upvotes: 0

Views: 446

Answers (2)

TheOriginalOdis
TheOriginalOdis

Reputation: 102

a = [[0,0,0,0,0,0,0,0,0,0,0,0,0,0],
     [0,0,0,0,0,0,1,0,0,1,1,1,1,0],
     [0,0,0,0,0,0,1,0,0,1,0,0,1,0],
     [0,0,0,0,0,0,1,0,0,1,0,0,1,0],
     [0,0,0,0,0,0,1,0,0,1,0,0,1,0],
     [0,0,0,0,0,0,1,0,0,1,1,1,1,0],
     [0,0,0,0,0,0,1,0,0,1,0,0,0,0],
     [0,0,0,0,0,0,1,0,0,1,0,0,0,0],
     [0,0,0,0,0,0,1,1,1,1,1,1,1,0],
     [0,0,0,0,0,0,0,0,0,0,0,0,0,0]]


class U2:

    def __init__(self, matrix):
        self.matrix = matrix
        self.solved = []

    def find_path(self):
        for line in self.matrix:
            comprehension = ["___" if item==1 else item=="x" for item in line]             
            self.solved.append(comprehension)
        return self.solved



a = U2(a)
solved = a.find_path()
for i in solved:
    print(i)

Something like this? This is the output:

[False, False, False, False, False, False, False, False, False, False, False, False, False, False]
[False, False, False, False, False, False, '___', False, False, '___', '___', '___', '___', False]
[False, False, False, False, False, False, '___', False, False, '___', False, False, '___', False]
[False, False, False, False, False, False, '___', False, False, '___', False, False, '___', False]
[False, False, False, False, False, False, '___', False, False, '___', False, False, '___', False]
[False, False, False, False, False, False, '___', False, False, '___', '___', '___', '___', False]
[False, False, False, False, False, False, '___', False, False, '___', False, False, False, False]
[False, False, False, False, False, False, '___', False, False, '___', False, False, False, False]
[False, False, False, False, False, False, '___', '___', '___', '___', '___', '___', '___', False]
[False, False, False, False, False, False, False, False, False, False, False, False, False, False]

Upvotes: 1

I break things
I break things

Reputation: 326

If you just want a list of lists with the positions of the number 1's that you can iterate through, you can do something like this:

>>>block = [[0,0,0,0,0,0,0,0,0,0,0,0,0,0],
...         [0,0,0,0,0,0,1,0,0,1,1,1,1,0],
...         [0,0,0,0,0,0,1,0,0,1,0,0,1,0],
...         [0,0,0,0,0,0,1,0,0,1,0,0,1,0],
...         [0,0,0,0,0,0,1,0,0,1,0,0,1,0],
...         [0,0,0,0,0,0,1,0,0,1,1,1,1,0],
...         [0,0,0,0,0,0,1,0,0,1,0,0,0,0],
...         [0,0,0,0,0,0,1,0,0,1,0,0,0,0],
...         [0,0,0,0,0,0,1,1,1,1,1,1,1,0],
...         [0,0,0,0,0,0,0,0,0,0,0,0,0,0]]

>>> answer = []
>>> for i in block:
...    answer.append([j for j, e in enumerate(i) if e == 1])

>>>answer
[[], [6, 9, 10, 11, 12], [6, 9, 12], [6, 9, 12], [6, 9, 12], [6, 9, 10, 11, 12], [6, 9], [6, 9], [6, 7, 8, 9, 10, 11, 12], []]

Upvotes: 0

Related Questions