Check if the given chessboard is valid or not
Given a 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 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 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.
Psedocode:
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;
}
Below is the implemantation of this approach:
C++
#include <bits/stdc++.h> using namespace std; #define MAX 2 // Check if the given chess board is valid or not. bool isValid(int c[][MAX], int n) { int X[] = { 0, -1, 0, 1 }; int Y[] = { 1, 0, -1, 0 }; bool isValid = true; // Traversing each cell of the chess board. for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { // for each adjacent cells for (int k = 0; k < 4; k++) { int newX = i + X[k]; int newY = j + Y[k]; // checking if they have different color if (newX < n && newY < n && newX >= 0 && newY >= 0 && c[newX][newY] == c[i][j]) { isValid = false; } } } } return isValid; } // Driven Program int main() { int n = 2; int c[2][2] = { { 1, 0 }, { 0, 1 } }; (isValid(c, n)) ? (cout << "Yes") : (cout << "No"); return 0; } |
Java
// Check if the given chess // board is valid or not class GFG { static int MAX = 2; static boolean isValid(int c[][], int n) { int X[] = { 0, -1, 0, 1 }; int Y[] = { 1, 0, -1, 0 }; boolean isValid = true; // Traversing each cell // of the chess board. for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { // for each adjacent cells for (int k = 0; k < 4; k++) { int newX = i + X[k]; int newY = j + Y[k]; // checking if they have // different color if (newX < n && newY < n && newX >= 0 && newY >= 0 && c[newX][newY] == c[i][j]) { isValid = false; } } } } return isValid; } // Driver Code public static void main(String[] args) { int n = 2; int[][] c = {{ 1, 0 }, { 0, 1 }}; if (isValid(c, n)) System.out.println("Yes"); else System.out.println("No"); } } // This code is contributed by ChitraNayal |
Python 3
# Python 3 Program to Check # if the given chessboard # is valid or not MAX = 2 # Check if the given chess # board is valid or not. def isValid(c, n) : X = [ 0, -1, 0, 1] Y = [ 1, 0, -1, 0] isValid = True # Traversing each cell # of the chess board. for i in range(n) : for j in range(n) : # for each adjacent cells for k in range(n) : newX = i + X[k] newY = j + Y[k] # checking if they have # different color if (newX < n and newY < n and newX >= 0 and newY >= 0 and c[newX][newY] == c[i][j]) : isValid = false return isValid # Driver Code if __name__ == "__main__" : n = 2 c = [ [1, 0], [0, 1] ] if isValid(c, n) : print("Yes") else : print("No") # This code is contributed # by ANKITRAI1 |
C#
// Check if the given chess // board is valid or not. using System; class GFG { static bool isValid(int[,] c, int n) { int[] X = { 0, -1, 0, 1 }; int[] Y = { 1, 0, -1, 0 }; bool isValid = true; // Traversing each cell // of the chess board. for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { // for each adjacent cells for (int k = 0; k < 4; k++) { int newX = i + X[k]; int newY = j + Y[k]; // checking if they have different color if (newX < n && newY < n && newX >= 0 && newY >= 0 && c[newX, newY] == c[i, j]) { isValid = false; } } } } return isValid; } // Driver Code public static void Main() { int n = 2; int[,] c = {{ 1, 0 }, { 0, 1 }}; if (isValid(c, n)) Console.Write("Yes"); else Console.Write("No"); } } // This code is contributed by ChitraNayal |
PHP
<?php // Check if the given chess // board is valid or not. function isValid(&$c, $n) { $X = array( 0, -1, 0, 1 ); $Y = array( 1, 0, -1, 0 ); $isValid = true; // Traversing each cell // of the chess board. for ($i = 0; $i < $n; $i++) { for ($j = 0; $j < $n; $j++) { // for each adjacent cells for ($k = 0; $k < 4; $k++) { $newX = $i + $X[$k]; $newY = $j + $Y[$k]; // checking if they have different color if ($newX < $n && $newY < $n && $newX >= 0 && $newY >= 0 && $c[$newX][$newY] == $c[$i][$j]) { $isValid = false; } } } } return $isValid; } // Driver Code $n = 2; $c = array(array(1, 0), array(0, 1)); echo (isValid($c, $n)) ? "Yes" : "No"; // This code is contributed by ChitraNayal ?> |
Output :
Yes
Recommended Posts:
- Check if a king can move a valid move or not when N nights are there in a modified chessboard
- Check if a Queen can attack a given cell on chessboard
- Check whether triangle is valid or not if sides are given
- Check if a + b = c is valid after removing all zeroes from a, b and c
- Program to check if a date is valid or not
- Check if given Sudoku board configuration is valid or not
- Maximum bishops that can be placed on N*N chessboard
- Maximum non-attacking Knights that can be placed on an N*M Chessboard
- Probability of Knight to remain in the chessboard
- Number of blocks in a chessboard a knight can move to in exactly k moves
- Total position where king can reach on a chessboard in exactly M moves
- Total position where king can reach on a chessboard in exactly M moves | Set 2
- Minimum Cuts can be made in the Chessboard such that it is not divided into 2 parts
- Minimum number of Parentheses to be added to make it valid
- Chessboard Pawn-Pawn game
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



