class Solution:
def tictactoe(self, moves):
board = [[0]*3 for _ in range(3)]
for idx, (r, c) in enumerate(moves):
player = 1 if idx % 2 == 0 else 2 # 1 for A, 2 for B
board[r][c] = player
def check(player):
# Check rows and columns
for i in range(3):
if all(board[i][j] == player for j in range(3)):
return True
if all(board[j][i] == player for j in range(3)):
return True
# Check diagonals
if all(board[i][i] == player for i in range(3)):
return True
if all(board[i][2-i] == player for i in range(3)):
return True
return False
if check(1):
return "A"
if check(2):
return "B"
if len(moves) == 9:
return "Draw"
return "Pending"
class Solution {
public:
string tictactoe(vector<vector<int>>& moves) {
vector<vector<int>> board(3, vector<int>(3, 0));
for (int i = 0; i < moves.size(); ++i) {
int r = moves[i][0], c = moves[i][1];
int player = (i % 2 == 0) ? 1 : 2;
board[r][c] = player;
}
auto check = [&](int player) {
for (int i = 0; i < 3; ++i) {
if (board[i][0] == player && board[i][1] == player && board[i][2] == player)
return true;
if (board[0][i] == player && board[1][i] == player && board[2][i] == player)
return true;
}
if (board[0][0] == player && board[1][1] == player && board[2][2] == player)
return true;
if (board[0][2] == player && board[1][1] == player && board[2][0] == player)
return true;
return false;
};
if (check(1)) return "A";
if (check(2)) return "B";
if (moves.size() == 9) return "Draw";
return "Pending";
}
};
class Solution {
public String tictactoe(int[][] moves) {
int[][] board = new int[3][3];
for (int i = 0; i < moves.length; i++) {
int r = moves[i][0], c = moves[i][1];
int player = (i % 2 == 0) ? 1 : 2;
board[r][c] = player;
}
if (check(board, 1)) return "A";
if (check(board, 2)) return "B";
if (moves.length == 9) return "Draw";
return "Pending";
}
private boolean check(int[][] board, int player) {
for (int i = 0; i < 3; i++) {
if (board[i][0] == player && board[i][1] == player && board[i][2] == player)
return true;
if (board[0][i] == player && board[1][i] == player && board[2][i] == player)
return true;
}
if (board[0][0] == player && board[1][1] == player && board[2][2] == player)
return true;
if (board[0][2] == player && board[1][1] == player && board[2][0] == player)
return true;
return false;
}
}
var tictactoe = function(moves) {
const board = Array.from({length: 3}, () => Array(3).fill(0));
for (let i = 0; i < moves.length; i++) {
const [r, c] = moves[i];
const player = i % 2 === 0 ? 1 : 2;
board[r][c] = player;
}
const check = (player) => {
for (let i = 0; i < 3; i++) {
if (board[i][0] === player && board[i][1] === player && board[i][2] === player)
return true;
if (board[0][i] === player && board[1][i] === player && board[2][i] === player)
return true;
}
if (board[0][0] === player && board[1][1] === player && board[2][2] === player)
return true;
if (board[0][2] === player && board[1][1] === player && board[2][0] === player)
return true;
return false;
}
if (check(1)) return "A";
if (check(2)) return "B";
if (moves.length === 9) return "Draw";
return "Pending";
};
[row, col]
, and the moves are made in order, alternating between Player A and Player B. Player A always moves first. The game can end in one of the following ways:
moves
of length at most 9, where each element is a list [row, col]
representing a move. Each cell is used at most once, and all moves are valid.
moves
list. For each move, determine which player's turn it is (Player A on even indices, Player B on odd indices) and mark the corresponding cell in the board.moves = [[0,0],[2,0],[1,1],[2,1],[2,2]]Step-by-step:
A | | --+---+-- | A | --+---+-- B | B | ANow, check for a winner: