Here is the simple way with PHP to traverse two dimensional arrays and get current moving states :).
function maze_runner($maze, $directions): string { $m = 0; $n = 0; $startM = 0; $startN = 0; foreach ($maze as $mazeRow) { foreach ($mazeRow as $mazeNumber) { if ($mazeNumber === 2) { $startM = $m; $startN = $n; break; } $n += 1; } $n = 0; $m += 1; } $currentValue = null; foreach ($directions as $direction) { if ($direction === 'N') { $startM -= 1; if ($startM < 0 || $startM >= count($maze[0])) { return 'Dead'; } } if ($direction === 'S') { $startM += 1; if ($startM < 0 || $startM >= count($maze[0])) { return 'Dead'; } } if ($direction === 'E') { $startN += 1; if ($startN < 0 || $startN >= count($maze[0])) { return 'Dead'; } } if ($direction === 'W') { $startN -= 1; if ($startN < 0 || $startN >= count($maze[0])) { return 'Dead'; } } $currentValue = $maze[$startM][$startN]; if ($currentValue === 3) { return 'Finish'; } if ($currentValue === 1) { return 'Dead'; } } return 'Lost'; }
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Here is the simple way with PHP to traverse two dimensional arrays and get current moving states :).