fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4. #define nl '\n'
  5.  
  6. void fastio() {
  7. std::ios_base::sync_with_stdio(false);
  8. std::cin.tie(nullptr);
  9. std::cout.tie(nullptr);
  10. }
  11.  
  12. void solve() {
  13. int n; cin >> n;
  14. vector<vector<int>>matrix(n+1,vector<int>(n+1));
  15. vector<tuple<int,int,int>>ops;
  16. for(int i =1;i<=n;i++){
  17. for(int j = 1 ; j <= n ; j++){
  18. matrix[i][j] =j;
  19. }
  20. }
  21. for(int i = 1; i <= n; i++) {
  22. // two reverse from 1 to i then from i+1 to n
  23. reverse(matrix[i].begin() + 1, matrix[i].begin() + i );
  24. reverse(matrix[i].begin() + i , matrix[i].begin() + 1 + n);
  25.  
  26. //if i ==1 this will not affect
  27. if(i!=1) ops.push_back({i, 1, i});
  28.  
  29. // as if i == n if we do i+1 this will be out of boundaires
  30. if(i+1 <n) ops.push_back({i, i+1, n});
  31. }
  32.  
  33.  
  34. cout << ops.size() << nl;
  35. for(auto i : ops){
  36. cout << get<0>(i) << " " << get<1>(i) << " " << get<2>(i) << nl;
  37. }
  38.  
  39. // just for testing
  40. // for(int i =1;i<=n;i++){
  41. // for(int j =1;j<=n;j++){
  42. // cout << matrix[i][j] << " ";
  43. // }
  44. // cout << nl;
  45. // }
  46.  
  47.  
  48. }
  49.  
  50. int main() {
  51. fastio();
  52. int t = 1;
  53. cin >> t;
  54. while (t--)
  55. solve();
  56. return 0;
  57. }
  58.  
Success #stdin #stdout 0s 5336KB
stdin
2 3 4
stdout
3
1 2 3
2 1 2
3 1 3
5
1 2 4
2 1 2
2 3 4
3 1 3
4 1 4