|
|
back to boardDiscussion of Problem 1020. RopeTest #5 WA, Python The following is my code. First come a special case where n is 1. If n is not 1, more work should be done. First, the nails are sorted based on their x-coordination. The leftmost nail is accessed, and compared to the second leftmost nail. With some boundary case, all the nails can be sorted along the clockwise or counter-clockwise direction. Finally, I measure the distances between adjacent nails with pythagorean theorm. Together with 2*pi*r, which accounts for the round edges, should be the answer. What exactly does the code fail Test #5? import math def nail(n, r): return 2 * math.pi * r def pyth(a, b): return math.sqrt(a**2 + b**2) def perimeter(coord): ans = 0.0 for i in range(len(coord)): ans += pyth((coord[i][0] - coord[i-1][0]), (coord[i][1] - coord[i-1][1])) return ans if __name__ == "__main__": n, r = raw_input().split() n = int(n) r = float(r) coord = [] for each in range(n): i, j = raw_input().split() coord += [(float(i), float(j))] if n == 1: print "%.2f" % (2 * math.pi * r) else: coord.sort() directed_coord = [] directed_coord += [coord.pop(0)] if coord[0][1] > directed_coord[0][1]: clockwise = True directed_coord += [coord.pop(0)] else: clockwise = False directed_coord += [coord.pop(0)] if clockwise == True: for i in coord[:]: if i[1] > directed_coord[-1][1]: coord.remove(i) directed_coord += [i] for i in coord[:]: if i[1] >= coord[-1][1]: coord.remove(i) directed_coord += [i] coord.reverse() directed_coord += coord else: for i in coord[:]: if i[1] < directed_coord[-1][1]: coord.remove(i) directed_coord += [i] for i in coord[:]: if i[1] <= coord[-1][1]: coord.remove(i) directed_coord += [i] coord.reverse() directed_coord += coord ans = nail(n, r) + perimeter(directed_coord) print "%.2f" % ans |
|
|