Multi-Storey Car Parking Lot

 Multi-Storey Car Parking Lot

PROBLEM STATEMENT : 

The program must accept an integer matrix of size R*C representing a multi-storey car parking lot. The integer 0 represents an empty slot and the integer 1 represents a car. The entry point is always present at the bottom-right of the ground floor. The way to reach the 1st floor is on the left side of the ground floor. The way to reach the 2nd floor is on the right side of the 1st floor. Similarly, the ways to the remaining floors are present on the left side and right side alternatively. A person wants to park his car in the first occurring empty slot on or above the Xth floor. The value of X is also passed as the input. The program must print the instructions he needs to follow to park his car as the output. The instructions must be in the following formats.

-> L followed by an integer K - It indicates that he needs to move K slots towards left. 

-> R followed by an integer K - It indicates that he needs to move K slots towards right. 

-> U - It indicates that he needs to go to the next floor. 

Note: At least one parking slot is always empty on or above the Xth floor. 

Boundary Condition(s): 

2 <= R, C <= 50 

0 <= X < R 

Input Format: 

The first line contains R and C separated by a space. 

The next R lines, each containing C integers separated by a space. 

Output Format: 

The lines containing the parking instructions. 

Example Input/Output 1: 
Input: () 
5 7 
1 0 0 0 0 0 0 
1 1 0 0 1 1 0 
0 1 1 0 1 1 0 
1 1 1 1 1 1 1 
1 1 1 1 1 1 1 
3  
Output: L 7 U R 7 U L 7 U R 3

Explanation: 
Here X = 3, the first occurring empty slot on the 3th floor is highlighted below. 
1 0 0 0 0 0 0 (4th Floor) 
1 1 0 0 1 1 0 (3rd Floor) 
0 1 1 0 1 1 0 (2nd Floor) 
1 1 1 1 1 1 1 (1st Floor) 
1 1 1 1 1 1 1 (Ground Floor) 
The instructions to park his car are given below. 
L 7 

R 7 

L 7 

R 3 

Example Input/Output 2: 
Input: () 
8 5 
0 0 0 0 0 
0 0 0 0 1 
1 1 1 1 1 
1 1 1 1 1 
1 1 1 1 1 
1 1 1 1 1 
1 1 1 1 1 
1 1 1 1 0 
2 
Output: L 5 U R 5 U L 5 U R 5 U L 5 U R 5 U L 2

Example Input/Output 3: 
Input: () 
4 5 
1 0 1 1 1 
0 0 1 0 1 
1 0 0 1 0 
1 0 1 1 1 
0  
Output: L 4



SOLUTION :

C (Programming Language)


      

#include<stdio.h> #include<stdlib.h> int main() { int r,c,i,j,flag=0,x; scanf("%d%d",&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",&x); for(i=r-1;i>=0;--i) { if(i>=r-x) { if(flag==0) { printf("L %d\nU\n",c); } else { printf("R %d\nU\n",c); } } else { if(flag==0) { for(j=0;j<c;++j) { //For reverse search of '0' if(a[i][c-j-1]==0) { printf("L %d",j+1); return 0; } } printf("L %d\nU\n",c); } else { for(j=0;j<c;++j) { //For forward search if(a[i][j]==0) { printf("R %d",j+1); return 0; } } printf("R %d\nU\n",c); } } flag=!flag; } }



C++ (CPP)

       

#include <bits/stdc++.h> using namespace std; int main(int argc, char** argv) { int r,c,i,j,x; bool flag=false; cin>>r>>c; int a[r][c]; for(i=0;i<r;++i) { for(j=0;j<c;++j) { cin>>a[i][j]; } } cin>>x; for(i=r-1;i>=0;--i) { if(i>=r-x) { if(!flag) { cout<<"L "<<c<<"\nU\n"; } else { cout<<"R "<<c<<"\nU\n"; } } else { if(!flag) { for(j=0;j<c;++j) { if(a[i][c-j-1]==0) { cout<<"L "<<j+1; return 0; } } cout<<"L "<<c<<"\nU\n"; } else { for(j=0;j<c;++j) { if(a[i][j]==0) { cout<<"R "<<j+1; return 0; } } cout<<"R "<<c<<"\nU\n"; } } flag=!flag; } }



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,x; int[][] a=new int[r][c]; Boolean flag=false; for(i=0;i<r;++i) { for(j=0;j<c;++j) { a[i][j]=sc.nextInt(); } } x=sc.nextInt(); for(i=r-1;i>=0;--i) { if(i>=r-x) { if(!flag) { System.out.println("L "+c+"\nU"); } else { System.out.println("R "+c+"\nU"); } } else { if(!flag) { for(j=0;j<c;++j) { if(a[i][c-j-1]==0) { System.out.println("L "+(j+1)); System.exit(0); //To stop program } } System.out.println("L "+c+"\nU"); } else { for(j=0;j<c;++j) { if(a[i][j]==0) { System.out.println("R "+(j+1)); System.exit(0); } } System.out.println("R "+c+"\nU"); } } flag=!flag; } } }



PYTHON

       

r,c=map(int,input().split()) l=[list(map(int,input().split())) for i in range(r)] flag=0 x=int(input()) for i in range(x): if i%2==0: print("L",c) else: print("R",c) print("U") if x%2==1: flag=1 for i in range(-x-1,-r-1,-1): if flag==1: if 0 in l[i]: print("R",l[i].index(0)+1) break else: print("R",c) print("U") else: if 0 in l[i]: print("L",l[i][::-1].index(0)+1) break else: print("L",c) print("U") flag=not flag



Never Stop Learning !!


Comments

Popular posts from this blog

DP (1) - Count number of ways to cover a distance

Zero Insert After K Times One

Left Number Twice Right