Problema
A 3×3 board is given as three strings of X, O and . (empty). A player wins with three of their marks in a row, column or diagonal. Boards never have two winners.
Implement tic_tac_toe_winner(board).
Input
board: Three rows of three characters each.
Output
Return "X" or "O" for the winner, "Draw" if the board is full with no winner, or "Pending" if empty cells remain and nobody has won.
Vincoli
board has exactly 3 rows of length 3
Esempi
Input:
board = ["XOX", ".X.", "O.X"]
Output atteso:
expected = "X"
Input:
std::vector<std::string> board = {"XOX", ".X.", "O.X"};
Output atteso:
std::string expected = "X";
Input:
string[] board = new string[] {"XOX", ".X.", "O.X"};
Output atteso:
string expected = "X";
Spiegazione
X holds the diagonal from top-left to bottom-right, so X wins.
Input:
board = ["XOX", "XOO", "OXX"]
Output atteso:
expected = "Draw"
Input:
std::vector<std::string> board = {"XOX", "XOO", "OXX"};
Output atteso:
std::string expected = "Draw";
Input:
string[] board = new string[] {"XOX", "XOO", "OXX"};
Output atteso:
string expected = "Draw";
Spiegazione
The board is full and no line has three of a kind: Draw.
Come affrontarlo
Prova prima da solo, poi apri un suggerimento alla volta.
Suggerimento 1
Controlla ogni riga, colonna e le due diagonali per tre segni identici non vuoti.
Suggerimento 2
Tieni traccia di eventuali celle vuote mentre scansioni la griglia una sola volta.
Suggerimento 3
Ricorda che una griglia piena senza vincitore è un pareggio, altrimenti è in sospeso.
Complessità attesa
tempo O(1), spazio aggiuntivo O(1)
Riferimenti
Questa sfida è adattata per CodeUtility. Segui la traccia e i vincoli sopra.