Advertisement
Guest User

Untitled

a guest
Feb 24th, 2020
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.08 KB | None | 0 0
  1. #include <vector>
  2. #include <iostream>
  3. #include <algorithm>
  4. #include <set>
  5. #include <stack>
  6. #include <cmath>
  7. #include <random>
  8. #include <ctime>
  9. #include <fstream>
  10.  
  11. using namespace std;
  12.  
  13. int n;
  14.  
  15. struct shift {
  16.     int n, s;
  17.     char d;
  18.    
  19.     shift(int tn, char td, int ts): n(tn), d(td), s(ts) { }
  20. };
  21.  
  22. int dist(int x0, int y0, int x1, int y1) {
  23.     if (y0 == y1) {
  24.         return 4;
  25.     } else {
  26.         return 3;
  27.     }
  28. }
  29.  
  30. void make_shift(int x, char d, int s, vector<vector<int>> &v, vector<shift> &ans) {
  31.     ans.push_back(shift(x, d, s));
  32.     if (d == 'U') {
  33.         vector<int> tmp(n);
  34.         for (int i = 0; i < n; i++) {
  35.             int posx = i + s;
  36.             if (posx >= n) {
  37.                 posx -= n;
  38.             }
  39.             tmp[i] = v[posx][x];
  40.         }
  41.         for (int i = 0; i < n; i++) {
  42.             v[i][x] = tmp[i];
  43.         }
  44.     } else if (d == 'R') {
  45.         vector<int> tmp(n);
  46.         for (int i = 0; i < n; i++) {
  47.             int posx = i - s;
  48.             if (posx < 0) {
  49.                 posx += n;
  50.             }
  51.             tmp[i] = v[x][posx];
  52.         }
  53.         for (int i = 0; i < n; i++) {
  54.             v[x][i] = tmp[i];
  55.         }
  56.     } else if (d == 'D') {
  57.         vector<int> tmp(n);
  58.         for (int i = 0; i < n; i++) {
  59.             int posx = i - s;
  60.             if (posx < 0) {
  61.                 posx += n;
  62.             }
  63.             tmp[i] = v[posx][x];
  64.         }
  65.         for (int i = 0; i < n; i++) {
  66.             v[i][x] = tmp[i];
  67.         }
  68.     } else if (d == 'L') {
  69.         vector<int> tmp(n);
  70.         for (int i = 0; i < n; i++) {
  71.             int posx = i + s;
  72.             if (posx >= n) {
  73.                 posx -= n;
  74.             }
  75.             tmp[i] = v[x][posx];
  76.         }
  77.         for (int i = 0; i < n; i++) {
  78.             v[x][i] = tmp[i];
  79.         }
  80.     }
  81. }
  82.  
  83. void apply(int x0, int y0, int x1, int y1, vector<vector<int>> &v, vector<shift> &ans) {
  84.     if (y0 == y1) {
  85.         make_shift(x0, 'R', 1, v, ans);
  86.         make_shift(y1, 'D', x0 - x1, v, ans);
  87.         make_shift(x0, 'L', 1, v, ans);
  88.         make_shift(y1, 'U', x0 - x1, v, ans);
  89.     } else {
  90.         make_shift(y1, 'D', x0 - x1, v, ans);
  91.         int delta = y0 - y1;
  92.         if (delta < 0) {
  93.             delta += n;
  94.         }
  95.         make_shift(x0, 'L', delta, v, ans);
  96.         make_shift(y1, 'U', x0 - x1, v, ans);
  97.     }
  98. }
  99.  
  100. int main() {
  101.     int t;
  102.     cin >> t;
  103.     cout << t << endl;
  104.     while (t--) {
  105.         cin >> n;
  106.        
  107.         vector<vector<int>> tv(n, vector<int>(n));
  108.         for (int i = 0; i < n; i++) {
  109.             for (int j = 0; j < n; j++) {
  110.                 cin >> tv[i][j];
  111.             }
  112.         }
  113.        
  114.         vector<int> colors(n);
  115.         for (int i = 0; i < n; i++) {
  116.             colors[i] = i;
  117.         }
  118.        
  119.         int best_ans = 1000 * 1000 * 1000;
  120.         vector<shift> final_ans;
  121.        
  122.         for (int iter = 0; iter < 10000; iter++) {
  123.             vector<shift> ans;
  124.             vector<vector<int>> v(tv);
  125.            
  126.             for (int row = 0; row < n; row++) {
  127.                 int color = colors[row];
  128.                 vector<int> positions;
  129.                 vector<bool> used;
  130.                 for (int col = 0; col < n; col++) {
  131.                     if (v[row][col] != color) {
  132.                         positions.push_back(col);
  133.                         used.push_back(false);
  134.                     }
  135.                 }
  136.                
  137.                 for (int z = 0; z < n; z++) {
  138.                     for (int i = row + 1; i < n; i++) {
  139.                         for (int j = 0; j < n; j++) {
  140.                             if (v[i][j] == color) {
  141.                                 int cur_best = 100, best_ind = -1;
  142.                                 for (int t = 0; t < positions.size(); t++) {
  143.                                     if (used[t]) {
  144.                                         continue;
  145.                                     }
  146.                                     if (dist(i, j, row, positions[t]) < cur_best) {
  147.                                         cur_best = dist(i, j, row, positions[t]);
  148.                                         best_ind = t;
  149.                                     }
  150.                                 }
  151.                                
  152.                                 used[best_ind] = true;
  153.                                 apply(i, j, row, positions[best_ind], v, ans);
  154.                                
  155.                                
  156.                             }
  157.                         }
  158.                     }
  159.                 }
  160.             }
  161.            
  162.             if (best_ans > ans.size()) {
  163.                 best_ans = (int) ans.size();
  164.                 final_ans = ans;
  165.             }
  166.             random_shuffle(colors.begin(), colors.end());
  167.            
  168.         }
  169.         cout << final_ans.size() << endl;
  170.  
  171.         for (int i = 0; i < final_ans.size(); i++) {
  172.             cout << final_ans[i].n << ' ' << final_ans[i].d << ' ' << final_ans[i].s << endl;
  173.         }
  174.        
  175.     }
  176. }
  177.  
  178. //* * * * * ok
  179. //* * * * * ok
  180. //* * * b * cur
  181. //* * * * *
  182. //  * * * a *
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement