#!/usr/bin/perl
# your code goes here

# "Read line from STDIN, split on whitespace, return list". 
# We do it several times, therefore make it a sub:

sub i{<>=~/\S+/g}

# @b is height-map of city-scape ("array of arrays", 
# i.e. array of array references). 
# Also, store grid-lines range in @r array for later re-use:

@b=map[i],@r=0..<>-1;

# Next, things are getting somewhat compressed. 
# Let's take them apart and start from the very end. 

print.1<=>(map{
    @a[1,0,2,4,3]=@a;
    @b=map{$i=$_;[map$b[$_][$i],@r]}@r;
    grep$a[0]
        &&($k=(($x=$_)-$a[3])/$a[0])**2<=$k
        &&pop[sort map@{$b[$_]}[$x-!!$x,$x],
                   ($_=$a[4]+$k*$a[1]),$_-/^\d+$/]
          >=$a[5]+$k*$a[2]
    ,@R=@r
}@a=map$_-shift@v,i,@u=@v=@$_),$/for([i])x<>

# Read player coordinates, make anonymous array, duplicate reference 
# as many times as there are enemies, return a list:

# ([i])x<>

# For each element of this list, compare 0.1 with result of some operation 
# (on which more later), print result of comparison 
# (can be "-1" or "1" here) followed by a newline:

# print.1<=>(...),$/for([i])x<>

# That "..." operation is a "map" in scalar (because of comparison) 
# context, i.e. number of elements in list returned by "map": 
# "1" is printed for empty list, "-1" for non-empty.

# This "map" arguments are CODE-BLOCK and LIST. 
# LIST is result of another "map":

# @a=map$_-shift@v,i,@u=@v=@$_

# @$_ is dereferencing of "for" loop variable and that's player coordinates. 
# "i" returns current enemy coordinates. 
# So, what _this_ "map" returns and what's stored (for later re-use) 
# in @a array and what's the LIST for "main" map - they are six numbers:

# Xe - Xp
# Ye - Yp
# Ze - Zp
# Xp
# Yp
# Zp

# i.e. they are differences between enemy and player respective coordinates,
# followed by player coordinates.

# The CODE-BLOCK mentioned above:

# @a[1,0,2,4,3]=@a;
# @b=map{$i=$_;[map$b[$_][$i],@r]}@r;
# grep$a[0]
#     &&($k=(($x=$_)-$a[3])/$a[0])**2<=$k
#     &&pop[sort map@{$b[$_]}[$x-!!$x,$x],
#                ($_=$a[4]+$k*$a[1]),$_-/^\d+$/]
#       >=$a[5]+$k*$a[2]
# ,@R=@r

# As you see, the topic variable ($_) of enclosing "map" is _not_ used. 
# The whole point was to build @a array, execute CODE-BLOCK two 
# (or _any even number_ of) times (6, here), and return list which, 
# in turn, is composed of results of "grep" executions in above BLOCK.

# Two first lines of this BLOCK are to swap "x y" coordinates. 

# @a[1,0,2,4,3]=@a;
# @b=map{$i=$_;[map$b[$_][$i],@r]}@r;

# With this little trick we can use the same "grep" part to check both 
# "x" and "y" grid-lines. 
# Because BLOCK is executed even number of times, coordinates will be 
# restored to original state by the time we get to the next enemy.

# Now to the "grep" part. 
# It evaluates some expression for each element of @r array (grid-lines)
# and returns non-empty list if "expression" is TRUE for any grid-line. 
# This, in turn, makes "map" return non-empty list i.e. "-1" is printed 
# for a given enemy. 

# It means that the "expression" checks if, for each integer "x" 
# in range, the PE ("player-enemy") line is above or below city-scape.

# There are 3 boolean sub-expressions, multiplied with logical "and"s.

# First, 

# $a[0]

# i.e. only make further checks if Xe != Xp to avoid "division by zero", 
# later. The case of Xe == Xp will be checked when we swap coordinates.

# Second,

# ($k=(($x=$_)-$a[3])/$a[0])**2<=$k

# This sub-expression, apart from defining $x and $k variables for later 
# re-use, actually checks if current grid-line is in Xe .. Xp range 
# (or Xp .. Xe) - i.e. only go to the 3d sub-expression if it is.

# Third,

# pop[sort map@{$b[$_]}[$x-!!$x,$x],
#          ($_=$a[4]+$k*$a[1]),$_-/^\d+$/]
# >=$a[5]+$k*$a[2]

# As you see, it finds a maximum value in some LIST 

# pop[sort LIST]

# and compares it to some value. Alphabetical sorting is OK in our case.

# The "value" is none other than "z" coordinate on PE line:

# $a[5]+$k*$a[2]

# And that LIST is a list of up to 4 "buildings" heights, which share 
# the same (x,y) point. BTW, "y" is 

# $_=$a[4]+$k*$a[1]

# These comments are getting a bit long. Understanding sub-expressions 
# "2" and "3" mentioned above, as well as "swapping" X <-> Y mechanics 
# may require some effort, though not very much. 

# I hope it's clearer now how it all works. All this complexity was only
# introduced for brevity.
