Create a maze using the supplied 20 times 15 grid Use a penc

Create a maze using the supplied 20 times 15 grid. Use a pencil so you can make changes! The start cell must be in the leftmost column and should be indicated by a circled S. The finish cell must be in the rightmost column and should be indicated by a circled F. A wall cell should be indicated by an W. Your maze should only have one solution and it should not contain any loops. Make the solution meander with many turns and dead-ends. Write a program to store your maze in a two-dimensional integer array. After the comment section and #include section, define the following manifest constants: #define ST 0 times 01//start cell #define FI 0 times 02//finish cell #define WA 0 times 04//wall cell #define lessthanorequalto 0 times 10//able to move left #define RI 0 times 20//able to move right #define UP 0 times 40//able to move up #define DN 0 times 80//able to move down After these #defines, declare and initialize a two-dimensional array for the maze: int Maze[15][20] =//Maze[row][col] {}; The origin of the maze is the upper-left cell, with the column number increasing to the right and the row number increasing downwards. Initialize the array by bitwise OR\'ing the previously defined manifest constants to set the bits for each grid location. A cell containing a w ill should be initialized to: WA A cell from which you can move to the right, up, or down should be initialized to: RI | UP | DN To help you keep track of the initialization values, use one line per cell and put a comment line above every 20 values, grouping the initial values for each row: int Maze[15][20] =//Maze[row][col] {//Row 0, left to right WA, WA, DN, WA, WA, RI, lessthanorequalto | RI, lessthanorequalto | DN, WA, WA, WA, RI, lessthanorequalto | DN, WA, WA, WA, WA, DN, WA, WA, //Row 1, left to right ...}; You should consider the maze to be surrounded by walls when initializing the edge cells.

Solution

Please find below the C Program to create a maze in 2-D using integer array.

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

#define MAXHEIGHT 120
#define MAXWIDTH 120
#define wall \'#\'
#define path_cell \'.\'

typedef struct {
char type;
int reachable;
int visited;
} mazecells_c;

typedef struct {
int hght;
int wdth;
mazecells_c M[MAXHEIGHT][MAXWIDTH];
} m_c;

void readmaze(m_c *maze);
void print(m_c *m);
void print_reachable(m_c *m);

int main(void) {
m_c MAZE;
readmaze(&MAZE);
print(&MAZE);
puts(\"\");
print_reachable(&MAZE);

return 0;
}

void readmaze(m_c *maze) {
int row=0, col=0;
char ch;
FILE *fp = stdin;//fopen(\"mmap.txt\", \"r\");
while(EOF != fscanf(fp, \"%c\", &ch)){
if(ch == wall || ch == path_cell){
maze->M[row][col].type = ch;
maze->M[row][col].reachable = 0;
maze->M[row][col].visited = 0;
++col;
} else if(ch == \'\ \'){
maze->wdth = col;
col = 0;
++row;
}
}
if(col != 0)
++row;
//fclose(fp);
maze->hght = row;
}

void print(m_c *m){
for(int r = 0; r < m->hght; ++r){
for(int c = 0; c < m->wdth; ++c){
putchar(m->M[r][c].type);
}
putchar(\'\ \');
}
}

typedef enum dir {
UP, RIGHT, DOWN, LEFT, FORWARD
} DIR;

typedef struct pos {
int r, c;
DIR dir;
} Pos;

typedef struct node {
Pos pos;
struct node *next;
} Node;

typedef struct queque {
Node *head, *tail;
} Queque;

Queque *new_queque(void){
Queque *q = malloc(sizeof(*q));
q->head = q->tail = NULL;
return q;
}

void enque(Queque *q, Pos pos){
Node *node = malloc(sizeof(*node));
node->pos = pos;
node->next = NULL;
if(q->head == NULL){
q->head = q->tail = node;
} else {
q->tail = q->tail->next = node;
}
}

Pos deque(Queque *q){
Pos pos = q->head->pos;
Node *temp = q->head;
if((q->head = q->head->next)==NULL)
q->tail = NULL;
free(temp);
return pos;
}

bool isEmpty_que(Queque *q){
return q->head == NULL;
}

Pos dxdy(DIR curr, DIR next){
Pos d = { 0, 0, 0};
switch(curr){
case UP:
switch(next){
case LEFT:
d.c -= 1;
break;
case FORWARD:
d.r -= 1;
break;
case RIGHT:
d.c += 1;
break;
}
break;
case RIGHT:
switch(next){
case LEFT:
d.r -= 1;
break;
case FORWARD:
d.c += 1;
break;
case RIGHT:
d.r += 1;
break;
}
break;
case DOWN:
switch(next){
case LEFT:
d.c += 1;
break;
case FORWARD:
d.r += 1;
break;
case RIGHT:
d.c -= 1;
break;
}
break;
case LEFT:
switch(next){
case LEFT:
d.r += 1;
break;
case FORWARD:
d.c -= 1;
break;
case RIGHT:
d.r -= 1;
break;
}
break;
}
return d;
}

Pos next_pos(Pos pos, DIR dir){
Pos dxy = dxdy(pos.dir, dir);
switch(dir){
case RIGHT:
pos.dir = (pos.dir + 1) % 4;
break;
case LEFT:
if((pos.dir = (pos.dir - 1)) < 0)
pos.dir += 4;
break;
case FORWARD:
break;
}
pos.r += dxy.r;
pos.c += dxy.c;
return pos;
}
static inline bool isValid(m_c *m, Pos pos){
if(pos.r < 0 || pos.r >= m->hght || pos.c < 0 || pos.c >= m->wdth || m->M[pos.r][pos.c].type == wall)
return false;
return true;
}
static inline bool isValidAndUnvisit(m_c *m, Pos pos){
return isValid(m, pos) && !m->M[pos.r][pos.c].reachable;
}

void print_reachable(m_c *m){
int i;
for(i = 0; i < m->wdth; ++i)
if(m->M[0][i].type == path_cell)
break;
Pos pos = { 0, i, DOWN};
Queque *q = new_queque();
enque(q, pos);
while(!isEmpty_que(q)){
pos = deque(q);
if(!m->M[pos.r][pos.c].reachable){
m->M[pos.r][pos.c].reachable = 1;

Pos next = next_pos(pos, LEFT);
if(isValidAndUnvisit(m, next))
enque(q, next);
next = next_pos(pos, FORWARD);
if(isValidAndUnvisit(m, next))
enque(q, next);
next = next_pos(pos, RIGHT);
if(isValidAndUnvisit(m, next))
enque(q, next);
}
}
free(q);
for(int r = 0; r < m->hght; ++r){
for(int c = 0; c < m->width; ++c){
if(m->M[r][c].reachable)
putchar(\'+\');
else if(m->M[r][c].type == path_cell)
putchar(\'-\');
else
putchar(m->M[r][c].type);
}
putchar(\'\ \');
}
}

 Create a maze using the supplied 20 times 15 grid. Use a pencil so you can make changes! The start cell must be in the leftmost column and should be indicated
 Create a maze using the supplied 20 times 15 grid. Use a pencil so you can make changes! The start cell must be in the leftmost column and should be indicated
 Create a maze using the supplied 20 times 15 grid. Use a pencil so you can make changes! The start cell must be in the leftmost column and should be indicated
 Create a maze using the supplied 20 times 15 grid. Use a pencil so you can make changes! The start cell must be in the leftmost column and should be indicated
 Create a maze using the supplied 20 times 15 grid. Use a pencil so you can make changes! The start cell must be in the leftmost column and should be indicated

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site