Jethro and Cletus are bored in quarantine and model their apartment as an ASCII grid. Jethro wants a program to label each accessible space in the grid with the minimum number of steps from their computer ('A'). The assistant is provided example grids and the expected labeled outputs. They are tasked with writing a recursive C program, paintRoom.c, to solve this problem.
Find shortest path labeling small apartment locations A-Z
1. Please answer in C! I provided my current code, my code is currently not painting the room with
values from A to 'Z'
Jethro and Cletus are quarantined at home and bored. They spend most of their day sitting
at their computer monitor working or browsing the internet in their small apartment. Out of
boredom Jethro begins counting the number of steps needed to reach each location in their
small apartment. After seeing that taking different paths from their computer to their coffee
maker yields different numbers of steps a question dawns on them. They want to know what
is the fewest number of steps needed to reach all of the locations in their small apartment
starting from their computer.
Fortunately, Jethro is quite skilled at ASCII art. So they model their room with ASCII
characters. A space can be moved through. An asterisk * is a wall or furniture that
cannot be traversed (no climbing over furniture!). Going up, down, left, right one space
takes exactly one step (well assume no diagonal movements for that sake of simplicity). For
example, here is a possible model of their room:
**************
* A *
* * *
* * *
* *** *
* *
* ***** *
* *** *
**************
Assume that (0, 0) is the upper lefthand corner. For the sake of simplicity you can assume
the apartment is enclosed in * characters and that the location of Jethros computer has
been marked with an A.
Jethro is still new to programming and wants to hire you to write the program to label all
of the locations in their apartment with the minimum number of steps needed to reach
them. To keep with the ASCII art theme youll use the letters A-Z Such that:
A is 0 steps
B is 1 step
C is 2 steps
...
Y is 24 steps
2. Z is 25 (or more) steps
Heres some example rooms:
Example 1:
Base room:
*****
*A *
*** *
* *
*****
Room after algorithm:
*****
*ABC*
***D*
*GFE*
*****
(We cant pass through the * symbols)
Example 2:
Base room:
**************
* A *
* * *
* * *
* *** *
* *
* ***** *
* *** *
**************
Room after algorithm:
**************
*BABCDEFGHIJK*
*CBCD*FGHIJKL*
*DCDE*GHIJKLM*
*ED***HIJKLMN*
*FEFGHIJKLMNO*
3. *GFGHI*****OP*
*HGHIJ***RQPQ*
**************
Example 3:
Base room:
**********************************
* A *
**********************************
Room after algorithm:
**********************************
*DCBABCDEFGHIJKLMNOPQRSTUVWXYZZZZ*
**********************************
(We dont count past Z since Jethro has a pretty small apartment)
Example 4:
Base room:
*******
*A *
**** *
* * *
**** *
* *
*******
Room after algorithm:
*******
*ABCDE*
****EF*
* *FG*
****GH*
*KJIHI*
*******
(Unreachable areas should remain unchanged)Heres some example rooms:
Example 1:
Base room:
*****
*A *
*** *
4. * *
*****
Room after algorithm:
*****
*ABC*
***D*
*GFE*
*****
(We cant pass through the * symbols)
Example 2:
Base room:
**************
* A *
* * *
* * *
* *** *
* *
* ***** *
* *** *
**************
Room after algorithm:
**************
*BABCDEFGHIJK*
*CBCD*FGHIJKL*
*DCDE*GHIJKLM*
*ED***HIJKLMN*
*FEFGHIJKLMNO*
*GFGHI*****OP*
*HGHIJ***RQPQ*
**************
Example 3:
Base room:
**********************************
* A *
**********************************
Room after algorithm:
5. **********************************
*DCBABCDEFGHIJKLMNOPQRSTUVWXYZZZZ*
**********************************
(We dont count past Z since Jethro has a pretty small apartment)
Example 4:
Base room:
*******
*A *
**** *
* * *
**** *
* *
*******
Room after algorithm:
*******
*ABCDE*
****EF*
* *FG*
****GH*
*KJIHI*
*******
(Unreachable areas should remain unchanged)
Part 1 - Programming: (15 points)
Write a brute force recursive program to solve the problem described above in paintRoom.c.
Hint 1: This program will take fewer lines of code than previous one but youll need to
think about and test them carefully. For reference, my recP aintRoom only had 10 lines of
code in it.
Hint 2: From any given space, you need to try to continue moving up, down, left, and right
(i.e. 4 recursive calls).
Hint 3: Your algorithm can move into a *, but upon recognizing it as an obstacle, it should
return from that call.
Hint 4: It may help to track the distance traveled so far from the starting A character.
Hint 5: Only updating locations that contain a wont be enough. Sometimes youll need
to update a location you previously visited and labeled.
6. Hint 6: chars can be treated like small valued integers. In particular, it may be helpful to
use operations like + and <= with your chars.
PROGRAMS
driver.c
#include
#include
#include
#include "paintRoom.h"
RoomData read2DArray( const char* name );
void print2DArray( RoomData room );
void free2DArray( RoomData room );
/* change the following constants to add/remove some cases for testing */
const int numFiles = 8;
const char *defaultFilenames[] = { "room-Small01.txt", "room-Small02.txt", "room-
Medium01.txt", "room-Medium02.txt", "room-Long01.txt", "room-Long02.txt", "room-
Large01.txt", "room-Large02.txt" };
const bool enableFilenames[] = { true , true , true , true , true
, true , true , true };
/* DO NOT MODIFY THIS FILE */
int main( int argc, char *argv[] )
{
int i;
RoomData room;
printName( );
printf("Running default test files: nn");
for( i=0; i
#include
/* DO NOT MODIFY THIS FILE */
typedef struct RoomData
{
char **roomArray; /* the 2d char array representing the room shared by Jethro and Cletus
*/
int numrows; /* the number of rows for the char** roomArray */
7. int numcols; /* the number of columns for the char** roomArray */
} RoomData;
void printName( );
void paintRoom( RoomData room );
#endif
paintroom.c
#include "paintRoom.h"
void recPaintRoom( RoomData room, int row, int col, int distanceFromA /*feel free to
remove/add any other parameters here*/ );
/*declare any other helper functions here*/
/* printName
* input: none
* output: none
*
* Prints name the student who worked on this solution
*/
void printName( )
{
/* TODO : Fill in your name*/
printf("nThis solution was completed by:n");
printf("student namen");
}
/* TODO
* paintRoom
* input: the room to process
* output: N/A
*
* This non-recursive function is called by the driver and it makes the initial call to recursive
function recPaintRoom.
*/
void paintRoom( RoomData room )
{
/* Call any other helper functions (a helper function to find the location of 'A' in room may be
8. handy) */
int i;
int j;
/* Find starting location */
int startRow = -1;
int startCol = -1;
for( i = 0; i < room.numrows; i++ ){
for( j = 0; j < room.numcols; j++ ) {
if( room.roomArray[i][j] == 'A') { /*STOPPING POINT
USE FUNCTIONS IN MAKE FILE NOT .roomArray NEXT */
startRow = i;
startCol = j;
break;
}
}
if( startRow != -1) {
break;
}
}
/*Call your recursive function here */
if( startRow != -1) {
int currentValue = 0;
recPaintRoom( room, startRow, startCol, currentValue);
}
}
/* TODO
* recPaintRoom
* input: the room to process, the row and column of the current location being explored, the
distance traveled from 'A'
* output: N/A
*/
void recPaintRoom( RoomData room, int row, int col, int distanceFromA )
9. {
/* debugging output */
printf("Exploring (%d,%d) with label '%c'n", row, col, distanceFromA );
/* Base cases: */
if( row < 0 || row >= room.numrows || col < 0 || col >= room.numcols ||
room.roomArray[row][col] == '*') {
return;
}
/*Check if current location is an obstacle or already labeled */
if( room.roomArray[row][col] == 'A' || (room.roomArray[row][col] >= 'A' &&
room.roomArray[row][col] <= 'Z' )){
return;
}
else
/* Update label for current location */
room.roomArray[row][col] = distanceFromA;
/* Recursive cases: */
recPaintRoom(room, row - 1, col, distanceFromA + 1); /* Move up */
recPaintRoom(room, row + 1, col, distanceFromA + 1); /* Move down */
recPaintRoom(room, row, col - 1, distanceFromA + 1); /* Move Left */
recPaintRoom(room, row, col + 1, distanceFromA + 1); /* Move Right */
/* Debugging output */
printf("completed rec call with (%d, %d)n", row, col);
}