fork download
  1. #include <iostream>
  2. using namespace std;
  3. int main() {
  4. int vertSize;
  5. int horSize;
  6. int firstNumber;
  7. int lastNumber;
  8. cin >> horSize >> vertSize >> firstNumber >> lastNumber;
  9. if (firstNumber > lastNumber){
  10. swap(firstNumber, lastNumber);
  11. }
  12. //вычисление координат
  13. int firstNumberx = (firstNumber - 1) % (2 * horSize) + 1;
  14. int firstNumbery = (firstNumber - 1) / (2 * horSize) + 1;
  15. int lastNumberx = (lastNumber - 1) % (2 * horSize) + 1;
  16. int lastNumbery = (lastNumber - 1) / (2 * horSize) + 1;
  17. //создание и задание размера для статического массива
  18. int Search[2 * horSize];
  19. Search[firstNumberx - 1] = 0;
  20. int step = 1;
  21. for (int i = firstNumberx - 2; i >= 0; i--){
  22. Search[i] = step++;
  23.  
  24. }
  25. step = 1;
  26. for (int i = firstNumberx; i < 2 * horSize; i++){
  27. Search[i] = step++;
  28. }
  29. //сравнение по у
  30. for (int j = firstNumbery; j < lastNumbery; j++) {
  31. for (int i = 1; i < 2 * horSize; i += 2) {
  32. Search[i - 1] = Search[i] + 1;
  33. Search[i] += 2;
  34. //определние значения на предпоследнем элементе
  35. if (((i - 2) >= 0) && (Search[i]<Search[i - 2])){
  36. Search[i - 2] = Search[i];
  37. }
  38. }
  39. }
  40. cout << Search[lastNumberx - 1] << endl;
  41. return 0;
  42. }
Success #stdin #stdout 0s 4496KB
stdin
5 4 7 14
stdout
3