Python 2.7版- 429 420 308 308个字符
我认为这个挑战更多是数学问题,而不是代码高尔夫球问题,因此,如果我错过了一些明显的优化,对我来说不要太苛刻。无论如何,这是代码:
b=lambda:raw_input().split()
m=map
d=range(input())
h=[m(int,b())for _ in d]
x,y,z=m(float,b())
for e,f,g in[m(float,b())for _ in[1]*input()]:o=lambda x,y,u,v,i,j:i<=x+u/v*(j+1-y)<=i+1<[]>z+(g-z)/v*(j+1-y)<=max(h[i][j:j+2])if v else 0;print 1-2*any(o(x,y,e-x,f-y,j,i)+o(y,x,f-y,e-x,i,j)for j in d for i in d)
这应该适用于边缘和角落情况(意外的双关语),并且非常牢固。提供的示例的输出:
-1
1
1
这是一个“简短”的解释:
fast_read = lambda : raw_input().split() # define a helper
# m = map another helper
grid_range = range(input())
houses = [map(int, fast_read()) for _ in grid_range]
# 'map(int,...)' is a shorter version of '[int(a) for a in ...]'
pos_x,pos_y,pos_z = map(float, fast_read()) # read the player position
# the following loops through all enemy coordinates
for ene_x, ene_y, ene_z in [map(float,fast_read()) for _ in[1]*input()]:
vec_z = ene_z - pos_z
# is_hit macro uses vector math to detemine whether we hit a specific wall
# wallhit -> 1
# no wallhit -> 0
is_hit = lambda pos_x, pos_y, vec_x, vec_y, co_x, co_y:\
(co_x <= pos_x + vec_x/vec_y * (co_y + 1 - pos_y) <= co_x + 1 # check if hit_x is good
< [] > # an effective and
pos_z + (ene_z - pos_z)/vec_y * (co_y + 1 - pos_y) <= max(houses[co_x][co_y:co_y + 2]) # check if hit_z is good
if vec_y else 0) # if vec_y is 0 we can't hit the wall parallel to y
print (.5 - # can hit -> 0.5 - 0 = 0.5, hit -> 0.5 - 1 = -0.5
any( # if we hit any wall
# we swap x and y-coordinate because we read them "incorrect"
is_hit(pos_x, pos_y, ene_x-pos_x, ene_y-pos_y, cur_y, cur_x) # check for hit in x-direction
+ # effective 'or'
is_hit(pos_y, pos_x, ene_y-pos_y, ene_x-pos_x, cur_x, cur_y) # check for hit in y-direction
for cur_y in grid_range # loop y
for cur_x in grid_range)) # loop x
我猜这充满了缺陷。顺便说一句,我在嵌套时保存了字符(第一级是一个空格,第二个是一个标签,然后是一个标签和一个空格...)。我希望所有这些答案都可以指出这样做的方法。