Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Save humans, destroy zombies!
- **/
- // game loop
- while (TRUE)
- {
- unset($human);
- unset($zombie);
- $human;
- $zombie;
- fscanf(STDIN, "%d %d", $x, $y);
- fscanf(STDIN, "%d", $humanCount);
- for ($i = 0; $i < $humanCount; $i++)
- {
- fscanf(STDIN, "%d %d %d", $humanId, $humanX, $humanY);
- $human[$i] = [
- 'id' => $humanId,
- 'x' => $humanX,
- 'y' => $humanY
- ];
- }
- fscanf(STDIN, "%d", $zombieCount);
- for ($i = 0; $i < $zombieCount; $i++)
- {
- fscanf(STDIN, "%d %d %d %d %d", $zombieId, $zombieX, $zombieY, $zombieXNext, $zombieYNext);
- $zombie[$zombieId] = [
- 'id' => $zombieId,
- 'x' => $zombieX,
- 'y' => $zombieY,
- 'xn' => $zombieXNext,
- 'yn' => $zombieYNext
- ];
- }
- // Write an action using echo(). DON'T FORGET THE TRAILING \n
- // To debug: error_log(var_export($var, true)); (equivalent to var_dump)
- $me = ['x' => $x, 'y' => $y];
- $dst = closest($human, $zombie, $me);
- $targetId = $dst[array_key_first($dst)]['zid'];
- error_log(var_export($dst, true));
- echo(round($zombie[$targetId]['x'])." ".round($zombie[$targetId]['y'])."\n"); // Your destination coordinates
- unset($targetId);
- //}
- }
- function closest($human, $zombie, $me)
- {
- $i = 0;
- foreach($zombie as $z){
- foreach($human as $h){
- 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){
- $dst[$i] = [
- 'hid' => $h['id'],
- 'zid' => $z['id'],
- 'medst' => getDistance($me['x'], $me['y'], $h['x'], $h['y']),
- 'dst' => getDistance($z['x'], $z['y'], $h['x'], $h['y'])];
- $i++;
- }
- }
- }
- usort($dst, function($a, $b) {
- return $a['dst'] <=> $b['dst'];
- });
- return $dst;
- }
- function getDistance($lat1, $lon1, $lat2, $lon2) {
- if($lat1 > $lat2){
- $xC = $lat1-$lat2;
- }elseif($lat1 == $lat2){
- $xC=0;
- }else{
- $xC = $lat2-$lat1;
- }
- if($lon1 > $lon2){
- $yC = $lon1-$lon2;
- }elseif($lon1 == $lon2){
- $yC=0;
- }else{
- $yC = $lon2-$lon1;
- }
- return sqrt(($xC^2)+($yC^2));
- }
- ?>
RAW Paste Data