Answered step by step
Verified Expert Solution
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 to Nfrom top to bottom and columns are numbered from to Mfrom left to right The robot starts cleaning in the topleft 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 degrees clockwise and tries to move forward again until it encounters another obstacle, and so on Dots in the array represent empty squares and Xs 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 solutionR; that, given an array R consisting of N strings, each of length M representing the grid, returns the number of clean squares. Examples: Given A XXXX your function should return The robot starts at facing rightwards, and moves to where it turns due to the obstacle at Then it goes down from to where it changes direction again due to another obstacle. Next it goes left from to where it turns once because of the grid boundary, then it moves once and turns once more, which makes it stand again at position facing rightwards, just as at the beginning, which means it will now repeat the loop indefinitely. The total number of cleaned squares is Given A XXX your function should return Given A XXXXXX your function should return Given A your function should return 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 ; topleft cell is empty; each string in R consists only of the following characters: andor X In your solution, focus on correctness. The performance of your solution will not be the focus of the assessment.
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 to Nfrom top to bottom and columns are numbered from to Mfrom left to right
The robot starts cleaning in the topleft 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 degrees clockwise and tries to move forward again until it encounters another obstacle, and so on
Dots in the array represent empty squares and Xs 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 solutionR;
that, given an array R consisting of N strings, each of length M representing the grid, returns the number of clean squares.
Examples:
Given A XXXX your function should return
The robot starts at facing rightwards, and moves to where it turns due to the obstacle at
Then it goes down from to where it changes direction again due to another obstacle.
Next it goes left from to where it turns once because of the grid boundary, then it moves once and turns once more, which makes it stand again at position facing rightwards, just as at the beginning, which means it will now repeat the loop indefinitely.
The total number of cleaned squares is
Given A XXX your function should return
Given A XXXXXX your function should return
Given A your function should return 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 ;
topleft cell is empty;
each string in R consists only of the following characters: andor 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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started