fork download
  1. from collections import deque
  2.  
  3. def min_turns(h, d):
  4. start = (0, h, 0)
  5. q = deque([(0, h, 0, 0)])
  6. visited = set([start])
  7.  
  8. while q:
  9. pos, health, cons, turns = q.popleft()
  10.  
  11. if pos >= d:
  12. return turns
  13. new_state = (pos, health+1, 0)
  14. if new_state not in visited:
  15. visited.add(new_state)
  16. q.append((pos, health+1, 0, turns+1))
  17.  
  18. cost = cons + 1
  19. if health > cost:
  20. new_state = (pos+1, health-cost, cons+1)
  21. if new_state not in visited:
  22. visited.add(new_state)
  23. q.append((pos+1, health-cost, cons+1, turns+1))
  24.  
  25.  
  26. t = int(input())
  27. for _ in range(t):
  28. h, d = map(int, input().split())
  29. print(min_turns(h, d))
  30.  
Success #stdin #stdout 0.08s 14156KB
stdin
5
3 2
1 1
5 3
2 4
10 7
stdout
3
2
4
7
10