问题描述:
已知有一组传感器,确定传感器的位置,覆盖半径,问他们是否能够将一个已知的房间覆盖完全
参数:*
表示房间的信息的[w,h]
一组传感器的参数[x,y,r]
其中利用x,y表示传感器的位置,r表示传感器的覆盖范围
功能:
当房间被传感器所完全覆盖时返回True,否则返回False
代码如下:
#-*-coding: utf-8-*-
import math
def is_covered(room, sensors):
# your code here
Ss=0
Sm=0
for i in sensors:
Ss=i[2]**2*round(math.pi,2)+Ss
Sm=room[0]*room[1]
if Sm>Ss:
return False
else:
for i in range(room[0]):
for j in range(room[1]):
f=0
for k in sensors:
if (i-k[0])**2+(j-k[1])**2<=k[2]**2:
f=1
break
if f==0:
return False
return True
if __name__ == '__main__':
print("Example:")
print(is_covered([200, 150], [[100, 75, 130]]))
# These "asserts" are used for self-checking and not for an auto-testing
assert is_covered([200, 150], [[100, 75, 130]]) == True
assert is_covered([200, 150], [[50, 75, 100], [150, 75, 100]]) == True
assert is_covered([200, 150], [[50, 75, 100], [150, 25, 50], [150, 125, 50]]) == False
assert is_covered([200, 150], [[100, 75, 100], [0, 40, 60], [0, 110, 60], [200, 40, 60], [200, 110, 60]]) == True
assert is_covered([200, 150], [[100, 75, 100], [0, 40, 50], [0, 110, 50], [200, 40, 50], [200, 110, 50]]) == False
assert is_covered([200, 150], [[100, 75, 110], [105, 75, 110]]) == False
assert is_covered([200, 150], [[100, 75, 110], [105, 75, 20]]) == False
assert is_covered([3, 1], [[1, 0, 2], [2, 1, 2]]) == True
assert is_covered([30, 10], [[0, 10, 10], [10, 0, 10], [20, 10, 10], [30, 0, 10]]) == True
assert is_covered([30, 10], [[0, 10, 8], [10, 0, 7], [20, 10, 9], [30, 0, 10]]) == False
print("Coding complete? Click 'Check' to earn cool rewards!")