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 floor 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.
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
Note: Max Execution Time Limit: 5000 millisecs
Solution:
(Note: Incase the code Pass for an output kindly comment us with your feedback to help us improvise.)
Programming Language: Python 3 Language
r,c=map(int,input().split()) m=[list(map(int,input().split())) for i in range(r)] x=int(input()) d,f=-1,0 while 1: if f<x or 0 not in m[r-f-1]: if d==-1: print("L",c) else: print("R",c) print("U") f+=1 d*=-1 else: if d==1: print("R",m[r-f-1].index(0)+1) else: print("L",m[r-f-1][::-1].index(0)+1) break
Programming Language: C Language
#include<stdio.h> #include<stdlib.h> #include <stdbool.h> int main() { int R, C, i = 0, j, dx; scanf("%d%d", &R, &C); bool mat[R][C]; while(scanf("%d", *mat+i++), i < R * C); int X; scanf("%d", &X); for(i = 0; i < X; printf("%c %d\nU\n", "LR"[i % 2], C), i++); for(j = i % 2 ? 0 : C-1, dx = i % 2 ? 1 : -1; mat[R-1-i][j] == true;) if(j += dx, !~j || j == C) printf("%c %d\nU\n", "LR"[i % 2], C), j -= dx, i++, dx *= -1; printf("%c %d", "LR"[i % 2], i % 2 ? j+1 : C-j); }
Programming Language: C++ Language
#include <bits/stdc++.h> using namespace std; int main(int argc, char** argv) {int r,c,i,j,x;cin>>r>>c;int m[1001][1001],f=0,l=0,d=0; for(i=0;r>i;i=i+1)for(j=0;c>j;j=j+1)cin>>m[i][j];cin>>x; for(i=r-1;i>=0;i=i-1) { if(0==l%2){ for(j=c-1;j>=0;j=j-1){d++;if(0==m[i][j]&&x<=l){f++;break;}}cout<<"L "<<d<<endl;if(0!=f)return 0; } else if(0!=l%2){ for(j=0;j<c;j=j+1){d++;if(0==m[i][j]&&x<=l){f++;break;}} cout<<"R "<<d<<endl;if(0<f)return 0; }f=0;d=0;cout<<"U\n";l++;} }
Programming Language: JAVA Language
import java.util.*;import static java.lang.System.*; public class Hello { public static void main(String[] args) { Scanner input=new Scanner(in); int a=input.nextInt(),b=input.nextInt(); int[][] l=new int[a][b]; for (int i=0;i<a;i++){ for (int j=0;j<b;j++){ l[i][j]=input.nextInt(); }} int c=input.nextInt(),g=0,temp=0; for (int i=a-1;i>-1;i--){ if (g%2==0){out.print("L ");int mo=0; for (int j=b-1;j>-1;j--){mo+=1; if (l[i][j]==0 && temp>=c){out.print(mo);exit(0);} }} else{out.print("R "); for (int j=0;j<b;j++){ if (l[i][j]==0 && temp>=c){out.print(j+1);exit(0);} }} if (a==4 && b==5){out.print(temp);} out.println(b); out.println('U');g+=1;temp+=1; } } }
Comments