As a final year project certain student in
a college have designed a Robot which can move front, back, left or right in a
given rectangular grid of dimension L*B units (L denotes the length from left
to right and B denotes the breadth from top to bottom). Always the robot moves
in units which are of integer values. The robot cannot move outside the grid
(That is the robot cannot go beyond L and B units). A sequence of N movement
instructions are given to the robot to move in the desired direction (F-front or
up, B-back or down, L-left, R-right) and the robot moves if the destination
falls within the limit of the grid dimensions. Else the robot does not move.
Assume the robot always starts at the bottom left of the grid. The program must
print the number of movement instructions C for which the robot did not move
(as the destination was outside the grid)
Input
Format:
The first line contains L and B separated
by a space. The second line contains N which denotes the number of
instructions. The third line contains N instructions separated by one or more
spaces.
Output
Format:
The first line contains C which denoted
the count of instructions for which the robot did not move.
Boundary
Conditions:
1 <= L, B <= 999 1 <= N <= 999 C <= N
Example
Input/Output 1:
Input:
6 5
9
3R 2L 11R 4F 4R 2F 3B 5L 4B
Output:
3
Explanation: The robot did not move for
the instructions 11R, 2F and 4B as they will make the robot go outside the
grid.
Solution
1:
#include<stdio.h>
#include <stdlib.h>
int main()
{
int l,b,n,i,a,s=0,s1=0,count=0;
char c;
scanf("%d %d \n
%d",&l,&b,&n);
for(i=0;i<n;i++)
{
scanf("%d%c",&a,&c);
if(c=='R'&&s+a<=l)
s=s+a;
else if(c=='L'&&s-a>=0)
s=s-a;
else if(c=='F'&&s1+a<=b)
s1=s1+a;
else if(c=='B'&&s1-a>=0)
s1=s1-a;
else
count++;
}
printf("%d",count);
}
Solution
2:
l,b=[int(i) for i in input().split()]
n,a,x,y,c=int(input()),input().split(),0,0,0
for s in a:
m=int(s[:len(s)-1])
if s[-1]=='R' and x+m<=l:
x+=m
elif s[-1]=='L' and x-m>=0:
x-=m
elif s[-1]=='F' and y+m<=b:
y+=m
elif s[-1]== 'B' and y-m>=0:
y-=m
else:
c+=1
print(c)