The problem
Introduction
Welcome Adventurer. Your purpose is to navigate the maze and attain the end level with out touching any partitions. Doing so will kill you immediately!
Activity
You'll be given a 2D array of the maze and an array of instructions. Your activity is to comply with the instructions given. In case you attain the tip level earlier than all of your strikes have gone, it is best to return End. In case you hit any partitions or go exterior the maze border, it is best to return Lifeless. If you end up nonetheless within the maze after utilizing all of the strikes, it is best to 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 dimension and content material will alter from take a look at to check. 2. The beginning and end positions will change for the ultimate checks. 3. The instructions array will at all times be in higher case and will likely be within the format of N = North, E = East, W = West and S = South. 4. In case you attain the tip level earlier than all of your strikes have gone, it is best to return End. 5. In case you hit any partitions or go exterior the maze border, it is best to return Lifeless. 6. If you end up nonetheless within the maze after utilizing all of the strikes, it is best to return Misplaced.
The answer in C
Possibility 1:
#embody <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:
#embody <stddef.h>
#embody <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:
#embody <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) DEAD(y, n)
return "Misplaced";
}
Take a look at circumstances to validate our resolution
#embody <criterion/criterion.h>
void tester(const int *maze, size_t n, const char *instructions, char *final result);
Take a look at(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 a substitute 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"); }
}