【发布时间】:2021-07-06 06:30:47
【问题描述】:
我正在尝试解决问题:在 kattis 中识别地图块。
https://open.kattis.com/problems/maptiles2
我得到了正确的答案,直到第 6 次测试我一直收到超出时间限制的错误。
这是我的代码,我不确定有什么问题。我可以做些什么来优化它,让它运行得更快?
#! /usr/bin/env python3
import sys
s=sys.stdin.read().splitlines()
quadkey=s[0]
zoom_level=len(quadkey)
#Lists of x and y cordinates
x_cords=[]
y_cords=[]
for i in range(2**zoom_level):
x_cords.append(i)
y_cords.append(i)
#Try to loop through the quadkey and locate the cordinates
for i in range(len(quadkey)):
half=int(len(x_cords)/2)
if quadkey[i]=='0':
x_cords=x_cords[:half]
y_cords=y_cords[:half]
elif quadkey[i]=='1':
x_cords=x_cords[half:]
y_cords=y_cords[:half]
elif quadkey[i]=='2':
x_cords=x_cords[:half]
y_cords=y_cords[half:]
elif quadkey[i]=='3':
x_cords=x_cords[half:]
y_cords=y_cords[half:]
x_cord=x_cords[0]
y_cord=y_cords[0]
print(zoom_level, x_cord, y_cord)
【问题讨论】:
标签: python algorithm performance kattis