verliber

Codingame CodevsZombie PHP

Dec 2nd, 2020
767
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. /**
  3.  * Save humans, destroy zombies!
  4.  **/
  5.  
  6.  
  7. // game loop
  8. while (TRUE)
  9. {
  10.     unset($human);
  11.     unset($zombie);
  12.     $human;
  13.     $zombie;
  14.     fscanf(STDIN, "%d %d", $x, $y);
  15.     fscanf(STDIN, "%d", $humanCount);
  16.     for ($i = 0; $i < $humanCount; $i++)
  17.     {
  18.         fscanf(STDIN, "%d %d %d", $humanId, $humanX, $humanY);
  19.         $human[$i] = [
  20.             'id'    => $humanId,
  21.             'x'     => $humanX,
  22.             'y'     => $humanY
  23.         ];
  24.     }
  25.     fscanf(STDIN, "%d", $zombieCount);
  26.     for ($i = 0; $i < $zombieCount; $i++)
  27.     {
  28.         fscanf(STDIN, "%d %d %d %d %d", $zombieId, $zombieX, $zombieY, $zombieXNext, $zombieYNext);
  29.         $zombie[$zombieId] = [
  30.             'id'    => $zombieId,
  31.             'x'     => $zombieX,
  32.             'y'     => $zombieY,
  33.             'xn'    => $zombieXNext,
  34.             'yn'    => $zombieYNext
  35.         ];
  36.     }
  37.  
  38.     // Write an action using echo(). DON'T FORGET THE TRAILING \n
  39.     // To debug: error_log(var_export($var, true)); (equivalent to var_dump)
  40.     $me = ['x' => $x, 'y' => $y];
  41.     $dst = closest($human, $zombie, $me);
  42.     $targetId = $dst[array_key_first($dst)]['zid'];
  43.     error_log(var_export($dst, true));
  44.     echo(round($zombie[$targetId]['x'])." ".round($zombie[$targetId]['y'])."\n"); // Your destination coordinates
  45.     unset($targetId);
  46.    
  47.     //}
  48. }
  49.  
  50. function closest($human, $zombie, $me)
  51. {
  52.     $i = 0;
  53.     foreach($zombie as $z){
  54.         foreach($human as $h){
  55.             if((getDistance($me['x'], $me['y'], $h['x'], $h['y'])) -getDistance($z['x'], $z['y'], $h['x'], $h['y']) < 100 && (getDistance($me['x'], $me['y'], $h['x'], $h['y'])) -getDistance($z['x'], $z['y'], $h['x'], $h['y']) < 50 || getDistance($z['x'], $z['y'], $h['x'], $h['y']) > 40){
  56.             $dst[$i] = [
  57.                 'hid' => $h['id'],
  58.                 'zid' => $z['id'],
  59.                 'medst' => getDistance($me['x'], $me['y'], $h['x'], $h['y']),
  60.                 'dst' => getDistance($z['x'], $z['y'], $h['x'], $h['y'])];
  61.             $i++;
  62.             }
  63.         }
  64.     }
  65.     usort($dst, function($a, $b) {
  66.     return $a['dst'] <=> $b['dst'];
  67. });
  68.     return $dst;
  69. }
  70.  
  71.  
  72. function getDistance($lat1, $lon1, $lat2, $lon2) {
  73.     if($lat1 > $lat2){
  74.     $xC = $lat1-$lat2;
  75.     }elseif($lat1 == $lat2){
  76.     $xC=0;
  77.     }else{
  78.     $xC = $lat2-$lat1;  
  79.     }
  80.     if($lon1 > $lon2){
  81.     $yC = $lon1-$lon2;
  82.     }elseif($lon1 == $lon2){
  83.     $yC=0;
  84.     }else{
  85.     $yC = $lon2-$lon1;  
  86.     }
  87.  
  88. return sqrt(($xC^2)+($yC^2));
  89. }
  90. ?>
RAW Paste Data