LeetCode //C - 1275. Find Winner on a Tic Tac Toe Game

1275. Find Winner on a Tic Tac Toe Game

Tic-tac-toe is played by two players A and B on a 3 x 3 grid. The rules of Tic-Tac-Toe are:

  • Players take turns placing characters into empty squares ' '.
  • The first player A always places 'X' characters, while the second player B always places 'O' characters.
  • 'X' and 'O' characters are always placed into empty squares, never on filled ones.
  • The game ends when there are three of the same (non-empty) character filling any row, column, or diagonal.
  • The game also ends if all squares are non-empty.
  • No more moves can be played if the game is over.

Given a 2D integer array moves where m o v e s i = r o w i , c o l i movesi = row_i, col_i movesi=rowi,coli indicates that the i t h i^{th} ith move will be played on g r i d r o w i c o l i gridrow_icol_i gridrowicoli. return the winner of the game if it exists (A or B). In case the game ends in a draw return "Draw". If there are still movements to play return "Pending".

You can assume that moves is valid (i.e., it follows the rules of Tic-Tac-Toe), the grid is initially empty, and A will play first.

Example 1:

Input: moves = \[0,0,2,0,1,1,2,1,2,2]

Output: "A"

Explanation: A wins, they always play first.

Example 2:

Input: moves = \[0,0,1,1,0,1,0,2,1,0,2,0]

Output: "B"

Explanation: TB wins.

Example 3:

Input: moves = \[0,0,1,1,2,0,1,0,1,2,2,1,0,1,0,2,2,2]

Output: "Draw"

Explanation: The game ends in a draw since there are no moves to make.

Constraints:
  • 1 <= moves.length <= 9
  • movesi.length == 2
  • 0 < = r o w i , c o l i < = 2 0 <= row_i, col_i <= 2 0<=rowi,coli<=2
  • There are no repeated elements on moves.
  • moves follow the rules of tic tac toe.

From: LeetCode

Link: 1275. Find Winner on a Tic Tac Toe Game


Solution:

Ideas:

put A as 1, B as -1.

If any row, column, or diagonal sums to 3, A wins.

If it sums to -3, B wins.

Code:
c 复制代码
char* tictactoe(int** moves, int movesSize, int* movesColSize) {
    int board[3][3] = {0}; // A = 1, B = -1

    for (int i = 0; i < movesSize; i++) {
        int r = moves[i][0];
        int c = moves[i][1];

        board[r][c] = (i % 2 == 0) ? 1 : -1;
    }

    for (int i = 0; i < 3; i++) {
        int row = board[i][0] + board[i][1] + board[i][2];
        int col = board[0][i] + board[1][i] + board[2][i];

        if (row == 3 || col == 3) return "A";
        if (row == -3 || col == -3) return "B";
    }

    int diag1 = board[0][0] + board[1][1] + board[2][2];
    int diag2 = board[0][2] + board[1][1] + board[2][0];

    if (diag1 == 3 || diag2 == 3) return "A";
    if (diag1 == -3 || diag2 == -3) return "B";

    if (movesSize == 9) return "Draw";
    return "Pending";
}
相关推荐
Pniubi8 小时前
力扣54.螺旋矩阵
算法·leetcode·矩阵
Zwawa9 小时前
用两个按键做一个OLED菜单:先把选择和执行分开
c语言·单片机·嵌入式
choumou_M11 小时前
算法_1:由浅入深学习快速排序QuickSort
学习·算法·排序算法
Coder_Shenshen11 小时前
罗克韦尔ACD工程归档离线变量树解析
算法·c#
心仪久了会沦陷13 小时前
C语言数据结构入门——树与二叉树详解:概念、堆实现与链式遍历
c语言·数据结构·算法
小此方13 小时前
[算法实战论:动态规划]背包问题概念,01不满背包问题,01满背包问题,背包问题的滚动数组空间优化
算法·动态规划
悠仁さん14 小时前
【C++】哈希表
c++·算法·散列表
地平线开发者15 小时前
点云算法整理与常用计算模块:从数据到模型,再到工程与量化
算法·自动驾驶
Severus_black16 小时前
【C++初阶】list使用及相关实现,思路顺滑流畅!
c语言·数据结构·c++·stl·list
武子康17 小时前
H3-World 为何在加载 LoRA 前,先检查导入的代码?
人工智能·深度学习·算法