Answer to Question #341384 in Python for sandhya

Question #341384

You are on the top floor of a multiplex complex and want to go the ground floor,The lift is not working so you have to search for the staircase on each floor and go down to the exit.

As you are new to that multiplex,you have taken the floor map.you are given a map. M of building in a 2D matrix.

. All walkable space are represented by a 0

. Staircases are represented by a 1

.your starting position is represented by 2 and can be at any level of the car park

.Exit is always at the bottom right of the ground floor

. you must use the staircases 1 s to go down a level

.Each floor will have only one staircase apart from the ground floor which will not have any staircases.find the quickest route of the multiplex complex

input:the first line of input contains two space separated integer N representing the number of rows and C representing the number of columns

the next N lines contain C space-separated integers

I/P: 3 3

1 0 2

0 1 0

0 0 0

O/P:L2 D1 R1 D1 R1



1
Expert's answer
2022-05-16T16:08:05-0400
while True:
    n = input('Please, input n => ').strip()
    if n.isdigit():
        n = int(n)
        break
    else:
        print('Please, input any digit')
while True:
    m = input('Please, input m => ').strip()
    if m.isdigit():
        m = int(m)
        break
    else:
        print('Please, input any digit')


one_cord = []
a = ['0', '2']
lst = [[0 for j in range(m)]for i in range(n)]
for i in range(n):
    if i != n - 1:
        a.append('1')
    for j in range(m):
        while True:
            tmp = input(f'a[{i}][{j}] = ').strip()
            if tmp.isdigit() and tmp in a:
                tmp = int(tmp)
                lst[i][j] = tmp
                if tmp == 2:
                    two_cord = (i, j)
                    a.remove('2')
                elif tmp == 1:
                    one_cord.append((i, j))
                    a.remove('1')
                break
            else:
                print('Please, input digit (0, (1 - once on the each n except the last one), (2 - once))')


flag = False
for cor in one_cord:
    if cor[0] == two_cord[0]:
        flag = True
    if flag:
        direction = cor[1] - two_cord[1]
        if -9999 < direction < 0:
            print('L' + str(abs(direction)), end = ' ')
            print('D1', end = ' ')
        else:
            print('R' + str(direction), end = ' ')
            print('D1', end = ' ')
        two_cord = cor
        two_cord[0] + 1


direction = two_cord[1] - (m - 1)
print('R' + str(abs(direction)), end = ' ')

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS