bogdanNiculeasa

inprogress

Mar 5th, 2022
885
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.44 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int checkIsChessBoard(int mat[4][4], int n);
  6.  
  7. int main()
  8. {
  9.     int mat[4][4] =
  10.     {
  11.         {1, 0, 1, 0},
  12.         {0, 1, 0, 1},
  13.         {1, 0, 1, 0},
  14.         {0, 1, 0, 1}
  15.     };
  16.     int isBoard = checkIsChessBoard(mat, 4);
  17.  
  18.     if (isBoard == 1) {
  19.         cout << "Matrix can be a chessboard";
  20.     } else {
  21.         cout << "Matrix can't be a chessboard";
  22.     }
  23.     return 0;
  24. }
  25.  
  26. int canTransformToChessBoard(int mat[4][4], int n) {
  27.     int numberOf1s = mat[0][0] ^ mat[3][0] ^ mat[]
  28. }
  29.  
  30. int checkIsChessBoard(int mat[4][4], int n) {
  31.     int isBoard = 1;
  32.     for (int i = 0; i < n; i++)
  33.     {
  34.         if (isBoard == 0) break;
  35.         for (int j = 0; j < n; j++)
  36.         {
  37.             // If it is the first line we do not check above and we take care of the margins
  38.             if (i == 0)
  39.             {
  40.                 // if it is the first column, we only check right and below neighbnors
  41.                 if (j == 0)
  42.                 {
  43.                     if (mat[i][j] == mat[i+1][j] || mat[i][j] == mat[i][j+1])
  44.                     {
  45.                         isBoard = 0;
  46.                         break;
  47.                     }
  48.                     // If it is the last column, we only check left and below neighbour
  49.                 }
  50.                 else if (j == n-1)
  51.                 {
  52.                     if (mat[i][j] == mat[i+1][j] || mat[i][j] == mat[i][j-1])
  53.                     {
  54.                         isBoard = 0;
  55.                         break;
  56.                     }
  57.                     // otherwise we check left, right and below neighbour
  58.                 }
  59.                 else
  60.                 {
  61.                     if ( mat[i][j] == mat[i][j-1] || mat[i][j] == mat[i][j+1] || mat[i][j] == mat[i+1][j])
  62.                     {
  63.                         isBoard = 0;
  64.                         break;
  65.                     }
  66.                 }
  67.             }
  68.             else if (i == n-1)     // If it is the last line, we check above and take care of the margins
  69.             {
  70.                 // If it is the first column, we only check above and right neighbours
  71.                 if (j == 0)
  72.                 {
  73.                     if (mat[i][j] == mat[i-1][j] ||  mat[i][j] == mat[i][j+1])
  74.                     {
  75.                         isBoard = 0;
  76.                         break;
  77.                     }
  78.                 }
  79.                 else if (j == n -1 )     // if it is the last column we only check above and left neighbours {
  80.                 {
  81.                     if (mat[i][j] == mat[i-1][j] || mat[i][j] == mat[i][j-1])
  82.                     {
  83.                         isBoard = 0;
  84.                         break;
  85.                     }
  86.                 }
  87.                 else     // otherwise we check above, left and right neighbours
  88.                 {
  89.                     if (mat[i][j] ==  mat[i-1][j] || mat[i][j] == mat[i][j-1] || mat[i][j] == mat[i][j+1])
  90.                     {
  91.                         isBoard = 0;
  92.                         break;
  93.                     }
  94.                 }
  95.             }
  96.             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
  97.             {
  98.                 if (j == 0)   // If it is the first column we check above, below and right neighbours
  99.                 {
  100.                     if (mat[i][j] == mat[i-1][j] || mat[i][j] == mat[i+1][j] || mat[i][j] == mat[i][j+1])
  101.                     {
  102.                         isBoard = 0;
  103.                         break;
  104.                     }
  105.                 }
  106.                 else if (j == n - 1)     // If we are on the last column, we only check below, above and left neighbour
  107.                 {
  108.                     if (mat[i][j] == mat[i-1][j] || mat[i][j] == mat[i+1][j] || mat[i][j] == mat[i][j-1])
  109.                     {
  110.                         isBoard = 0;
  111.                         break;
  112.                     }
  113.                 } else { // We can check all 4 neighbours: below, above, left and right
  114.                     if (
  115.                         mat[i][j] == mat[i-1][j] ||
  116.                         mat[i][j] == mat[i+1][j] ||
  117.                         mat[i][j] == mat[i][j-1]||
  118.                         mat[i][j] == mat[i][j+1])
  119.                     {
  120.                         isBoard = 0;
  121.                         break;
  122.                     }
  123.                 }
  124.             }
  125.         }
  126.     }
  127.     return isBoard;
  128. }
  129.  
Advertisement
Add Comment
Please, Sign In to add comment