fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define el "\n"
  4. #define ll long long
  5. #define ull unsigned long long
  6. #define se second
  7. #define fi first
  8. #define be begin()
  9. #define en end()
  10. #define Faster cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0);
  11. vector<pair<int,int>> vt;
  12. bool cmp(pair<int,int> a, pair<int,int> b)
  13. {
  14. if(a.fi + a.se != b.fi + b.se) return a.fi + a.se < b.fi + b.se;
  15. return a.fi < b.fi;
  16. }
  17. void findSubMatrix(int **a, int n)
  18. {
  19. for(pair<int,int> x : vt) cout << "(" << x.fi << "," << x.se << ")" << el;
  20. cout << el;
  21. }
  22.  
  23. void sortBySum(int **a, int n)
  24. {
  25. sort(vt.begin(), vt.end(), cmp);
  26. for(pair<int,int> x : vt) cout << "(" << x.fi << "," << x.se << ")" << el;
  27. cout << el;
  28. }
  29.  
  30. int main()
  31. {
  32. Faster;
  33. int n; cin >> n;
  34. int **a = new int*[n];
  35. for(int i = 0; i < n; i++)
  36. {
  37. a[i] = new int[n];
  38. }
  39. for(int i = 0; i < n; i++)
  40. {
  41. for(int j = 0; j < n; j++) cin >> a[i][j];
  42. }
  43. for(int i = 0; i < n - 1; i++)
  44. {
  45. for(int j = 0; j < n - 1; j++)
  46. {
  47. int ans = 0;
  48. for(int x = i; x < i + 2; x++)
  49. {
  50. for(int y =j; y < j + 2; y++)
  51. {
  52. ans += a[x][y];
  53. }
  54. }
  55. if(ans == 0 || ans == 4) vt.push_back({i,j});
  56. }
  57. }
  58. findSubMatrix(a, n);
  59. sortBySum(a, n);
  60. return 0;
  61. }
  62.  
  63.  
Success #stdin #stdout 0s 5288KB
stdin
6
0 1 0 1 0 1
0 1 1 0 1 1
0 1 1 1 1 1
1 1 0 1 0 1
1 1 1 0 1 1
1 1 0 1 1 1
stdout
(1,1)
(1,4)
(3,0)
(4,0)
(4,4)

(1,1)
(3,0)
(4,0)
(1,4)
(4,4)