Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Count the number of squares on a rectangular grid visited by a cleaning robot. The robot only moves forward, turning right if it cannot make

Count the number of squares on a rectangular grid visited by a cleaning robot. The robot only moves forward, turning right if it cannot make a move.
Task description
There is a cleaning robot which is cleaning a rectangular grid of size N x M, represented by array R consisting of N strings.
Rows are numbered from 0 to N1(from top to bottom) and columns are numbered from 0 to M1(from left to right).
The robot starts cleaning in the top-left corner, facing rightwards.
It moves in a straight line for as long as it can, in other words, while there is an unoccupied grid square ahead of it.
When it cannot move forward, it rotates 90 degrees clockwise and tries to move forward again until it encounters another obstacle, and so on.
Dots in the array (".") represent empty squares and "X"s represent occupied squares (ones the robot cannot move through).
Each square that the robot occupied at least once is considered clean. The robot moves indefinitely.
Write a function:
function solution(R);
that, given an array R consisting of N strings, each of length M, representing the grid, returns the number of clean squares.
Examples:
1. Given A =["...X..","....XX","..X..."], your function should return 6.
The robot starts at (0,0), facing rightwards, and moves to (0,2), where it turns due to the obstacle at (0,3).
Then it goes down from (0,2) to (1,2), where it changes direction again due to another obstacle.
Next it goes left from (1,2) to (1,0), where it turns once because of the grid boundary, then it moves once and turns once more, which makes it stand again at position (0,0) facing rightwards, just as at the beginning, which means it will now repeat the loop indefinitely.
The total number of cleaned squares is 6.
2. Given A =["....X..","X......",".....X.","......."], your function should return 15.
3. Given A =["...X.",".X..X","X...X","..X.."], your function should return 9.
4. Given A =["."], your function should return 1, because there is only one square on the grid and it is cleaned in the first move.
Assume that:
N and M are integers within the range [1..20];
top-left cell is empty;
each string in R consists only of the following characters: "." and/or "X".
In your solution, focus on correctness. The performance of your solution will not be the focus of the assessment.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Database Management

Authors: Jeff Hoffer, Ramesh Venkataraman, Heikki Topi

12th edition

133544613, 978-0133544619

More Books

Students also viewed these Databases questions