Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int checkIsChessBoard(int mat[4][4], int n);
- int main()
- {
- int mat[4][4] =
- {
- {1, 0, 1, 0},
- {0, 1, 0, 1},
- {1, 0, 1, 0},
- {0, 1, 0, 1}
- };
- int isBoard = checkIsChessBoard(mat, 4);
- if (isBoard == 1) {
- cout << "Matrix can be a chessboard";
- } else {
- cout << "Matrix can't be a chessboard";
- }
- return 0;
- }
- int canTransformToChessBoard(int mat[4][4], int n) {
- int numberOf1s = mat[0][0] ^ mat[3][0] ^ mat[]
- }
- int checkIsChessBoard(int mat[4][4], int n) {
- int isBoard = 1;
- for (int i = 0; i < n; i++)
- {
- if (isBoard == 0) break;
- for (int j = 0; j < n; j++)
- {
- // If it is the first line we do not check above and we take care of the margins
- if (i == 0)
- {
- // if it is the first column, we only check right and below neighbnors
- if (j == 0)
- {
- if (mat[i][j] == mat[i+1][j] || mat[i][j] == mat[i][j+1])
- {
- isBoard = 0;
- break;
- }
- // If it is the last column, we only check left and below neighbour
- }
- else if (j == n-1)
- {
- if (mat[i][j] == mat[i+1][j] || mat[i][j] == mat[i][j-1])
- {
- isBoard = 0;
- break;
- }
- // otherwise we check left, right and below neighbour
- }
- else
- {
- if ( mat[i][j] == mat[i][j-1] || mat[i][j] == mat[i][j+1] || mat[i][j] == mat[i+1][j])
- {
- isBoard = 0;
- break;
- }
- }
- }
- else if (i == n-1) // If it is the last line, we check above and take care of the margins
- {
- // If it is the first column, we only check above and right neighbours
- if (j == 0)
- {
- if (mat[i][j] == mat[i-1][j] || mat[i][j] == mat[i][j+1])
- {
- isBoard = 0;
- break;
- }
- }
- else if (j == n -1 ) // if it is the last column we only check above and left neighbours {
- {
- if (mat[i][j] == mat[i-1][j] || mat[i][j] == mat[i][j-1])
- {
- isBoard = 0;
- break;
- }
- }
- else // otherwise we check above, left and right neighbours
- {
- if (mat[i][j] == mat[i-1][j] || mat[i][j] == mat[i][j-1] || mat[i][j] == mat[i][j+1])
- {
- isBoard = 0;
- break;
- }
- }
- }
- 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
- {
- if (j == 0) // If it is the first column we check above, below and right neighbours
- {
- if (mat[i][j] == mat[i-1][j] || mat[i][j] == mat[i+1][j] || mat[i][j] == mat[i][j+1])
- {
- isBoard = 0;
- break;
- }
- }
- else if (j == n - 1) // If we are on the last column, we only check below, above and left neighbour
- {
- if (mat[i][j] == mat[i-1][j] || mat[i][j] == mat[i+1][j] || mat[i][j] == mat[i][j-1])
- {
- isBoard = 0;
- break;
- }
- } else { // We can check all 4 neighbours: below, above, left and right
- if (
- mat[i][j] == mat[i-1][j] ||
- mat[i][j] == mat[i+1][j] ||
- mat[i][j] == mat[i][j-1]||
- mat[i][j] == mat[i][j+1])
- {
- isBoard = 0;
- break;
- }
- }
- }
- }
- }
- return isBoard;
- }
Advertisement
Add Comment
Please, Sign In to add comment