fork(1) download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int long long
  4.  
  5. signed main() {
  6. int n, m;
  7. cin >> n >> m;
  8.  
  9. map<int, vector<pair<int,int>>> mp;
  10.  
  11. for(int i = 0; i < n; i++) {
  12. for(int j = 0; j < m; j++) {
  13. int x;
  14. cin >> x;
  15. mp[x].push_back({i, j});
  16. }
  17. }
  18.  
  19. for(auto &[val, v] : mp) {
  20.  
  21. cout << "Value = " << val << '\n';
  22.  
  23. int k = v.size();
  24.  
  25. for(int i = 0; i < k; i++) {
  26. for(int j = i + 1; j < k; j++) {
  27.  
  28. int dist =
  29. abs(v[i].first - v[j].first) +
  30. abs(v[i].second - v[j].second);
  31.  
  32. cout << "(" << v[i].first << "," << v[i].second << ") ";
  33. cout << "(" << v[j].first << "," << v[j].second << ") ";
  34. cout << "Distance = " << dist << '\n';
  35. }
  36. }
  37.  
  38. cout << '\n';
  39. }
  40. }
Success #stdin #stdout 0.01s 5324KB
stdin
3 4
1 1 2 2
2 1 1 2
2 2 1 1
stdout
Value = 1
(0,0) (0,1) Distance = 1
(0,0) (1,1) Distance = 2
(0,0) (1,2) Distance = 3
(0,0) (2,2) Distance = 4
(0,0) (2,3) Distance = 5
(0,1) (1,1) Distance = 1
(0,1) (1,2) Distance = 2
(0,1) (2,2) Distance = 3
(0,1) (2,3) Distance = 4
(1,1) (1,2) Distance = 1
(1,1) (2,2) Distance = 2
(1,1) (2,3) Distance = 3
(1,2) (2,2) Distance = 1
(1,2) (2,3) Distance = 2
(2,2) (2,3) Distance = 1

Value = 2
(0,2) (0,3) Distance = 1
(0,2) (1,0) Distance = 3
(0,2) (1,3) Distance = 2
(0,2) (2,0) Distance = 4
(0,2) (2,1) Distance = 3
(0,3) (1,0) Distance = 4
(0,3) (1,3) Distance = 1
(0,3) (2,0) Distance = 5
(0,3) (2,1) Distance = 4
(1,0) (1,3) Distance = 3
(1,0) (2,0) Distance = 1
(1,0) (2,1) Distance = 2
(1,3) (2,0) Distance = 4
(1,3) (2,1) Distance = 3
(2,0) (2,1) Distance = 1