Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.99 KB | None | 0 0
  1. <?php
  2.  
  3. $width = $_POST['width'] + 1;
  4. $height = $_POST['height'] + 1;
  5.  
  6. $mapVel = $_POST['v'];
  7. $mapHeight = $_POST['h'];
  8.  
  9. $mapRes = [];
  10. $mapPath = [];
  11.  
  12. $mapRes[$height - 1][$width - 1] = 0;
  13.  
  14. for($i = $width - 2; $i >= 0; --$i) {
  15.     $mapRes[$height - 1][$i] = $mapRes[$height - 1][$i + 1] + $mapVel[$height - 1][$i];
  16.     $mapPath[$height - 1][$i] = 'R';
  17. }
  18.  
  19. for($i = $height - 2; $i >= 0; --$i) {
  20.     $mapRes[$i][$width - 1] = $mapRes[$i + 1][$width - 1] + $mapHeight[$i][$width - 1];
  21.     $mapPath[$i][$width - 1] = 'T';
  22. }
  23.  
  24. for($i = $height - 2; $i >= 0; --$i) {
  25.     for($j = $width - 2; $j >=0; --$j) {
  26.         if($mapRes[$i + 1][$j] + $mapHeight[$i][$j] > $mapRes[$i][$j + 1] + $mapVel[$i][$j]) {
  27.             $mapRes[$i][$j] = $mapRes[$i][$j + 1] + $mapVel[$i][$j];
  28.             $mapPath[$i][$j] = 'R';
  29.         }
  30.         else {
  31.             $mapRes[$i][$j] = $mapRes[$i + 1][$j] + $mapHeight[$i][$j];
  32.             $mapPath[$i][$j] = 'T';
  33.         }
  34.     }
  35. }
  36.  
  37. $result = [
  38.     'distance' => $mapRes,
  39.     'path' => $mapPath,
  40. ];
  41.  
  42. echo json_encode($result);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement