fork download
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4.  
  5. class Main
  6. {
  7. public static void main (String[] args) throws java.lang.Exception
  8. {
  9. Scanner in = new Scanner(System.in);
  10. int[][] x = new int[2001][2001];
  11. int n = in.nextInt();
  12. int move;
  13. int xPos=1001, yPos=1001;
  14. for(int i = 0; i < n; i++) {
  15. move = in.nextInt();
  16. if(x[xPos][yPos] == 1) {
  17. System.out.print(i);
  18. return;
  19. }
  20. else x[xPos][yPos]=1;
  21. if(move == 1) {
  22. xPos--;
  23. yPos++;
  24. }
  25. if(move == 2) {
  26. yPos++;
  27. }
  28. if(move == 3) {
  29. xPos++;
  30. yPos++;
  31. }
  32. if(move == 4) {
  33. xPos++;
  34. }
  35. if(move == 5) {
  36. xPos++;
  37. yPos--;
  38. }
  39. if(move == 6) {
  40. yPos--;
  41. }
  42. if(move == 7) {
  43. xPos--;
  44. yPos--;
  45. }
  46. if(move == 8) {
  47. xPos--;
  48. }
  49. }
  50. System.out.print("Ok" + "\n" + (Math.abs(xPos-1001)+Math.abs(yPos-1001)));
  51. }
  52. }
Success #stdin #stdout 0.07s 2184192KB
stdin
9 2 2 3 3 4 4 7 7 7
stdout
Ok
2