我的程式原本是AC的,不過幾天後重上傳,卻變NA了,重複上傳多次又變回AC,請問是複雜度過高嗎,如果是,可以提供點想法嗎。謝謝
n = int(input())
lis = []
for i in range(n):
lis.append([int(x) for x in input().split()])
lis.append([0,0,0])
lx = sorted(lis,key = lambda x:(x[0],x[1]))
ly = sorted(lis,key = lambda x:(x[1],x[0]))
sx = 0 #設定起始點座標
sy = 0 #設定起始點座標
direct = 1 #定義方向,1為右,2為上,3為左,4為下,預設右
count = 0 #碰到鏡子次數
def search(x,y,d):
num = 0
if d == 1 or d == 3:
s = 0
e = len(ly) - 1
while s <= e:
mid = (s+e)//2
if ly[mid][0] == x and ly[mid][1] == y:
num = mid
break
if ly[mid][1] < y or (ly[mid][1] == y and ly[mid][0] < x):
s = mid + 1
else:
e = mid - 1
if ly[num][0] != x:
return False
else:
s = 0
e = len(ly) - 1
while s <= e:
mid = (s+e)//2
if lx[mid][0] == x and lx[mid][1] == y:
num = mid
break
if lx[mid][0] < x or (lx[mid][0] == x and lx[mid][1] < y):
s = mid + 1
else:
e = mid - 1
if lx[num][1] != y:
return False
if d==1:
if ly[num+1][1] == y:
return ly[num+1]
else:
return False
elif d==2:
if lx[num+1][0] == x:
return lx[num+1]
else:
return False
if d==3:
if ly[num-1][1] == y:
return ly[num-1]
else:
return False
else:
if lx[num-1][0] == x:
return lx[num-1]
else:
return False
while True:
mirror = search(sx,sy,direct)
if mirror == False: break
sx = mirror[0] #刷新初始值(下一個鏡子就是新的起始點)
sy = mirror[1]
if count == 0:
lx.remove([0,0,0])
ly.remove([0,0,0])
if mirror[2] == 0: #判斷光反射後方向
if direct==1:
direct = 2
elif direct==2:
direct =1
elif direct==3:
direct=4
else:
direct =3
if mirror[2] == 1:
if direct==1:
direct = 4
elif direct==2:
direct =3
elif direct==3:
direct=2
else:
direct = 1
count+= 1
print(count)