Problème
Use a board whose top-left square is white. Rows and columns start at 0 at the top left. Adjacent squares alternate colors. Return empty text for a position outside the board.
Implement chess_square_color(column, row).
Input
column: The column, counted from the left starting at 0.
row: The row, counted from the top starting at 0.
Output
Return white, black, or an empty string for invalid coordinates.
Contraintes
−10 ≤ column, row ≤ 20. Valid board coordinates are 0 through 7.
Exemples
Entrée :
column = 2
row = 3
Sortie attendue :
expected = "black"
Entrée :
long long column = 2;
long long row = 3;
Sortie attendue :
std::string expected = "black";
Entrée :
long column = 2;
long row = 3;
Sortie attendue :
string expected = "black";
Explication
Five steps from the white corner change color an odd number of times.
Entrée :
column = 8
row = 0
Sortie attendue :
expected = ""
Entrée :
long long column = 8;
long long row = 0;
Sortie attendue :
std::string expected = "";
Entrée :
long column = 8;
long row = 0;
Sortie attendue :
string expected = "";
Explication
Column 8 is outside the valid range 0 through 7.
Comment l'aborder
Essayez d'abord seul, puis ouvrez les indices un par un.
Indice 1
Square color alternates with moves across rows and columns, so only the coordinate pattern matters.
Indice 2
A parity check on row and column can tell you which color to return.
Indice 3
Return empty text immediately when either coordinate is outside the 0 to 7 board range.
Complexité attendue
O(1) time, O(1) extra space
Références
Ce défi a été adapté pour CodeUtility. Suivez l'énoncé et les contraintes ci-dessus.