$j_end) { list($i, $j) = moveL($i, $j); $moves[] = 'L'; } } else if ($j == $j_end) { list($i, $j) = moveLR($i, $j); $moves[] = 'LR'; } else if ($j < $j_end) { if ($i < $i_end) { list($i, $j) = moveLR($i, $j); $moves[] = 'LR'; } else if ($i > $i_end) { list($i, $j) = moveUR($i, $j); $moves[] = 'UR'; } } else if ($j > $j_end) { if ($i < $i_end) { list($i, $j) = moveLL($i, $j); $moves[] = 'LL'; } else if ($i > $i_end) { list($i, $j) = moveUL($i, $j); $moves[] = 'UL'; } } $currentPosition = $i . ' ' . $j; if (in_array($currentPosition, $positions)) { throw new Exception; } $positions[] = $currentPosition; } echo count($moves) . "\n"; echo implode(' ', $moves); } catch (Exception $exception) { echo 'Impossible'; } } function isOut($i, $j, $n) { return ( $i < 0 || $i > ($n - 1) || $j < 0 || $j > ($n - 1) ); } function moveUR($i, $j) { return [$i - 2, $j + 1]; } function moveR($i, $j) { return [$i, $j + 2]; } function moveLR($i, $j) { return [$i + 2, $j + 1]; } function moveLL($i, $j) { return [$i + 2, $j - 1]; } function moveL($i, $j) { return [$i, $j - 2]; } function moveUL($i, $j) { return [$i - 2, $j - 1]; } fscanf($handle, '%i', $n); fscanf($handle, '%i %i %i %i', $i_start, $j_start, $i_end, $j_end); printShortestPath($n, $i_start, $j_start, $i_end, $j_end); ?>