Zig-Zag Robots
Zig-Zag Robots
PROBLEM STATEMENT :
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
0 2 0 0 0 0
0 0 2 0 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
2 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 2 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
SOLUTION :
C (Programming Language)
#include<stdio.h>
#include<stdlib.h>
int main()
{
int r,c,i,j,t,div,mod,shifted_ind,temp,rev_two;
scanf("%d %d\n",&r,&c);
int a[r][c];
for(i=0;i<r;++i)
{
for(j=0;j<c;++j)
{
scanf("%d ",&a[i][j]);
}
}
scanf("%d",&t);
for(i=0;i<r;++i)
{
int one=-1,two=-1,new_ind;
for(j=0;j<c;++j)
{
if(a[i][j]==1) one=j;
if(a[i][j]==2) two=j;
}
if(one!=-1)
{
shifted_ind=one+t;
div=shifted_ind/(c-1);
mod=shifted_ind%(c-1);
if(shifted_ind<c)
new_ind=shifted_ind;
else if(div%2==1||(div%2==1&&mod==0))
new_ind=c-1-mod;
else
new_ind=mod;
temp=a[i][one];
a[i][one]=a[i][new_ind];
a[i][new_ind]=temp;
for(j=0;j<c;++j) printf("%d ",a[i][j]);
}
else
{
rev_two=c-two-1;
shifted_ind=rev_two+t;
div=shifted_ind/(c-1);
mod=shifted_ind%(c-1);
if(shifted_ind<c)
new_ind=shifted_ind;
else if(div%2==1||(div%2==1&&mod==0))
new_ind=c-1-mod;
else
new_ind=mod;
temp=a[i][two];
a[i][two]=a[i][c-new_ind-1];
a[i][c-new_ind-1]=temp;
for(j=0;j<c;++j) printf("%d ",a[i][j]);
}
printf("\n");
}
}
C++ (CPP)
#include <bits/stdc++.h>
using namespace std;
int main(int argc, char** argv)
{
int r,c,t,i,j,shifted_ind,new_ind,mod,div,rev_two;
cin>>r>>c;
int a[r][c];
for(i=0;i<r;++i)
{
for(j=0;j<c;++j)
{
cin>>a[i][j];
}
}
cin>>t;
for(i=0;i<r;++i)
{
int one=-1,two=-1;
for(j=0;j<c;++j)
{
if(a[i][j]==1) one=j;
if(a[i][j]==2) two=j;
}
if(one!=-1)
{
shifted_ind=one+t;
div=shifted_ind/(c-1);
mod=shifted_ind%(c-1);
if(shifted_ind<c)
new_ind=shifted_ind;
else if(div%2==1||(div%2==1&&mod==0))
new_ind=c-1-mod;
else
new_ind=mod;
swap(a[i][one],a[i][new_ind]);
}
else
{
rev_two=c-two-1;
shifted_ind=rev_two+t;
div=shifted_ind/(c-1);
mod=shifted_ind%(c-1);
if(shifted_ind<c)
new_ind=shifted_ind;
else if(div%2==1||(div%2==1&&mod==0))
new_ind=c-1-mod;
else
new_ind=mod;
swap(a[i][two],a[i][new_ind]);
swap(a[i][new_ind],a[i][c-new_ind-1]);
}
for(j=0;j<c;++j) cout<<a[i][j]<<" ";
cout<<"\n";
}
}
JAVA
import java.util.*;
public class Hello {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int r=sc.nextInt(),c=sc.nextInt(),i,j,t,div,mod,shifted_ind,new_ind,temp,rev_two;
int[][] a=new int[r][c];
for(i=0;i<r;++i)
{
for(j=0;j<c;++j)
{
a[i][j]=sc.nextInt();
}
}
t=sc.nextInt();
for(i=0;i<r;++i)
{
int one=-1,two=-1;
for(j=0;j<c;++j)
{
if(a[i][j]==1) one=j;
if(a[i][j]==2) two=j;
}
if(one!=-1)
{
shifted_ind=one+t;
div=shifted_ind/(c-1);
mod=shifted_ind%(c-1);
if(shifted_ind<c)
new_ind=shifted_ind;
else if(div%2==1||(div%2==1&&mod==0))
new_ind=c-1-mod;
else
new_ind=mod;
temp=a[i][one];
a[i][one]=a[i][new_ind];
a[i][new_ind]=temp;
}
else
{
rev_two=c-two-1;
shifted_ind=rev_two+t;
div=shifted_ind/(c-1);
mod=shifted_ind%(c-1);
if(shifted_ind<c)
new_ind=shifted_ind;
else if(div%2==1||(div%2==1&&mod==0))
new_ind=c-1-mod;
else
new_ind=mod;
temp=a[i][two];
a[i][two]=a[i][c-new_ind-1];
a[i][c-new_ind-1]=temp;
}
for(j=0;j<c;++j) System.out.print(a[i][j]+" ");
System.out.println();
}
}
}
PYTHON
r,c=map(int,input().split())
l=[list(map(int,input().split())) for i in range(r)]
t=int(input())
for lst in l:
if 1 in lst:
ind=lst.index(1)
shifted_ind=ind+t
div=shifted_ind//(c-1)
mod=shifted_ind%(c-1)
if shifted_ind<c:
lst[ind],lst[shifted_ind]=lst[shifted_ind],lst[ind]
print(*lst)
continue
if div%2==1 or (div%2==1 and mod==0):
new_ind=c-1-(mod)
else:
new_ind=mod
lst[ind],lst[new_ind]=lst[new_ind],lst[ind]
else:
lst=lst[::-1]
ind=lst.index(2)
shifted_ind=ind+t
div=shifted_ind//(c-1)
mod=shifted_ind%(c-1)
if shifted_ind<c:
lst[ind],lst[shifted_ind]=lst[shifted_ind],lst[ind]
lst=lst[::-1]
print(*lst)
continue
if div%2==1 or (div%2==1 and mod==0):
new_ind=mod
else:
new_ind=c-1-(mod)
lst[ind],lst[new_ind]=lst[new_ind],lst[ind]
print(*lst)
Comments
Post a Comment