Sunday, March 5, 2023
HomeSoftware EngineeringMethods to Remedy the Maze Runner in C

Methods to Remedy the Maze Runner in C


The problem

Introduction

Welcome Adventurer. Your goal is to navigate the maze and attain the end level with out touching any partitions. Doing so will kill you immediately!

Process

You'll be given a 2D array of the maze and an array of instructions. Your process is to observe the instructions given. For those who attain the tip level earlier than all of your strikes have gone, you must return End. For those who hit any partitions or go outdoors the maze border, you must return Lifeless. If you end up nonetheless within the maze after utilizing all of the strikes, you must return Misplaced.

The Maze array will appear like

maze = [[1,1,1,1,1,1,1],
        [1,0,0,0,0,0,3],
        [1,0,1,0,1,0,1],
        [0,0,1,0,0,0,1],
        [1,0,1,0,1,0,1],
        [1,0,0,0,0,0,1],
        [1,2,1,0,1,0,1]]

..with the next key

0 = Protected place to stroll
      1 = Wall
      2 = Begin Level
      3 = End Level
  instructions = "NNNNNEEEEE" == "End" // (instructions handed as a string)

Guidelines

1. The Maze array will at all times be sq. i.e. N x N however its measurement and content material will alter from take a look at to check.

2. The beginning and end positions will change for the ultimate exams.

3. The instructions array will at all times be in higher case and will probably be within the format of N = North, E = East, W = West and S = South.

4. For those who attain the tip level earlier than all of your strikes have gone, you must return End.

5. For those who hit any partitions or go outdoors the maze border, you must return Lifeless.

6. If you end up nonetheless within the maze after utilizing all of the strikes, you must return Misplaced.

The answer in C

Possibility 1:

#embrace <stddef.h>

char *maze_runner(const int *maze, size_t n, const char *instructions) 
{
  int x, y, cur;
  
  // find the beginning
  for (y = 0; y < n; y++) for (x = 0; x < n; x++) if (maze[y*n+x] == 2) goto strikes;
  
  strikes:
  for (char *d = instructions; *d; d++) 
  {
    swap (*d) {
      case 'N': y--; break;
      case 'S': y++; break;
      case 'E': x++; break;
      case 'W': x--; break;
    }
    if (x < 0 || y < 0 || y > n-1 || x > n-1)  return "Lifeless"; // out of bounds
    if ((cur = maze[y*n+x]) == 1)              return "Lifeless"; // hit wall 
    if (cur == 3)                              return "End"; // discovered exit
  }
  
  return "Misplaced";
}

Possibility 2:

#embrace <stddef.h>
#embrace <iso646.h>

#outline NORTH 'N'
#outline SOUTH 'S'
#outline EAST  'E'
#outline WEST  'W'

#outline START   2
#outline WALL    1
#outline FINISH  3

char *maze_runner(const int *maze, size_t n, const char *instructions) {
    size_t x, y;
    for (size_t i = 0; i < n; i++){
        for (size_t j = 0; j < n; j++)
            if (maze[i * n + j] == START){
                x = j;
                y = i;
            }
    }
    for (size_t i = 0; instructions[i]; i++){
        x += (instructions[i] == EAST) - (instructions[i] == WEST);
        y += (instructions[i] == SOUTH) - (instructions[i] == NORTH);
        if (maze[y * n + x] == WALL or x >= n or y >= n)
            return "Lifeless";
        if (maze[y * n + x] == FINISH)
            return "End";
    }
    return "Misplaced";
}

Possibility 3:

#embrace <stddef.h>

#outline DEAD(N, MAX) N < 0 || N > MAX - 1

void transfer(int *x, int *y, char dir) {
  swap (dir) {
      case 'N': *y -= 1; break;
      case 'S': *y += 1; break;
      case 'E': *x += 1; break;
      case 'W': *x -= 1; break;
  }
}

char *maze_runner(const int *maze, size_t n, const char *instructions) {
  int nums[n][n], x, y, i, *p;

  for (p = maze, i = 0; i < n * n; i++) {
    nums[i/n][i%n] = maze[i];
    if (*p++ == 2) x = ipercentn, y = i/n;
  }

  whereas (*instructions) 

  return "Misplaced";
}

Check circumstances to validate our resolution

#embrace <criterion/criterion.h>

void tester(const int *maze, size_t n, const char *instructions, char *final result);

Check(Example_Tests, should_pass_all_the_tests_provided) {

    #outline N 7

    const int maze[N * N] = {1, 1, 1, 1, 1, 1, 1,  
                             1, 0, 0, 0, 0, 0, 3, 
                             1, 0, 1, 0, 1, 0, 1,
                             0, 0, 1, 0, 0, 0, 1,    
                             1, 0, 1, 0, 1, 0, 1,
                             1, 0, 0, 0, 0, 0, 1,
                             1, 2, 1, 0, 1, 0, 1};
  
  //  maze is handed in as a 1-D int array with size n
  //  instructions are handed in as a null-terninated string
  //  don't allocate reminiscence, as an alternative return a string literal
  
  { const char *instructions = "NNNNNEEEEE";     tester(maze, N, instructions, "End"); }
  { const char *instructions = "NNNNNEESSEENNE"; tester(maze, N, instructions, "End"); }
  { const char *instructions = "NNNNNEEEEEWW";   tester(maze, N, instructions, "End"); }
  { const char *instructions = "NEEEE";          tester(maze, N, instructions,   "Misplaced"); }
  { const char *instructions = "NNNWW";          tester(maze, N, instructions,   "Lifeless"); }
  { const char *instructions = "NNNNNEESSSSSS";  tester(maze, N, instructions,   "Lifeless"); }
  
}
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments