fork(1) download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10.  
  11. public static void main(String[] args)
  12. {
  13. System.out.println(minMoves(0, 1, 0, 11));
  14. }
  15.  
  16. public static int minMoves(int pos, int steps, int move, int N)
  17. {
  18. if (Math.abs(pos) > N)
  19. return Integer.MAX_VALUE;
  20. if (Math.abs(pos) == N)
  21. return move;
  22. int j = minMoves(pos - steps, steps + 1, move + 1, N);
  23. int i = minMoves(pos + steps, steps + 1, move + 1, N);
  24. return Math.min(i, j);
  25. }
  26. }
Success #stdin #stdout 0.09s 320320KB
stdin
Standard input is empty
stdout
5