fork download
  1. #include <bits/stdc++.h>
  2. #include <chrono>
  3. using namespace std;
  4. using namespace chrono;
  5. // "AJEET JAIN"----"JAI JINENDRA"
  6. /* "णमो अरिहंताणं",
  7.   "णमो सिद्धाणं",
  8.   "णमो आयरियाणं",
  9.   "णमो उवज्झायाणं",
  10.   "णमो लोए सव्वसाहूणं",
  11.   "",
  12.   "एसो पंच नमोक्कारो, सव्व पावप्पणासणो",
  13.   "मंगलाणं च सव्वेसिं, पडमं हवै मंगलं", */
  14.  
  15.  
  16. // Aliases to op
  17. using ll = long long;
  18. using ull = unsigned long long;
  19. using ld = double;
  20. using vll = vector<ll>;
  21.  
  22.  
  23. // Constants
  24. constexpr ll INF = 4e18;
  25. constexpr ld EPS = 1e-9;
  26. constexpr ll MOD = 1e9 + 7;
  27.  
  28.  
  29.  
  30. // Macros
  31. #define F first
  32. #define S second
  33. #define all(x) begin(x), end(x)
  34. #define allr(x) rbegin(x), rend(x)
  35. #define py cout<<"YES\n";
  36. #define pn cout<<"NO\n";
  37. #define forn(i,n) for(int i=0;i<n;i++)
  38. #define for1(i,n) for(int i=1;i<=n;i++)
  39.  
  40. // #define insert push_back
  41. #define pb push_back
  42. #define MP make_pair
  43. #define endl '\n'
  44.  
  45.  
  46.  
  47. /*
  48.   ROUGH --
  49.  
  50.   */
  51.  
  52. void AJNJ() {
  53. ll n;
  54. cin >> n;
  55.  
  56. vector<vll> v(n + 2, vll(n + 2, 0));
  57. vector<vll> mark(n + 2, vll(n + 2, 0));
  58.  
  59. for (int i = 1; i <= n; i++) {
  60. for (int j = 1; j <= n; j++) {
  61. cin >> v[i][j];
  62. }
  63. }
  64.  
  65. for (int i = 1; i <= n; i++) {
  66. for (int j = 1; j <= n; j++) {
  67. ll k = v[i][j];
  68. int chk = 0;
  69.  
  70. if (j + 1 <= n && v[i][j + 1] == k){
  71. chk++;
  72. }
  73. if (j - 1 >= 1 && v[i][j - 1] == k){
  74. chk++;
  75. }
  76. if (i - 1 >= 1 && v[i - 1][j] == k){
  77. chk++;
  78. }
  79. if (i + 1 <= n && v[i + 1][j] == k){
  80. chk++;
  81. }
  82.  
  83. if (chk >= 2) {
  84. mark[i][j] = 1;
  85. }
  86. }
  87. }
  88.  
  89.  
  90. for (int i = 1; i <= n; i++) {
  91. for (int j = 1; j <= n; j++) {
  92. if (!mark[i][j]) continue;
  93. ll k = v[i][j];
  94.  
  95. if (j + 1 <= n && v[i][j + 1] == k){
  96. mark[i][j + 1] = 1;
  97. }
  98. if (j - 1 >= 1 && v[i][j - 1] == k){
  99. mark[i][j - 1] = 1;
  100. }
  101. if (i - 1 >= 1 && v[i - 1][j] == k){
  102. mark[i - 1][j] = 1;
  103. }
  104. if (i + 1 <= n && v[i + 1][j] == k){
  105. mark[i + 1][j] = 1;
  106. }
  107. }
  108. }
  109.  
  110. for (int i = 1; i <= n; i++) {
  111. for (int j = 1; j <= n; j++) {
  112. if (mark[i][j]) v[i][j] = 0;
  113. }
  114. }
  115.  
  116. for (int j = 1; j <= n; j++) {
  117. vll col;
  118. for (int i = n; i >= 1; i--) {
  119. if (v[i][j] > 0) col.push_back(v[i][j]);
  120. }
  121.  
  122. int idx = 0;
  123. for (int i = n; i >= 1; i--) {
  124. if (idx < (ll)col.size()){
  125. v[i][j] = col[idx++];
  126. }
  127. else {
  128. v[i][j] = 0;
  129. }
  130. }
  131. }
  132.  
  133. for (int i = 1; i <= n; i++) {
  134. for (int j = 1; j <= n; j++) {
  135. cout << v[i][j] << " ";
  136. }
  137. cout << endl;
  138. }
  139. }
  140.  
  141.  
  142. int main(){
  143. ios::sync_with_stdio(0);
  144. cin.tie(0);
  145. cout.tie(0);
  146. int T = 1;
  147. cin>>T;
  148. auto start1 = high_resolution_clock::now();
  149. while(T--){
  150. AJNJ();
  151. }
  152. auto stop1 = high_resolution_clock::now();
  153. auto duration = duration_cast<microseconds>(stop1 - start1);
  154. cerr << "Time: " << duration . count() / 1000 << " ms" << endl;
  155.  
  156. return 0;
  157. }
Success #stdin #stdout #stderr 0s 5316KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Time: 0 ms