Skip to content

How to Solve the Maze Runner in C

The challenge

Introduction

Welcome Adventurer. Your aim is to navigate the maze and reach the finish point without touching any walls. Doing so will kill you instantly!

Task

You will be given a 2D array of the maze and an array of directions. Your task is to follow the directions given. If you reach the end point before all your moves have gone, you should return Finish. If you hit any walls or go outside the maze border, you should return Dead. If you find yourself still in the maze after using all the moves, you should return Lost.

The Maze array will look 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]]
Code language: plaintext (plaintext)

..with the following key

      0 = Safe place to walk
      1 = Wall
      2 = Start Point
      3 = Finish Point
directions = "NNNNNEEEEE" == "Finish" // (directions passed as a string)
Code language: plaintext (plaintext)

Rules

1. The Maze array will always be square i.e. N x N but its size and content will alter from test to test.

2. The start and finish positions will change for the final tests.

3. The directions array will always be in upper case and will be in the format of N = North, E = East, W = West and S = South.

4. If you reach the end point before all your moves have gone, you should return Finish.

5. If you hit any walls or go outside the maze border, you should return Dead.

6. If you find yourself still in the maze after using all the moves, you should return Lost.

The solution in C

Option 1:

#include <stddef.h> char *maze_runner(const int *maze, size_t n, const char *directions) { int x, y, cur; // locate the start for (y = ; y < n; y++) for (x = ; x < n; x++) if (maze[y*n+x] == 2) goto moves; moves: for (char *d = directions; *d; d++) { switch (*d) { case 'N': y--; break; case 'S': y++; break; case 'E': x++; break; case 'W': x--; break; } if (x < || y < || y > n-1 || x > n-1) return "Dead"; // out of bounds if ((cur = maze[y*n+x]) == 1) return "Dead"; // hit wall if (cur == 3) return "Finish"; // found exit } return "Lost"; }
Code language: C++ (cpp)

Option 2:

See also  Top-down vs Bottom-up Database Design
#include <stddef.h> #include <iso646.h> #define NORTH 'N' #define SOUTH 'S' #define EAST 'E' #define WEST 'W' #define START 2 #define WALL 1 #define FINISH 3 char *maze_runner(const int *maze, size_t n, const char *directions) { size_t x, y; for (size_t i = ; i < n; i++){ for (size_t j = ; j < n; j++) if (maze[i * n + j] == START){ x = j; y = i; } } for (size_t i = ; directions[i]; i++){ x += (directions[i] == EAST) - (directions[i] == WEST); y += (directions[i] == SOUTH) - (directions[i] == NORTH); if (maze[y * n + x] == WALL or x >= n or y >= n) return "Dead"; if (maze[y * n + x] == FINISH) return "Finish"; } return "Lost"; }
Code language: PHP (php)

Option 3:

#include <stddef.h> #define DEAD(N, MAX) N < 0 || N > MAX - 1 void move(int *x, int *y, char dir) { switch (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 *directions) { int nums[n][n], x, y, i, *p; for (p = maze, i = ; i < n * n; i++) { nums[i/n][i%n] = maze[i]; if (*p++ == 2) x = i%n, y = i/n; } while (*directions) { move(&x, &y, *directions++); if (DEAD(x, n) || DEAD(y, n) || nums[y][x] == 1) return "Dead"; if (nums[y][x] == 3) return "Finish"; } return "Lost"; }
Code language: C++ (cpp)

Test cases to validate our solution

#include <criterion/criterion.h> void tester(const int *maze, size_t n, const char *directions, char *outcome); Test(Example_Tests, should_pass_all_the_tests_provided) { #define N 7 const int maze[N * N] = {1, 1, 1, 1, 1, 1, 1, 1, , , , , , 3, 1, , 1, , 1, , 1, , , 1, , , , 1, 1, , 1, , 1, , 1, 1, , , , , , 1, 1, 2, 1, , 1, , 1}; // maze is passed in as a 1-D int array with length n // directions are passed in as a null-terninated string // do not allocate memory, instead return a string literal { const char *directions = "NNNNNEEEEE"; tester(maze, N, directions, "Finish"); } { const char *directions = "NNNNNEESSEENNE"; tester(maze, N, directions, "Finish"); } { const char *directions = "NNNNNEEEEEWW"; tester(maze, N, directions, "Finish"); } { const char *directions = "NEEEE"; tester(maze, N, directions, "Lost"); } { const char *directions = "NNNWW"; tester(maze, N, directions, "Dead"); } { const char *directions = "NNNNNEESSSSSS"; tester(maze, N, directions, "Dead"); } }
Code language: C++ (cpp)

See also  How to Check for All Inclusive in C
Tags:
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x