Advertisement
Guest User

Untitled

a guest
Nov 12th, 2019
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.46 KB | None | 0 0
  1. #define N 9
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. typedef struct{double r[N];} rowOf;
  6.  
  7. void cpyMatrix(rowOf a[], rowOf b[]){
  8.     for(int r = 0; r < N; r++){
  9.         a[r] = b[r];
  10.     }
  11. }
  12.  
  13. double matrixEval(rowOf matrix[]){
  14.     double det = 1;
  15.  
  16.     for(int i = 0; i < N; i++){
  17.         bool found = false;
  18.         for(int row = i; row < N; row++){
  19.             if(matrix[row].r[i] != 0){
  20.                 if(row != i){
  21.                     rowOf temp = matrix[row];
  22.                     matrix[row] = matrix[i];
  23.                     matrix[i] = temp;
  24.                 }
  25.                 found = true;
  26.                 break;
  27.             }
  28.         }
  29.         if(!found){
  30.             det = 0;
  31.             break;
  32.         }
  33.         det *= matrix[i].r[i];
  34.         for(int row = i + 1; row < N; row++){
  35.             double coff = matrix[row].r[i] / matrix[i].r[i];
  36.             for(int j = i; j < N; j++){
  37.                 matrix[row].r[j] -= coff * matrix[i].r[j];
  38.                 if (abs(matrix[row].r[j]) < 10e-11)
  39.                     matrix[row].r[j] = 0;
  40.             }
  41.         }
  42.     }
  43.     return det;
  44. }
  45.  
  46. int main(){
  47.     cout << hex;
  48.     srand(time(NULL));
  49.  
  50.     int maxOfMax = 0;
  51.     string matrixString = "134278569569341827827695134298456371371982645645713298416837952783529416952164783";
  52.  
  53.     // random_shuffle(matrixString.begin(), matrixString.end());
  54.  
  55.     rowOf matrix[9], maxMatrix[9], matrixCopy[9], matrixSave[9];
  56.  
  57.     int iteration = 0;
  58.  
  59.     int r = 0, c = 0;
  60.  
  61.     for(char& num : matrixString){
  62.         matrix[r].r[c] = (double)(num - '0');
  63.         cout << matrix[r].r[c] << ' ';
  64.         r++;
  65.         if (r == N){
  66.             r = 0;
  67.             c += 1;
  68.         }
  69.     }
  70.    
  71.     pair<int, int> bestSwap = make_pair(-1,-1), lastSwap;
  72.     while(maxOfMax < 0x3704d007){
  73.         int maxVal = 0;
  74.         for(int limit = 0; limit < N; limit++){
  75.             for(int i = limit * N; i < (limit + 1) * N; i++){
  76.                 for(int j = i + 1; j < (limit + 1) * N; j++){
  77.                     if(matrix[i / N].r[i % N] == matrix[j / N].r[j % N]) continue;
  78.  
  79.                     cpyMatrix(matrixCopy, matrix);
  80.  
  81.                     matrixCopy[i / N].r[i % N] = matrix[j / N].r[j % N];
  82.                     matrixCopy[j / N].r[j % N] = matrix[i / N].r[i % N];
  83.  
  84.                     cpyMatrix(matrixSave, matrixCopy);
  85.  
  86.                     int det = ceil(matrixEval(matrixCopy));
  87.  
  88.                     if(abs(det) > maxVal){
  89.                         bool latin = true;
  90.  
  91.                         for(int index = 0; index < N; index++){
  92.                            
  93.                             set<double> row;
  94.                             set<double> col;
  95.  
  96.                             for(int x = 0; x < N; x++){
  97.                                 row.insert(matrixSave[index].r[x]);
  98.                                 col.insert(matrixSave[x].r[index]);
  99.                             }
  100.                             if(row.size() != N || col.size() != N){
  101.                                 latin = false;
  102.                                 break;
  103.                             }
  104.                         }
  105.                         if(!latin){
  106.                             maxVal = abs(det);
  107.                             cpyMatrix(maxMatrix, matrixSave);
  108.                             bestSwap = make_pair(i, j);
  109.                         }
  110.                     }
  111.                 }
  112.             }
  113.  
  114.         }
  115.        
  116.         cpyMatrix(matrix, maxMatrix);
  117.  
  118.         if(lastSwap == bestSwap){
  119.             cout << iteration++ << endl;
  120.             if(maxVal > maxOfMax){
  121.                 for(auto& row : matrix){
  122.                     for(auto& a : row.r){
  123.                         cout << a;
  124.                     }
  125.                 }
  126.                
  127.                 cout << '\n';
  128.                
  129.                 for(auto& row : matrix){
  130.                     for(auto& a : row.r){
  131.                         cout << a << ' ';
  132.                     }
  133.                     cout << '\n';
  134.                 }
  135.                 cout << "iteration: " << iteration << " " << maxOfMax << ' ' << maxVal << '\n';
  136.                 maxOfMax = maxVal;
  137.             }
  138.  
  139.             int row = rand() % N;
  140.  
  141.             for(int i = 0; i < N; i ++){
  142.                 int swap = rand() % 9;
  143.                 double temp = matrix[row].r[i];
  144.                 matrix[row].r[i] = matrix[row].r[swap];
  145.                 matrix[row].r[swap] = temp;
  146.             }
  147.         }
  148.        
  149.         lastSwap = bestSwap;
  150.     }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement