問題
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.
制約
board has exactly 3 rows of length 3
例
入力:
board = ["XOX", ".X.", "O.X"]
期待される出力:
expected = "X"
入力:
std::vector<std::string> board = {"XOX", ".X.", "O.X"};
期待される出力:
std::string expected = "X";
入力:
string[] board = new string[] {"XOX", ".X.", "O.X"};
期待される出力:
string expected = "X";
解説
X holds the diagonal from top-left to bottom-right, so X wins.
入力:
board = ["XOX", "XOO", "OXX"]
期待される出力:
expected = "Draw"
入力:
std::vector<std::string> board = {"XOX", "XOO", "OXX"};
期待される出力:
std::string expected = "Draw";
入力:
string[] board = new string[] {"XOX", "XOO", "OXX"};
期待される出力:
string expected = "Draw";
解説
The board is full and no line has three of a kind: Draw.
解き方のヒント
まずは自分で考えてから、ヒントを 1 つずつ開いてみましょう。
ヒント 1
各行、各列、2本の対角線を調べて、同じ非空マークが3つ並んでいるか確認します。
ヒント 2
盤面を1回走査しながら、空きマスが残っているかも追跡します。
ヒント 3
勝者なしで盤面が埋まっていれば引き分け、そうでなければ保留です。
目安の計算量
時間 O(1)、追加メモリ O(1)
参考資料
この課題はCodeUtility用に調整されています。上記の説明と制約に従ってください。