Zig-Zag Robots

Zig-Zag Robots: There are R robots in R rows (i.e., one robot in each row). There are two types of robots which are given below.
Type 1: The robots start moving towards the East. Once it reaches the end of the row, it again starts moving towards the West. Similarly, the type 1 robots move in the direction East-West alternatively.
Type 2: The robots start moving towards the West. Once it reaches the end of the row, it again starts moving towards the East. Similarly, the type 2 robots move in the direction West-East alternatively.
The speed of each robot is 1 meter per second. The length of each row is C meters. The initial state of the robots is passed as the input. The program must print the state of the robots after T seconds as the output. The value of T is also passed as the input.

Boundary Condition(s):
2 <= R, C <= 50
1 <= T <= 10^4

Input Format:
The first line contains R and C separated by a space.
The next R lines, each contains C integers separated by a space.
The (R+2)nd line contains T.

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

Example Input/Output 1:
Input:
5 6
0 0 0 0 1 0
0 0 2 0 0 0
0 0 0 2 0 0
1 0 0 0 0 0
0 0 0 0 0 2
3

Output:
0 0 0 1 0 0
0 2 0 0 0 0
2 0 0 0 0 0
0 0 0 1 0 0
0 0 2 0 0 0

Explanation:
Here R = 5, C = 6 and T = 3.
0 indicates the empty space.
1 indicates the position of the type 1 robot.
2 indicates the position of the type 2 robot.
After 1 second, the state of the robots is given below.
0 0 0 0 0 1
2 0 0 0 0
0 0 2 0 0 0
1 0 0 0 0
0 0 0 0 2 0
After 2 seconds, the state of the robots is given below.
0 0 0 0 1 0
0 0 0 0 0
2 0 0 0 0
0 0 1 0 0 0
0 0 0 2 0 0
After 3 seconds, the state of the robots is given below.
0 0 0 1 0 0
0 0 0 0
2 0 0 0 0 0
0 0 0 1 0 0
0 0 2 0 0 0

Example Input/Output 2:
Input:
4 4
1 0 0 0
0 1 0 0
0 0 2 0
0 0 0 2
5

Output:
0 1 0 0
1 0 0 0
0 0 0 2
0 0 2 0

n,m=map(int,input().split())
l=[list(map(int,input().split())) for i in range(n)]
t=int(input())
d={}
lst=[]
for i in range(n):
    for j in range(m):
        if l[i][j]:
            if l[i][j]==1:
                d[(i,j)]=1
            else:
                d[(i,j)]=-1
            lst.append(l[i][j])
for i in range(t):
    d1={}
    for curr in d:
        j=curr[1]
        j+=d[curr]
        if j<0 or j>=m:
            j-=d[curr]
            d[curr]*=-1
            j+=d[curr]
        d1[(curr[0],j)]=d[curr]
    d=d1
for i in range(n):
    for j in range(m):
        if (i,j) in d:
            print(lst[i],end=' ')
        else:
            print(0,end=' ')
    print()
            

Leave a Reply

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

More posts. You may also be interested in.