




Sponsored
Sponsored
In this approach, we use a 3x3 board to simulate the mechanics of a Tic Tac Toe game. For every move made, we place the respective player's symbol ('X' for player A and 'O' for player B) on the board. After each move, we check if this move results in a win for any player by verifying if there are three matching symbols in any row, column, or diagonal. If a win is found, that player is declared the winner and the game ends. If all moves are exhausted without a winner, the game is declared a draw. If there are remaining moves, the game is pending.
Time Complexity: O(n), where n is the length of the moves. In the worst case, it checks 8 winning conditions for all 9 moves.
Space Complexity: O(1), since the board is of fixed size (3x3).
1#include <stdio.h>
2#include <string.h>
3
4const char* tictactoe(int moves[][2], int movesSize) {
5    char board[3][3];
6    memset(board, ' ', sizeof(board));
7    int currentPlayer = 0;
8    char symbols[2] = {'X', 'O'};
9    for (int i = 0; i < movesSize; ++i) {
10        int row = moves[i][0];
11        int col = moves[i][1];
12        board[row][col] = symbols[currentPlayer];
13        currentPlayer = 1 - currentPlayer;
14    }
15    for (int i = 0; i < 3; ++i) {
16        if (board[i][0] != ' ' && board[i][0] == board[i][1] && board[i][1] == board[i][2])
17            return board[i][0] == 'X' ? "A" : "B";
18        if (board[0][i] != ' ' && board[0][i] == board[1][i] && board[1][i] == board[2][i])
19            return board[0][i] == 'X' ? "A" : "B";
20    }
21    if (board[0][0] != ' ' && board[0][0] == board[1][1] && board[1][1] == board[2][2])
22        return board[0][0] == 'X' ? "A" : "B";
23    if (board[0][2] != ' ' && board[0][2] == board[1][1] && board[1][1] == board[2][0])
24        return board[0][2] == 'X' ? "A" : "B";
25    return movesSize == 9 ? "Draw" : "Pending";
26}
27
28int main() {
29    int moves[][2] = {{0,0},{2,0},{1,1},{2,1},{2,2}};
30    const char* result = tictactoe(moves, sizeof(moves) / sizeof(moves[0]));
31    printf("%s\n", result);
32    return 0;
33}
34First, we initialize the board to be empty. We iterate through the given moves, alternately placing 'X' and 'O' based on the current player. After placing a move, we check all possible win conditions (rows, columns, diagonals) to see if the last move resulted in a win. If no winner is found, we check if all moves have been placed to determine if the game is a draw or if it's still pending.
Instead of simulating the grid itself, this approach tracks the status of each row, column, and the two diagonals using counters. Each player will have their counters, incrementing or decrementing upon making a move. The first instance a counter reaches 3 or -3 indicates a player has won the game. This method significantly reduces the need to check the grid extensively for each move while still adhering to the rules of Tic Tac Toe.
Time Complexity: O(n), where n is the number of moves.
Space Complexity: O(1), as only fixed-size counters are involved.
This solution uses integer counters to track the influence of moves on the board. Each player's move updates these counters; the goal is to detect a completion of three moves in any direction (horizontally, vertically, or diagonally). This approach efficiently determines the winner without maintaining the entire board's state.