Given an NXN chessboard. The task is to check if the given chessboard is valid or not. A chess board is considered valid if every 2 adjacent cells are painted with different color. Two cells are considered adjacent if they share a boundary.
First chessboard is valid whereas the second is invalid.
Examples:
Input : N = 2
C = {
{ 1, 0 },
{ 0, 1 }
}
Output : Yes
Input : N = 2
C = {
{ 0, 0 },
{ 0, 0 }
}
Output : No
Observe, on a chess board, every adjacent pair of cells is painted in a different color. So, for each cell (i, j), check whether the adjacent cells i.e (i + 1, j), (i -1, j), (i, j + 1), (i, j – 1) is painted with different color than (i, j) or not.
Pseudocode:
int X[] = {0, -1, 0, 1};
int Y[] = {1, 0, -1, 0};
bool validateConfiguration(int M[N][N])
{
bool isValid = true;
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
for (int k = 0; k < 4; k++)
{
int newX = i + X[k];
int newY = j + Y[k];
if (newX < N && newY < N && newX >= 0 &&
newY >= 0 && M[newX][newY] == M[i][j])
{
isValid = false;
}
}
}
}
return isValid;
}
Implementation:
C++
#include <bits/stdc++.h>
usingnamespacestd;
#define MAX 2
// Check if the given chess board is valid or not.
Time complexity: O(N^2) for given an N*N chessboard Auxiliary space: O(1)
Implementation: A simpler approach would be to check if the cells with even sums are of the same color.
C++
#include <bits/stdc++.h>
usingnamespacestd;
boolcheckBoard(vector<vector<int>> board)
{
intbase = board[0][0];
boolflag = true;
for(inti = 0; i < board.size(); i++)
{
for( intj = 0; j < board[i].size(); j++)
{
if(( i + j ) % 2 == 0)
{
if( board[i][j] != base )
{
returnfalse;
}
}
else
{
if(board[i][j] == base)
{
returnfalse;
}
}
}
}
returntrue;
}
intmain()
{
vector<vector<int>> board1={{0, 1},
{1, 0}};
vector<vector<int>> board2={{1, 0, 1},
{0, 1, 0},
{1, 0, 1}};
vector<vector<int>> board3={{1, 0, 1},
{0, 1, 0},
{1, 1, 1}};
if(checkBoard(board1))
cout << "true\n";
else
cout << "false\n";
if(checkBoard(board2))
cout << "true\n";
else
cout << "false\n";
if(checkBoard(board3))
cout << "true\n";
else
cout << "false\n";
return0;
}
//This code is contributed by aditya942003patil
Java
/*package whatever //do not write package name here */
importjava.io.*;
classGFG
{
staticbooleancheckBoard(int[][] board)
{
intbase = board[0][0];
booleanflag = true;
for(inti = 0; i < board.length; i++)
{
for( intj = 0; j < board[i].length; j++)
{
if(( i + j ) % 2== 0)
{
if( board[i][j] != base )
{
returnfalse;
}
}
else
{
if(board[i][j] == base)
{
returnfalse;
}
}
}
}
returntrue;
}
// Driver code
publicstaticvoidmain (String[] args) {
int[][] board1={{0, 1}, {1, 0}};
int[][] board2={{1, 0, 1},{0, 1, 0},{1, 0, 1}};
int[][] board3={{1, 0, 1},{0, 1, 0},{1, 1, 1}};
System.out.println(checkBoard(board1));
System.out.println(checkBoard(board2));
System.out.println(checkBoard(board3));
}
}
// This code is contributed by rag2127
Python3
# Write Python3 code here
defcheckBoard(board) :
base =board[0][0]
flag =True
fori inrange(len(board)) :
forj inrange(len(board[i])) :
if( i +j ) %2==0:
ifboard[i][j] !=base :
returnFalse
else:
ifboard[i][j] ==base :
returnFalse
returnTrue
board1 =[[0, 1], [1, 0]]
board2 =[[1, 0, 1], [0, 1, 0], [1, 0, 1]]
board3 =[[1, 0, 1], [0, 1, 0], [1, 1, 1]]
print(checkBoard(board1))
print(checkBoard(board2))
print(checkBoard(board3))
C#
usingSystem;
publicclassGFG
{
staticboolcheckBoard(int[,] board)
{
intBase = board[0, 0];
for(inti = 0; i < board.GetLength(0); i++)
{
for( intj = 0; j < board.GetLength(1); j++)
{
if(( i + j ) % 2 == 0)
{
if( board[i, j] != Base )
{
returnfalse;
}
}
else
{
if(board[i, j] == Base)
{
returnfalse;
}
}
}
}
returntrue;
}
// Driver code
staticpublicvoidMain ()
{
int[,] board1 = {{0, 1}, {1, 0}};
int[,] board2 = {{1, 0, 1},{0, 1, 0},{1, 0, 1}};
int[,] board3 = {{1, 0, 1},{0, 1, 0},{1, 1, 1}};
Console.WriteLine(checkBoard(board1));
Console.WriteLine(checkBoard(board2));
Console.WriteLine(checkBoard(board3));
}
}
// This code is contributed by avanitrachhadiya2155
Javascript
<script>
functioncheckBoard(board)
{
let base = board[0][0];
let flag = true;
for(let i = 0; i < board.length; i++)
{
for( let j = 0; j < board[i].length; j++)
{
if(( i + j ) % 2 == 0)
{
if( board[i][j] != base )
{
returnfalse;
}
}
else
{
if(board[i][j] == base)
{
returnfalse;
}
}
}
}
returntrue;
}
// Driver code
let board1 = [[0, 1], [1, 0]];
let board2 = [[1, 0, 1], [0, 1, 0], [1, 0, 1]];
let board3 = [[1, 0, 1], [0, 1, 0], [1, 1, 1]];
document.write(checkBoard(board1)+"<br>")
document.write(checkBoard(board2)+"<br>")
document.write(checkBoard(board3)+"<br>")
// This code is contributed by unknown2108
</script>
Output
true
true
false
Time complexity: O(N^2) for given an N*N chessboard Auxiliary space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!