Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $width = $_POST['width'] + 1;
- $height = $_POST['height'] + 1;
- $mapVel = $_POST['v'];
- $mapHeight = $_POST['h'];
- $mapRes = [];
- $mapPath = [];
- $mapRes[$height - 1][$width - 1] = 0;
- for($i = $width - 2; $i >= 0; --$i) {
- $mapRes[$height - 1][$i] = $mapRes[$height - 1][$i + 1] + $mapVel[$height - 1][$i];
- $mapPath[$height - 1][$i] = 'R';
- }
- for($i = $height - 2; $i >= 0; --$i) {
- $mapRes[$i][$width - 1] = $mapRes[$i + 1][$width - 1] + $mapHeight[$i][$width - 1];
- $mapPath[$i][$width - 1] = 'T';
- }
- for($i = $height - 2; $i >= 0; --$i) {
- for($j = $width - 2; $j >=0; --$j) {
- if($mapRes[$i + 1][$j] + $mapHeight[$i][$j] > $mapRes[$i][$j + 1] + $mapVel[$i][$j]) {
- $mapRes[$i][$j] = $mapRes[$i][$j + 1] + $mapVel[$i][$j];
- $mapPath[$i][$j] = 'R';
- }
- else {
- $mapRes[$i][$j] = $mapRes[$i + 1][$j] + $mapHeight[$i][$j];
- $mapPath[$i][$j] = 'T';
- }
- }
- }
- $result = [
- 'distance' => $mapRes,
- 'path' => $mapPath,
- ];
- echo json_encode($result);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement