문제
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
모든 행, 열, 그리고 두 대각선을 검사해 같은 비어 있지 않은 표시 3개가 있는지 확인하세요.
힌트 2
보드를 한 번 훑으면서 빈 칸이 남아 있는지도 추적합니다.
힌트 3
승자가 없고 보드가 가득 차면 무승부, 그렇지 않으면 진행 중입니다.
예상 복잡도
시간 O(1), 추가 공간 O(1)
참고 자료
이 문제는 CodeUtility에 맞게 수정되었습니다. 위의 설명과 제한 조건을 따르세요.