Please I need the mark for this Q to pass the course :( . I need help with any t
ID: 3776428 • Letter: P
Question
Please I need the mark for this Q to pass the course :( . I need help with any thing you can.
I put some hints to write the correct code in C WITHOUT USING <IOSTREAM> (Edit the code below or cpmplete or write new one)
Write a C program which will simulate robot movements in a maze. The maze has corridors and walls, and robot can move only in one of the four different directions: North, South, East, and West.
As an input you will be given a maze of rectangular shape divided into cells of fixed width. You are guaranteed that the maze will not exceed 60 × 60 dimensions. The maze will be given in the input, with a grid of characters ‘X’ and ‘.’, where ‘X’ denotes a wall and ‘.’ denotes an empty space.
The robot can move through empty spaces, but not through the wall. You can also assume that the robot cannot leave the maze; which is equivalent to the whole maze being surrounded by a wall.
After the maze you will be given the initial position of the robot with two numbers specifying row and column (the top left position is (1,1)). You will assume that the robot is
facing North (up on the maze grid). Following this, you will be given a sequence of commands consisting of letters, with possibly having whitespace between them. The letter commands are as follows:
R is a command to rotate the robot 90 degrees clockwise (to the right),
L is a command to rotate the robot 90 degrees counter-clockwise (to the left),
F moves the robot forward one cell, unless there is a wall preventing this, in which case the robot does nothing, and
Q denotes the final position. You should print out the current robot row, column, and orientation, and assume that the next test case follows. You will also print the original map with the letter ‘R’ being placed on all squares visited by the robot.
-------------------------------------------------------------------------------------------
Input
The input consists of several test cases. Each test case starts with a line with two integer numbers, r and c, where r is the number of rows and c is the number of columns. These numbers will be positive integers, except in the last line, which will be “0 0”, indicating the end of input.
After these numbers, there will be r lines describing the maze, each line corresponding to one row of cells. Each row of cells will contain a sequence of c characters describing the content of the cell. The X letter (‘X’) indicates a wall, and a dot character (‘.’) indicates an empty cell.
After the maze, the input will include two integers, which are the starting robot position, followed by the commands to move the robot. The commands consists of the letter commands specified earlier, with exactly one occurence of ‘Q’ at the end. The letter commands may be broken by arbitrary whitespaces.
Output
You program must print the final row, column, and orientation of the robot for each test case, followed by the map of robot movements. The orientation is denoted as N (for North or up), S (for South or down), E (for East or left), and W (for West or right). The map that follows should have the letter ‘R’ placed in all squares visited by the robot.
-------------------------------------------------------------------------------------------------
Sample Input
2 2
..
..
1 1
RFRFQ
7 8
XXXXXXXX
X.X.X.XX
X.X....X
X.X.XX.X
X.X.X..X
X...X.XX
XXXXXXXX
2 4
RRFLFF FFR
FF
RFFQ
0 0
Sample Output
2 2 S
RR
.R
5 6 W
XXXXXXXX
X.XRX.XX
X.XRRRRX
X.X.XXRX
X.X.XRRX
X...X.XX
XXXXXXXX
--------------------------------------------------------------
Edite Code with no using of <iostream.h> becouse my university terminal is C98 (C program)
#include <iostream>
using namespace std;
class Robot
{
public:
int x;
int y;
int dir = 0;
char get_dir()
{
switch (dir)
{
case 0:
return 'N';
case 1:
return 'W';
case 2:
return 'S';
case 3:
return 'E';
}
}
void get_status()
{
cout << x << ' ' << y << ' ' << get_dir() << endl;
}
void move(char maze[62][62], int r, int c)
{
int op;
if (dir < 2)
op = -1;
else
op = 1;
if (dir % 2 == 1)
if (x < c && x > 1 && maze[y][x + op] == '.')
x += op;
else
if(y < r && y > 1 && maze[y + op][x] == '.')
y += op;
}
void turn(char where)
{
if (where == 'L')
dir = ++dir % 4;
if (where == 'R')
dir = --dir % 4;
}
void wtf(char what, char maze[62][62], int r, int c)
{
if (what == 'Q')
get_status();
else if (what == 'F')
move(maze, r, c);
else
turn(what);
}
};
int main()
{
int r, c;
cin >> r >> c;
while (r != 0 && c != 0)
{
Robot pepito;
char omgjsuislost[62][62];
for (int i = 1; i <= r; i++)
for (int j = 1; j <= c; j++)
cin >> omgjsuislost[i][j];
cin >> pepito.x >> pepito.y;
char dequosser = ' ';
while (dequosser != 'Q')
{
cin >> dequosser;
pepito.wtf(dequosser, omgjsuislost, r, c);
}
cin >> r >> c;
}
return 0;
}
-------------------------------------------------------------------------------
OR complete this other parts of the Code pleeeesee
bool solveMaze(int maze[62][62], int r, int c, int sol[N][N])
{
if(r== 62-1 && c == 62-1)
{
sol[r][c] = 1;
return true;
}
if(c >= 0 && c < 62 && r >= 0 && y < 62 && maze[62][62] == 1)
{
sol[r][c] = 1;
if (solveMaze(maze, c+1, r, sol) == true)
return true;
if (solveMaze(maze, c, r+1, sol) == true)
return true;
sol[r][c] = 0;
return false;
}
return false;
}
--------------------------------------------------------------------------------------------------
Hint codes Could be helpful to write my C code :
/ #include "algorithm" if you think u require
#include <iostream>
#include <fstream>
using namespace std;
#include<stdio.h>
// Maze size
#define N 4
bool bvisited_grey_cell=false;
bool solveMazeUtil(int maze[N][N], int x, int y, int sol[N][N]);
void printSolution(int sol[N][N])
{
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
printf(" %d ", sol[i][j]);
printf(" ");
}
}
/* A valid index for N*N maze (X,Y) */
bool isSafe(int maze[N][N], int x, int y)
{
//CHECKING FOR POSSIBLE PATH/S AS MANY SOLUTIONS ARE POSSIBLE
if(x >= 0 && x < N && y >= 0 && y < N && (maze[x][y] == 1 || maze[x][y]==2))//GREY CELL IS ALSO SAFE
// if (x,y outside maze) return false
return true;
return false;
}
/*solves the Maze problem by using Backtracking*/
bool solveMaze(int maze[N][N])
{
int sol[N][N] = { {0, 0, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0}
};
if(solveMazeUtil(maze, 0, 0, sol) == false)
{
printf("Solution NOT exists");
return false;
}
printSolution(sol);
return true;
}
/* A recursive utility function to solve Maze problem */
bool solveMazeUtil(int maze[N][N], int x, int y, int sol[N][N])
{
// if (x,y is goal) return true
if(x == N-1 && y == N-1)
{
if (bvisited_grey_cell==true)// CHECK IS GREY CELL VISITED
{
sol[x][y] = 1;
return true;
}
}
// Check if maze[x][y] is valid
if(isSafe(maze, x, y) == true)
{
// mark x,y as part of solution path
if (maze[x][y]==2)// ARE WE ON GREY CELL? MARK IT.
bvisited_grey_cell=true;
sol[x][y] = 1;
/* Move forward in x direction */
if (solveMazeUtil(maze, x+1, y, sol) == true)
return true;
/* If x moving in x direction doesn't give solution then
Move down in y direction */
if (solveMazeUtil(maze, x, y+1, sol) == true)
return true;
/* If none of the above movements work then BACKTRACK:
unmark x,y as part of solution path */
sol[x][y] = 0;
return false;
}
return false;
}
int main()
{
// CHANGE THE MAZE TO TEST DIFFERENT CASE
int maze[N][N] = { {1, 0, 0, 0},
{1, 1, 2, 1},
{0, 1, 0, 0},
{1, 1, 1, 1}
};
solveMaze(maze);
getchar();
return 0;
}
Explanation / Answer
#include <stdio.h>
#define ROBOT
#define WALL
#define NONE
#define MAX_ROWS
#define MAX_COLS
#define EAST
#define NORTH
#define WEST
#define SOUTH
void sacn_state( int *pnrows,
int ncols[MAX_ROWS],
int object[MAX_ROWS][MAX_COLS],
int *pdirection
) ;
void print_state( int nrows,
int ncols[MAX_ROWS],
int object[MAX_ROWS][MAX_COLS],
int direction
) ;
int main ( void)
{
int rows; // number of rows in environment
int ncols[MAX_ROWS]; // number of columns in each row
int object[MAX_ROWS][MAX_COLS]; // object at each location
int direction ; // which way the robot is facing
scan_state(&nrows, ncols, object, &direction) ; // scan the state of the environment
printf_state( nrows, ncols, object, direction ) ; // print the state of the environment
return 0 ;
}
void scan_state ( int *pnrows,
int ncols[MAX_ROWS],
int object[MAX_ROWS}{MAX_COLS],
int *pdirection
)
{
int ch;
int r, c, rr, rc ;
r = 0;
c = -1 ;
while ( ( ch = getchar( ) ) != '(' ) {
c++;
if ( ch == ' ' ) {
ncols[r++] = c;
c = -1;
}
else if ( ch == ' * ' ) {
object[r] [c] = WALL ;
}
else if ( ( ch>=' A ' ) && ( ch <= 'Z') ) {
object [r] [c] = ch - ' A ' ;
}
else if ( ch == ' ^ ' ) {
object [r ][ c] = ROBOT ;
*pdirection = NORTH ;
}
else if ( ch == ' > ' ) {
object [r][c] =ROBOT;
*pdirection = EAST ;
}
else if ( ch == ' v' ) {
object [r][c] = ROBOT;
*pdirection =EAST ;
}
else if ( ch ==' v ' ) {
object [r][c] =ROBOT ;
*pdirection = SOUTH ;
}
else if ( ch == ' < ' ) {
object [ r] [c ] = ROBOT ;
*pdirection = WEST;
}
else {
object [r] [c] = NONE ;
}
}
// give the robot its own cordinates.
if (object[r][ c] == ROBOT ) {
object[rr][rc] = ROBOT ;
}
while ( ( ch = getcahr( ) ) != ' ) ' ) {
c++;
if ( ch == ' ' ) {
ncols[ r++] = c ;
c = -1;
}
else if ( ch == 'L' ) {
if ( *pdirection == SOUTH ) {
*pdirection = EAST ;
}
else {
*pdirection++ ;
}
}
else if ( ch == 'R' ) {
if ( *pdirection == EAST ) {
*pdirection = SOUTH ;
}
else {
*pdirection -- ;
}
}
}
*pnows = r ;
}
void print_state ( int rows,
int ncols[MAX_ROWS],
int object[MAX_ROWS] [MAX_COLS],
int direction
)
{
int r, c ;
for ( r = 0; r < nrows ; r++ ) {
for( c=0; c < ncols[r]; c++ ) {
if ( object[ r] [c ] = WALL ) {
printf ( " * " ) ;
}
if ( object [r] [c] == NONE ) {
print ( " " ) ;
}
if ( ( object [r] [c] >= 0 ) && ( object [ r] [c] <= 25 ) ) {
printf( "%c" , 'A'+object [r] [c] ) ;
}
if ( object [ r] [c] == ROBOT ) {
if ( direction [r] [c] == NORTH ) {
printf( " ^ " ) ;
}
if ( direction == EAST ) {
printf ( " > " ) ;
}
if ( direction == SOUTH) {
printf ( " v " ) ;
}
if ( direction == WEST ) {
printf ( " < " ) ;
}
}
}
printf( " " ) ;
}
}