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

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

<p>Given a 2D integer array <code>moves</code> where <code>moves[i] = [row<sub>i</sub>, col<sub>i</sub>]</code> indicates that the <code>i<sup>th</sup></code> move will be played on <code>grid[row<sub>i</sub>][col<sub>i</sub>]</code>. return <em>the winner of the game if it exists</em> (<code>A</code> or <code>B</code>). In case the game ends in a draw return <code>&quot;Draw&quot;</code>. If there are still movements to play return <code>&quot;Pending&quot;</code>.</p>

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

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/09/22/xo1-grid.jpg" style="width: 244px; height: 245px;" />
<pre>
<strong>Input:</strong> moves = [[0,0],[2,0],[1,1],[2,1],[2,2]]
<strong>Output:</strong> &quot;A&quot;
<strong>Explanation:</strong> A wins, they always play first.
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/09/22/xo2-grid.jpg" style="width: 244px; height: 245px;" />
<pre>
<strong>Input:</strong> moves = [[0,0],[1,1],[0,1],[0,2],[1,0],[2,0]]
<strong>Output:</strong> &quot;B&quot;
<strong>Explanation:</strong> B wins.
</pre>

<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/09/22/xo3-grid.jpg" style="width: 244px; height: 245px;" />
<pre>
<strong>Input:</strong> moves = [[0,0],[1,1],[2,0],[1,0],[1,2],[2,1],[0,1],[0,2],[2,2]]
<strong>Output:</strong> &quot;Draw&quot;
<strong>Explanation:</strong> The game ends in a draw since there are no moves to make.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= moves.length &lt;= 9</code></li>
	<li><code>moves[i].length == 2</code></li>
	<li><code>0 &lt;= row<sub>i</sub>, col<sub>i</sub> &lt;= 2</code></li>
	<li>There are no repeated elements on <code>moves</code>.</li>
	<li><code>moves</code> follow the rules of tic tac toe.</li>
</ul>

class Solution {
public:
    string tictactoe(vector<vector<int>>& moves) {        
        int winner = checkWinner(moves);
        if (winner == 1 || winner == 2) { 
            return winner == 1 ? "A" : "B";
        }

        if (moves.size() == 9) {
            return "Draw";
        }
        return "Pending";        
    }

    int checkWinner(vector<vector<int>>& moves) {
        vector<vector<char>> grid(3, vector<char> (3, ' '));
        for (int i = 0; i < moves.size(); ++i) {
            vector<int> pos = moves[i];
            if (i % 2 == 0)
                grid[pos[0]][pos[1]] = 'X';
            else 
                grid[pos[0]][pos[1]] = 'o';
        }

        // check row:
        for (int row = 0; row < 3; row++) {
            if (grid[row][0] == ' ' || grid[row][1] == ' ' || grid[row][2] == ' ')
                continue;
            if (grid[row][0] == grid[row][1] && grid[row][1] == grid[row][2])
                return grid[row][0] == 'X' ? 1 : 2;
        }

        // check col:
        for (int col = 0; col < 3; col++) {
            if (grid[0][col] == ' ' || grid[1][col] == ' ' || grid[2][col] == ' ')
                continue;
            if (grid[0][col] == grid[1][col] && grid[1][col] == grid[2][col])
                return grid[0][col] == 'X' ? 1 : 2;
        }

        // check diagron:
        if (grid[0][2] == 'X' && grid[1][1] == 'X' && grid[2][0] == 'X')
            return 1;
        if (grid[0][2] == 'o' && grid[1][1] == 'o' && grid[2][0] == 'o')
            return 2;
        if (grid[0][0] == 'X' && grid[1][1] == 'X' && grid[2][2] == 'X')
            return 1;
        if (grid[0][0] == 'o' && grid[1][1] == 'o' && grid[2][2] == 'o')
            return 2;
        return 0;

    }


};
10300