bogdanNiculeasa

Polihroniade

Mar 6th, 2022
1,200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.72 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. using namespace std;
  5.  
  6. void getSolution(int p, int matrix[1000][1000], int n);
  7. int isChessBoard(int mat[1000][1000], int n);
  8. void canTransformToChessBoard(int matrix[1000][1000], int n);
  9. void displayDetailedStepsForTransforming(int matrix[1000][1000], int n, int writeSteps);
  10. int findNextLineToBeSwapped(int matrix[1000][1000], int n, int startingLine);
  11. void swapRows(int mat[1000][1000], int n, int row1, int row2);
  12. void swapColumns(int mat[1000][1000], int n, int col1, int col2);
  13. int findNextColumnToBeSwapped(int matrix[1000][1000], int n, int startingCol);
  14. void writeNumberOfStepsNeededToTransform(int matrix[1000][1000], int n);
  15.  
  16.  
  17. ifstream fin("polihroniade.in");
  18. ofstream fout("polihroniade.out", std::ios_base::trunc);
  19.  
  20. int main()
  21. {
  22.     int p, t;
  23.     fin >> p >> t;
  24.     int currentScenario = 1;
  25.     while (currentScenario <= t)
  26.     {
  27.         int n;
  28.         fin >> n;
  29.         static int matrix[1000][1000] = {0};
  30.  
  31.         for (int i = 0; i < n; i++)
  32.         {
  33.             char line[n];
  34.             fin >> line;
  35.             for (int j = 0; j <  n; j++)
  36.             {
  37.                 matrix[i][j] = line[j] - '0';
  38.             }
  39.         }
  40.         getSolution(p, matrix, n);
  41.         currentScenario++;
  42.     }
  43.     fout.close();
  44.     fin.close();
  45.     return 0;
  46. }
  47.  
  48. void getSolution(int p, int matrix[1000][1000], int n) {
  49.     switch (p) {
  50.         case 1:
  51.             canTransformToChessBoard(matrix, n);
  52.             break;
  53.         case 2:
  54.             writeNumberOfStepsNeededToTransform(matrix, n);
  55.             break;
  56.         case 3:
  57.             displayDetailedStepsForTransforming(matrix, n, 1);
  58.             break;
  59.         default:
  60.             break;
  61.     }
  62. }
  63.  
  64. void canTransformToChessBoard(int matrix[1000][1000], int n) {
  65.     int canBeTransformed = 1;
  66.     int sum = 0;
  67.     for (int i = 0; i < n; i++) {
  68.         for (int j = 0; j < n; j++) {
  69.             sum += matrix[i][j];
  70.             if (matrix[0][0] ^ matrix[0][j] ^ matrix[i][0] ^ matrix[i][j]) {
  71.                  canBeTransformed = 0; // can't be a chessboard
  72.                 break;
  73.             }
  74.         }
  75.     }
  76.     // To check the casses where matrix is filled only with 1s or 0s;
  77.     if (sum == n * n || sum == 0) {
  78.         fout << 0 << endl;
  79.     } else {
  80.         fout << canBeTransformed << endl;
  81.     }
  82. }
  83.  
  84.  
  85.  
  86. void displayDetailedStepsForTransforming(int matrix[1000][1000], int n, int writeStepsToFile) {
  87.     if (isChessBoard(matrix, n)) {
  88.         fout << 0 << endl;
  89.     } else {
  90.         // We do not check if the matrix can be or not chess because it is guaranteed by the problem
  91.         // we call the prev function which knows to write the number of steps
  92.         writeNumberOfStepsNeededToTransform(matrix, n);
  93.  
  94.         int rowSwap = 0, colSwap = 0;
  95.         for (int i = 0; i < n; i++) {
  96.             if (matrix[i][0] == i % 2) {
  97.                 rowSwap++;
  98.                 int lineToBeSwapped = findNextLineToBeSwapped(matrix, n, i);
  99.                 fout << "L " << i + 1 << " " << lineToBeSwapped + 1 << endl;
  100.                 swapRows(matrix, n, i, lineToBeSwapped);
  101.             }
  102.             if (matrix[0][i] == i % 2) {
  103.                 colSwap++;
  104.                 int colToBeSwapped = findNextColumnToBeSwapped(matrix, n, i);
  105.                 fout << "C " << i + 1 << " " << colToBeSwapped + 1 << endl;
  106.                 swapColumns(matrix, n, i, colToBeSwapped);
  107.             }
  108.         }
  109.     }
  110. }
  111.  
  112. void writeNumberOfStepsNeededToTransform(int matrix[1000][1000], int n) {
  113.     // We do not check if the matrix can be or not chess because it is guaranteed by the problem
  114.     int rowSwap = 0, colSwap = 0;
  115.     for (int i = 0; i < n; i++) {
  116.  
  117.         if (matrix[i][0] == i % 2) {
  118.             rowSwap++;
  119.         }
  120.         if (matrix[0][i] == i % 2) {
  121.             colSwap++;
  122.         }
  123.     }
  124.     colSwap = min(colSwap, n-colSwap);
  125.     rowSwap = min(rowSwap, n - rowSwap);
  126.  
  127.     fout << (rowSwap + colSwap) / 2 << endl;
  128. }
  129.  
  130. int isChessBoard(int mat[1000][1000], int n) {
  131.     int isBoard = 1;
  132.     for (int i = 0; i < n; i++)
  133.     {
  134.         if (isBoard == 0) break;
  135.         for (int j = 0; j < n; j++)
  136.         {
  137.             // If it is the first line we do not check above and we take care of the margins
  138.             if (i == 0)
  139.             {
  140.                 // if it is the first column, we only check right and below neighbnors
  141.                 if (j == 0)
  142.                 {
  143.                     if (mat[i][j] == mat[i+1][j] || mat[i][j] == mat[i][j+1])
  144.                     {
  145.                         isBoard = 0;
  146.                         break;
  147.                     }
  148.                     // If it is the last column, we only check left and below neighbour
  149.                 }
  150.                 else if (j == n-1)
  151.                 {
  152.                     if (mat[i][j] == mat[i+1][j] || mat[i][j] == mat[i][j-1])
  153.                     {
  154.                         isBoard = 0;
  155.                         break;
  156.                     }
  157.                     // otherwise we check left, right and below neighbour
  158.                 }
  159.                 else
  160.                 {
  161.                     if ( mat[i][j] == mat[i][j-1] || mat[i][j] == mat[i][j+1] || mat[i][j] == mat[i+1][j])
  162.                     {
  163.                         isBoard = 0;
  164.                         break;
  165.                     }
  166.                 }
  167.             }
  168.             else if (i == n-1)     // If it is the last line, we check above and take care of the margins
  169.             {
  170.                 // If it is the first column, we only check above and right neighbours
  171.                 if (j == 0)
  172.                 {
  173.                     if (mat[i][j] == mat[i-1][j] ||  mat[i][j] == mat[i][j+1])
  174.                     {
  175.                         isBoard = 0;
  176.                         break;
  177.                     }
  178.                 }
  179.                 else if (j == n -1 )     // if it is the last column we only check above and left neighbours {
  180.                 {
  181.                     if (mat[i][j] == mat[i-1][j] || mat[i][j] == mat[i][j-1])
  182.                     {
  183.                         isBoard = 0;
  184.                         break;
  185.                     }
  186.                 }
  187.                 else     // otherwise we check above, left and right neighbours
  188.                 {
  189.                     if (mat[i][j] ==  mat[i-1][j] || mat[i][j] == mat[i][j-1] || mat[i][j] == mat[i][j+1])
  190.                     {
  191.                         isBoard = 0;
  192.                         break;
  193.                     }
  194.                 }
  195.             }
  196.             else     // We are in the middle of the matrix and can check both above and below matrix but also left and right depending on the columns
  197.             {
  198.                 if (j == 0)   // If it is the first column we check above, below and right neighbours
  199.                 {
  200.                     if (mat[i][j] == mat[i-1][j] || mat[i][j] == mat[i+1][j] || mat[i][j] == mat[i][j+1])
  201.                     {
  202.                         isBoard = 0;
  203.                         break;
  204.                     }
  205.                 }
  206.                 else if (j == n - 1)     // If we are on the last column, we only check below, above and left neighbour
  207.                 {
  208.                     if (mat[i][j] == mat[i-1][j] || mat[i][j] == mat[i+1][j] || mat[i][j] == mat[i][j-1])
  209.                     {
  210.                         isBoard = 0;
  211.                         break;
  212.                     }
  213.                 } else { // We can check all 4 neighbours: below, above, left and right
  214.                     if (
  215.                             mat[i][j] == mat[i-1][j] ||
  216.                             mat[i][j] == mat[i+1][j] ||
  217.                             mat[i][j] == mat[i][j-1]||
  218.                             mat[i][j] == mat[i][j+1])
  219.                     {
  220.                         isBoard = 0;
  221.                         break;
  222.                     }
  223.                 }
  224.             }
  225.         }
  226.     }
  227.     return isBoard;
  228. }
  229.  
  230. void swapRows(int mat[1000][1000], int n, int row1, int row2) {
  231.     for (int i = 0; i < n; i++) {
  232.         int temp = mat[row1][i];
  233.         mat[row1][i] = mat[row2][i];
  234.         mat[row2][i] = temp;
  235.     }
  236. }
  237.  
  238. void swapColumns(int mat[1000][1000], int n, int col1, int col2) {
  239.     for (int i = 0; i < n; i++) {
  240.         int temp = mat[i][col1];
  241.         mat[i][col1] = mat[i][col2];
  242.         mat[i][col2] = temp;
  243.     }
  244. }
  245.  
  246. int findNextLineToBeSwapped(int matrix[1000][1000], int n, int startingLine) {
  247.     for (int i = startingLine+1; i < n; i++) {
  248.         if (matrix[i][0] == i % 2) {
  249.             return i;
  250.         }
  251.     }
  252.     return -1;
  253. }
  254.  
  255. int findNextColumnToBeSwapped(int matrix[1000][1000], int n, int startingCol) {
  256.     for (int i = startingCol+1; i < n; i++) {
  257.         if (matrix[0][i] == i % 2) {
  258.             return i;
  259.         }
  260.     }
  261.     return -1;
  262. }
Advertisement
Add Comment
Please, Sign In to add comment