-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathphp.php
44 lines (39 loc) · 1.6 KB
/
php.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
<?php
/**
* Auto-generated code below aims at helping you parse
* the standard input according to the problem statement.
**/
// $nbFloors: number of floors
// $width: width of the area
// $nbRounds: maximum number of rounds
// $exitFloor: floor on which the exit is found
// $exitPos: position of the exit on its floor
// $nbTotalClones: number of generated clones
// $nbAdditionalElevators: ignore (always zero)
// $nbElevators: number of elevators
fscanf(STDIN, "%d %d %d %d %d %d %d %d", $nbFloors, $width, $nbRounds, $exitFloor, $exitPos, $nbTotalClones, $nbAdditionalElevators, $nbElevators);
$elevators = [];
for ($i = 0; $i < $nbElevators; $i++)
{
// $elevatorFloor: floor on which this elevator is found
// $elevatorPos: position of the elevator on its floor
fscanf(STDIN, "%d %d", $elevatorFloor, $elevatorPos);
$elevators[$elevatorFloor] = $elevatorPos;
}
// game loop
while (TRUE)
{
// $cloneFloor: floor of the leading clone
// $clonePos: position of the leading clone on its floor
// $direction: direction of the leading clone: LEFT or RIGHT
fscanf(STDIN, "%d %d %s", $cloneFloor, $clonePos, $direction);
$isOnExitFloor = $cloneFloor === $exitFloor;
$sameFloorElevatorPos = isset($elevators[$cloneFloor]) ? $elevators[$cloneFloor] : -1;
$posToTarget = $isOnExitFloor ? $exitPos : $sameFloorElevatorPos;
$targetDirection = $clonePos < $posToTarget ? "RIGHT" : "LEFT";
// action: WAIT or BLOCK
if (($clonePos !== $posToTarget && $targetDirection !== $direction) || $clonePos === 0 || clonePos === width - 1)
echo("BLOCK\n");
else echo("WAIT\n");
}
?>