-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCvZ.php
104 lines (90 loc) · 2.1 KB
/
CvZ.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php
/**
* Save humans, destroy zombies!
**/
/**
* This file is for the project "Code vs Zombies" on Codingame (https://www.codingame.com/multiplayer/optimization/code-vs-zombies)
**/
class pos {
public $x;
public $y;
}
$player = new pos ();
function get_distance($pos1, $pos2) {
$dist1 = ($pos1->x - $pos2->x) * ($pos1->x - $pos2->x);
$dist2 = ($pos1->y - $pos2->y) * ($pos1->y - $pos2->y);
$dist = sqrt($dist1 + $dist2);
return ($dist);
}
function can_reach($human, $distz, $player) {
$distp = get_distance($human, $player);
if ($distz / 400 < ($distp - 3000) / 1000)
return (false);
return (true);
}
function find_closer($humans, $zombies, $player) {
$minAll = 99999;
$dest = new pos ();
$dest = $humans[0];
foreach ($humans as $idh => $human) {
$min = 99999;
foreach ($zombies as $idz => $zombie) {
$dist = get_distance($human, $zombie);
if ($dist <= $min) {
$min = $dist;
}
}
if (can_reach($human, $min, $player) && $minAll > $min) {
$minAll = $min;
$dest = $human;
}
}
return ($dest);
}
// game loop
while (TRUE)
{
unset($humans);
unset($zombies);
$humans = [];
$zombies = [];
fscanf(STDIN, "%d %d",
$player->x,
$player->y
);
fscanf(STDIN, "%d",
$humanCount
);
for ($i = 0; $i < $humanCount; $i++)
{
fscanf(STDIN, "%d %d %d",
$humanId,
$x,
$y
);
$humans[$humanId] = new pos ();
$humans[$humanId]->x = $x;
$humans[$humanId]->y = $y;
}
fscanf(STDIN, "%d",
$zombieCount
);
for ($i = 0; $i < $zombieCount; $i++)
{
fscanf(STDIN, "%d %d %d %d %d",
$zombieId,
$zombieX,
$zombieY,
$nextX,
$nextY
);
$zombies[$zombieId] = new pos ();
$zombies[$zombieId]->x = $nextX;
$zombies[$zombieId]->y = $nextY;
}
$dest = find_closer($humans, $zombies, $player);
// Write an action using echo(). DON'T FORGET THE TRAILING \n
// To debug (equivalent to var_dump): error_log(var_export($var, true));
echo("$dest->x $dest->y\n"); // Your destination coordinates
}
?>