Created
May 12, 2013 13:34
-
-
Save xtoss/5563570 to your computer and use it in GitHub Desktop.
Python solution for google code jam 2013 Round 1C - Problem B - Pogo: http://code.google.com/codejam/contest/2437488/dashboard#s=p1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Used to solve google code jam 2013 Round 1C - Problem B - Pogo | |
Owner: Jamie Xu - [email protected] | |
""" | |
import math | |
def Pogo(): | |
in_f = open('A-large.in', 'r') | |
out_f = open('output.txt', 'w') | |
num_of_case = int(in_f.readline().rstrip('\n')) | |
for i in range(1, num_of_case+1): | |
solve_case(in_f, out_f, i) | |
def solve_case(in_f, out_f, case_index): | |
print "start handling case #{}".format(case_index) | |
case_info = in_f.readline().rstrip('\n').split(" ") | |
x, y = map(int, case_info) | |
x_agg_steps, x_pas_steps, x_current_length = get_steps(math.fabs(x), 1) | |
y_agg_steps, y_pas_steps, y_current_length = get_steps(math.fabs(y), x_current_length + 1) | |
str = "E" * x_agg_steps + "N" * y_agg_steps + "WE" * x_pas_steps + "SN" * y_pas_steps | |
# handle the negative case | |
if x < 0: | |
str = str.replace("E", "T").replace("W", "E").replace("T", "W") | |
if y < 0: | |
str = str.replace("S", "T").replace("N", "S").replace("T", "N") | |
out_f.write("Case #{}: {}\n".format(case_index, str)) | |
def get_steps(goal, step_length=1): | |
""" | |
agg_steps goes toward the goal directly, with one more units than last step. | |
pas_steps goes toward the goal by one unit at a time by using two steps as a combination, for example "SN" goes to N one unit. | |
""" | |
init_length = step_length | |
init_goal = goal | |
if step_length == 1: | |
agg_steps = int(math.sqrt(2*goal+0.25) - 0.5) | |
pas_steps = int(goal - ((1+agg_steps)*agg_steps)/2) | |
current_length = agg_steps | |
else: | |
agg_steps = 0 | |
while goal >= step_length: | |
agg_steps = agg_steps + 1 | |
goal = goal - step_length | |
step_length = step_length + 1 | |
pas_steps = int(init_goal - (init_length + step_length - 1) * agg_steps / 2) | |
current_length = step_length - 1 | |
return (agg_steps, pas_steps, current_length) | |
if __name__ == '__main__': | |
Pogo() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment