human
Check if the robot is within the bounds of the grid after given moves | Function that returns true if the robot is safe.
If current move is " L " then increase the counter of coll.
If value of coll is equal to column then break.
If current move is " R " then increase the counter of colr.
If value of colr is equal to column then break.
If current move is " U " then increase the counter of rowu.
If value of rowu is equal to row then break.
If current move is " D " then increase the counter of rowd.
If value of rowd is equal to row then break.
If robot is within the bounds of the grid.
Unsafe.
Driver code"
Solution in Python:
assistant
#Here is code to check if the robot is within the bounds of the grid after given moves and the other steps required.
 def isSafe ( N , M , str ) :
	coll = 0
	colr = 0
	rowu = 0
	rowd = 0
	for i in range ( len ( str ) ) :
		if ( str [ i ] == ' L ' ) :
			coll += 1
			if ( colr > 0 ) :
				colr -= 1
			if ( coll == M ) :
				break
		elif ( str [ i ] == ' R ' ) :
			colr += 1
			if ( coll > 0 ) :
				coll -= 1
			if ( colr == M ) :
				break
		elif ( str [ i ] == ' U ' ) :
			rowu += 1
			if ( rowd > 0 ) :
				rowd -= 1
			if ( rowu == N ) :
				break
		elif ( str [ i ] == ' D ' ) :
			rowd += 1
			if ( rowu > 0 ) :
				rowu -= 1
			if ( rowd == N ) :
				break
	if ( abs ( rowd ) < N and abs ( rowu ) < N and abs ( coll ) < M and abs ( colr ) < M ) :
		return True
	return False
if __name__ == ' _ _ main _ _ ' :
	N = 1
	M = 1
	str = " R "
	if ( isSafe ( N , M , str ) ) :
		print ( " Yes " )
	else :
		print ( " No " )