Tutte le sfide
Media 3 risolte 60.0% di accettazione

Tic-Tac-Toe Winner

Decide the result of a tic-tac-toe board.

Cosa imparerai

  • Check rows, columns and diagonals
  • Detect a draw or an unfinished game
  • Keep grid checks readable

Problema

Mostra traduzione

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

Esempio 1
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.

Esempio 2
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

Check every row, column, and the two diagonals for three identical non-empty marks.

Suggerimento 2

Track whether any empty cells remain while scanning the board once.

Suggerimento 3

Remember that a full board with no winner is a draw, otherwise it is pending.

Complessità attesa

O(1) time, O(1) extra space

Riferimenti

Questa sfida è adattata per CodeUtility. Segui la traccia e i vincoli sopra.

La migliore soluzione superata di ogni programmatore. Le più veloci, leggere e meglio valutate stanno in alto.

# Programmatore Linguaggio Tempo Memoria Voto dell'IA Soluzione
1 Н"Никита "Programmer" Python 947 ms - 5/5 Vedi il codice
2 LMLaura Mahlow Python 975 ms - 2/5 Vedi il codice
3 PKPornchai Kaewkrabil Go 1,357 ms - 4/5 Vedi il codice

Impara dagli altri

Soluzioni della community

Attenzione spoiler: 3 soluzioni complete sotto Prova prima a risolverla da solo. Puoi comunque sbirciare il codice degli altri sviluppatori e le revisioni dell'IA. Accedi e risolvi la sfida per partecipare alla discussione.