fork download
  1. #include <bits/stdc++.h>
  2. #include <cctype>
  3. #include <cstdlib>
  4. #include <iostream>
  5. #include <pthread.h>
  6. #include <vector>
  7.  
  8. void one_piece() {
  9. std::cin.tie(0);
  10. std::cin.sync_with_stdio(0);
  11. std::cout.tie(0);
  12. std::cout.sync_with_stdio(0);
  13. }
  14.  
  15.  
  16. int main() {
  17. one_piece();
  18.  
  19. int n, m, r, c;
  20. std::cin >> n >> m >> r >> c;
  21.  
  22. int sum = 0;
  23. int empty = ((r-1)*m) + c;
  24.  
  25. for(int i = 1; i <= n; i++) {
  26. for(int j = 1; j <= m; j++) {
  27. int id = ((i-1)*m) + j;
  28. if(id > empty) {
  29. if(id%m == 1) {
  30. int newr = i - 1;
  31. long long distance = std::abs(i - newr) + std::abs(j - m);
  32. sum += distance;
  33. } else {
  34. int newc = j - 1;
  35. long long distance = std::abs(i - i) + std::abs(j - newc);
  36. sum += distance;
  37. }
  38. empty = id;
  39. }
  40. }
  41. }
  42.  
  43. std::cout << sum;
  44.  
  45.  
  46. return 0;
  47. }
Success #stdin #stdout 0s 5284KB
stdin
2 3 1 2
stdout
6