fork download
  1.  
  2. def solve(h, w):
  3. top = 1
  4. left = 0
  5. bottom = h + 1
  6. right = w + 1
  7. x = 0
  8. y = 1
  9. g = 0
  10. while True:
  11. if g == 0:
  12. if x == right - 1:
  13. return y, x
  14. x = right - 1
  15. right -= 1
  16. elif g == 1:
  17. if y == bottom - 1:
  18. return y, x
  19. y = bottom - 1
  20. bottom -= 1
  21. elif g == 2:
  22. if x == left + 1:
  23. return y, x
  24. x = left + 1
  25. left += 1
  26. elif g == 3:
  27. if y == top + 1:
  28. return y, x
  29. y = top + 1
  30. top += 1
  31. g = (g + 1) % 4
  32.  
  33. def test():
  34. testcases = [[3, 3], [10, 7], [1000000, 333333], [3, 1]]
  35. for tc in testcases:
  36. i, j = solve(*tc)
  37. print("problem(h,w):", *tc, "answer(i,j):", i, j)
  38.  
  39. test()
Success #stdin #stdout 0.14s 28384KB
stdin
Standard input is empty
stdout
problem(h,w): 3 3 answer(i,j): 2 2
problem(h,w): 10 7 answer(i,j): 7 4
problem(h,w): 1000000 333333 answer(i,j): 833334 166667
problem(h,w): 3 1 answer(i,j): 3 1