Concrete and Glass Slabs

Concrete and Glass SlabsThe program must accept a character matrix of size R*C containing “=” and “” as the input. The character “=” represents a concrete slab and the character “” represents a glass slab. If an iron ball falls on a glass slab, then it will break the glass and continue to fall. The iron ball stops falling when it falls on a concrete slab, but if the iron ball falls directly on a concrete slab without breaking any glass before, then the concrete slab will break and continue to fall till the next occurring concrete slab in that column. If a slab is broken, the program must replace the corresponding character with the character “x“. Finally, the program must print the modified matrix representing the state of R*C slabs as the output.

Boundary Condition(s):
2 <= R, C <= 50

Input Format:
The first line contains R and C separated by a space.
The next R lines, each contains C characters separated by a space.

Output Format:
The first R lines, each contains C characters separated by a space.

Example Input/Output 1:
Input:
5 5
= = – – =
= – – – –
= – – = –
– – = = =
= – = – –

Output:
x x x x x
= x x x x
= x x = x
– x = = =
= x = – –

Explanation:
In the 1st column, the iron ball falls directly on the concrete slab. So it breaks the first occurring concrete slab and it stops falling on the next occurring concrete slab.
In the 2nd column, the iron ball falls directly on the concrete slab. So it breaks the first occurring concrete slab and it keeps breaking the glass slabs.
In the 3rd column, the iron ball falls on the glass slabs. So it breaks the glass slabs till the first occurring concrete slab.
In the 4th column, the iron ball falls on the glass slabs. So it breaks the glass slabs till the first occurring concrete slab.
In the 5th column, the iron ball falls directly on the concrete slab. So it breaks the first occurring concrete slab and it keeps breaking the glass slabs. Finally, it stops falling on the next occurring concrete slab.

Example Input/Output 2:
Input:
7 8
= – = = = = – –
– = – – = = – –
= – – = = = – =
= = = – = – – –
= – = – – = – –
= – = = – = = –
= – – – = – = =

Output:
x x x x x x x x
x = x x = = x x
= – x = = = x =
= = = – = – x –
= – = – – = x –
= – = = – = = –
= – – – = – = =

r,c=map(int,input().split())
mat=[list(map(str,input().split())) for i in range(r)]
for j in range(c):
    for i in range(r):
        if i==0:
            mat[i][j]='x'
        elif mat[i][j]=='=':
            break
        else:
            mat[i][j]='x'
for i in range(r):
    for j in range(c):
        print(mat[i][j],end=" ")
    print()

Previous Article

Unfolded Cube Pattern

Next Article

Submatrix - Four Same Corners

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *