So far i was able to come up with a code to generate a random number (1-4) in a
ID: 3846778 • Letter: S
Question
So far i was able to come up with a code to generate a random number (1-4) in a mx2 matrix with the first one being (0,0). I don't know where to start with the coding.
Vacuum Robot Path In this problem, we are going to study the displacement of a vacuum cleaner robot that randomly wanders around a closed square room The room is represented by a square grid L ft XLft, so there are (L+1? grid points," each of size 1 ft2 in the room. Each grid point is represented by (x,y) coordinates, where x and y are both integers The robot cleans the room by randomly moving from one grid point to another Whenever the robot stops on a grid point, the area around that grid point is cleaned The robot also cleans as it moves, meaning that if it moves from (x y) (1; 1) to (x, y) (1; 3), then the points defined by (x, y) (1; 1), (1; 2), and (1; 3) have now all been cleaned. The robot starts in the bottom left corner of the room (x, y) (0,0) The direction and length of each robot movement is determined by two random numbers, which are provided in the input argument rmat. The first movement is determined by the first row of rmat, the second movement is determined by the second row, and so on. The first number in each row determines the direction (1 for +y, 2 for -y, 3 for +x, and 4 for -x), and the second number in each row determines the length of each movement. For example, if the robot's current position is (x, y) (2, 3) and the next row in rmat is 1,3, then the robot's next position is (x, y) (2, 6) (if L 26) f all rows of rmat have been used and the robot still has not cleaned the entire room then the values of t are recycled, starting with the first row If a step would cause the robot to hit a wall, then the robot should not take that step but instead go to the next row of rmat Write a function with the header function IN, path] Robo vacuum (L, rmat where L determines the size of the room as described above and rmat is a MX 2 matrix of random integers 1 through 4, which will tell the robot where to move. Your function will output N, the number of steps it takes for the robot to cover the entire room, as well as path, a (N +1) X 2 matrix where each row contains (x, y) coordinates of every step The first row of path will be 0, 0, and the second row will have the (x, y) coordinates of the robot after the first step When testing your code, make sure that rmat is big enough that the robot doesn't get stuck in a repeating path that doesn't cover the entire room. The larger L you use, the more rows rmat should have. Reasonable values for L should range from about 5 to 15 along with a few hundred rows in rmat. Also, plot the path of the robot to confirm that it has covered all the squares in the roomExplanation / Answer
The algorithm that can solve the above problem is given below. I am using C++ for the code as no language specification is given.The code can be converted to any language as per the requirement.
step 1 ) Use a funcution is_valid to check whether the given coordinates are valid. It takes L as input
bool is_valid(int x, int y , int L )
{ if(x<0|| x>L|| y<0||y>L)
return false
}
step 2 ) I assume that you have created a funtion for generating the random number matrix. We will create a function as metntioned in the question RobotVaccum(L,rmt) as mentioned in the question. It takes the random matrix and the size of the grid as input
a) We first keep an L*L matrix to check whether the entire room has been covered. And create a function has covered which keeps checking whether the room is clean.Also another function which marks as grid to be cleaned if it is covered
More details are commented along with the algorithm provided
//
// Robot.cpp
//
//
//
//
#include "Robot.hpp"
c//clean_grids is a variable which keeps track of how many grids are clear. it is initialised with the value (L+1)^2 which is the number of grid points
bool is_valid(int x, int y , int L ) // if this returns false that meanse the robot hits the wall
{ if(x<0|| x>L|| y<0||y>L)
return false
else
return true
}
void mark_clean(vector<int> Check_matrix,int x, int y,int clean_grids )
{
if (Check_matrix[x][y] ) == 0 )
{
Check_matrix[x][y]= 1 ; // if a grid is clean we keep the grid value to be 1 else zero.
clean_grids-- ;
}
}
bool check_clean(int clean_grids)
{
if(clean_grids ==0)
return true ;
else
return false ;
}
a
// we will two global variables to store the answer values being the number of steps and the path. the path is represetned as a matrix
int steps;
vector<vector<int>> path ;
vector<vector<int>> Check_matrix ; // define it as an L*L matrix
void int add_path_horizontal_positive(int x , int y_old, int y_new )
{ for(int i = y_old ; i<=y_new ; i++)
path.push_back({x,i}) ;
mark_clean(Check_matrix,x,i,clean_grids) ; // mark the grid to be clean
}
void int add_path_horizontal_negative(int x , int y_old, int y_new)
{
for(int i = y_old ; i<=y_new ; i--)
{
path.push_back({x,i}) ;
mark_clean(Check_matrix,x,i,clean_grids) ;
}
}
void int add_path_vertical_positive(int y , int x_old, int x_new )
{ for(int i = x_old ; i<=x_new ; i++)
path.push_back({i,y}) ;
mark_clean(Check_matrix,i,y,clean_grids) ;
}
void int add_path_vertical_negative(int y , int x_old, int x_new)
{
for(int i = x_old ; i<=x_new ; i--)
path.push_back({i,y}) ;
mark_clean(Check_matrix,i,y,clean_grids) ;
}
void Robot_vaccum(int L , vector<vector<int>> rmt) //give the input rmt materix here
{ steps = 0 ;
vectro<int> temp = {0,0}
path.push_back(temp) ; // initializint the path matrix
int clean_grids = (L+1)^2 ;
rmt.pop() ;
int coordinates x =0, y=0 ;
while(check_clean(clean_grids))
{
while (!rmt.empty() )
{
vector<int> movement = rmt.top() ; // take the first row from the random matrix
switch (movement[0] )
{
case 1 : if(is_valid(x,y+movement[1])) // if the new coordinate is valid
{ int y_new = y + movement[1] ;
steps++; // increase the step numbers
add_path_horizontal_positive(x,y,y_new) ; // add the new path coordinates
y = y_new ;
} // set the new coordinate
break ;
case 2 :if(is_valid(x,y-movement[1]))
{ int y_new = y - movement[2] ;
N++ ;
add_path_horizontal_negative(x,y,y_new) ;
y = y_new;
}
break ;
case 3 : if(is_valid(x+movement[1],y))
{ int x_new= x + movement[1] ;
N++ ;
add_path_vertical_positive(y,x,x_new) ;
x = x_new ;
}
break ;
case 4 :if(is_valid(x-+movement[1]),y))
{ x_new = x - movement[2] ;
N++;
int y , int x_old, int x_new
}
break ;
}
rmt.pop() ;
}
if(rmt.empty()==0 && (clean_grids!=0)
{
// here add the code that you created to create a random matrix and refill the matrix
}
}
}
// The following is the funtion to print the path
void print_path(vector<vector<int>> Check_matrix,int L )
{
while(Check_matrix.empty()!=0)
{
for(int i=0 ; i <L ; i++ )
{
cout<<endl ;
for(int j =0 ; j < L ; j++)
{
cout<<Check_matrix[i][j]<<" " ;
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.