# How to Resolve the Labyrinth Runner in C

## The obstacle #

Intro

` Invite Traveler. Your goal is to browse the labyrinth and reach the surface point without touching any walls. Doing so will eliminate you immediately!`

Job

` You will be provided a 2D range of the labyrinth and a variety of instructions. Your job is to follow the instructions provided. If you reach completion point prior to all your relocations have actually gone, you need to return Complete. If you strike any walls or go outside the labyrinth border, you need to return Dead. If you discover yourself still in the labyrinth after utilizing all the relocations, you need to return Lost.`

The Labyrinth range will appear like

`````` labyrinth = [[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 following secret

``` 0 = Safe location to stroll.
1 = Wall.
2 = Start Point.
3 = Complete Point```
`````` instructions="NNNNNEEEEE" == "Complete"// (instructions passed as a string).
``````

Guidelines

``` 1. The Labyrinth range will constantly be square i.e. N x N however its size and material will change from test to test.

2. The start and surface positions will alter for the last tests.

3. The instructions range will constantly remain in upper case and will remain in the format of N = North, E = East, W = West and S = South.

4. If you reach completion point prior to all your relocations have actually gone, you need to return Complete.

5. If you strike any walls or go outside the labyrinth border, you need to return Dead.

6. If you discover yourself still in the labyrinth after utilizing all the relocations, you need to return Lost.```

## The service in C #

Choice 1:

`````` #include << stddef.h>>.

char * maze_runner( const int * labyrinth, size_t n, const char * instructions).
{
int x, y, cur;.

// find the start.
for (y = 0; y < < n; y++) for (x = 0; x < < n; x++) if (labyrinth[y*n+x] == 2) goto relocations;.

relocations:.
for (char * d = instructions; * d; d++).
{
switch (* 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 "Dead";// out of bounds.
if (( cur = labyrinth[y*n+x]) == 1) return "Dead";// struck wall.
if (cur == 3) return "Complete";// discovered exit.
}

return "Lost";.
}
``````

Choice 2:

`````` #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 surface 3.

char * maze_runner( const int * labyrinth, 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 (labyrinth[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 (labyrinth[y * n + x] == WALL or x >>= n or y >>= n).
if (labyrinth[y * n + x] == SURFACE).
return "Complete";.
}
return "Lost";.
}
``````

Choice 3:

`````` #include << stddef.h>>.

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

void relocation( 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 * labyrinth, size_t n, const char * instructions) {
int nums[n][n], x, y, i, * p;.

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

while (* instructions)

return "Lost";.
}
``````

## Test cases to verify our service #

`````` #include << criterion/criterion. h>>.

void tester( const int * labyrinth, size_t n, const char * instructions, char * result);.

Test( Example_Tests, should_pass_all_the_tests_provided) {

#define N 7.

const int labyrinth[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};.

// labyrinth is passed in as a 1-D int range with length n.
// instructions are passed in as a null-terninated string.
// do not assign memory, rather return a string actual.

{const char * instructions="NNNNNEEEEE"; tester( labyrinth, N, instructions, "Complete");}
{const char * instructions="NNNNNEESSEENNE"; tester( labyrinth, N, instructions, "Complete");}
{const char * instructions="NNNNNEEEEEWW"; tester( labyrinth, N, instructions, "Complete");}
{const char * instructions="NEEEE"; tester( labyrinth, N, instructions, "Lost");}
{const char * instructions="NNNWW"; tester( labyrinth, N, instructions, "Dead");}
{const char * instructions="NNNNNEESSSSSS"; tester( labyrinth, N, instructions, "Dead");}

}
``````