fork download
  1.  
  2. import java.util.*;
  3. import java.lang.*;
  4. import java.io.*;
  5.  
  6.  
  7. class Main
  8. {
  9.  
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. int vertSize;
  13. int horSize;
  14. int firstNumber;
  15. int lastNumber;
  16. Scanner in = new Scanner(System.in);
  17. horSize = in.nextInt();
  18. vertSize = in.nextInt();
  19. firstNumber = in.nextInt();
  20. lastNumber= in.nextInt();
  21. if (firstNumber > lastNumber){
  22. int C = firstNumber;
  23. firstNumber = lastNumber;
  24. lastNumber = C;
  25. }
  26. //вычисление координат
  27. int firstNumberx = (firstNumber - 1) % (2 * horSize) + 1;
  28. int firstNumbery = (firstNumber - 1) / (2 * horSize) + 1;
  29. int lastNumberx = (lastNumber - 1) % (2 * horSize) + 1;
  30. int lastNumbery = (lastNumber - 1) / (2 * horSize) + 1;
  31. //создание и задание размера для статического массива
  32. int[] Search= new int[2 * horSize];
  33. Search[firstNumberx - 1] = 0;
  34. int step = 1;
  35. for (int i = firstNumberx - 2; i >= 0; i--){
  36. Search[i] = step++;
  37. }
  38. step = 1;
  39. for (int i = firstNumberx; i < 2 * horSize; i++){
  40. Search[i] = step++;
  41. }
  42. //сравнение по у
  43. for (int j = firstNumbery; j < lastNumbery; j++) {
  44. for (int i = 1; i < 2 * horSize; i += 2) {
  45. Search[i - 1] = Search[i] + 1;
  46. Search[i] += 2;
  47. //определние значения на предпоследнем элементе
  48. if (((i - 2) >= 0) && (Search[i]<Search[i - 2])){
  49. Search[i - 2] = Search[i];
  50. }
  51. }
  52. }
  53. System.out.println(Search[lastNumberx - 1]);
  54. }
  55. }
Success #stdin #stdout 0.07s 2184192KB
stdin
5 4  1 38
stdout
13