|
| 1 | +<!--|This file generated by command(leetcode description); DO NOT EDIT. |--> |
| 2 | +<!--+----------------------------------------------------------------------+--> |
| 3 | +<!--|@author openset <[email protected]> |--> |
| 4 | +<!--|@link https://github.com/openset |--> |
| 5 | +<!--|@home https://github.com/openset/leetcode |--> |
| 6 | +<!--+----------------------------------------------------------------------+--> |
| 7 | + |
| 8 | +[< Previous](https://github.com/openset/leetcode/tree/master/problems/number-of-ships-in-a-rectangle "Number of Ships in a Rectangle") |
| 9 | + |
| 10 | +[Next >](https://github.com/openset/leetcode/tree/master/problems/number-of-burgers-with-no-waste-of-ingredients "Number of Burgers with No Waste of Ingredients") |
| 11 | + |
| 12 | +## [1275. Find Winner on a Tic Tac Toe Game (Easy)](https://leetcode.com/problems/find-winner-on-a-tic-tac-toe-game "找出井字棋的获胜者") |
| 13 | + |
| 14 | +<p>Tic-tac-toe is played by two players <em>A</em> and <em>B</em> on a <i>3</i> x <i>3</i> grid.</p> |
| 15 | + |
| 16 | +<p>Here are the rules of Tic-Tac-Toe:</p> |
| 17 | + |
| 18 | +<ul> |
| 19 | + <li>Players take turns placing characters into empty squares (" ").</li> |
| 20 | + <li>The first player <em>A</em> always places "X" characters, while the second player <em>B</em> always places "O" characters.</li> |
| 21 | + <li>"X" and "O" characters are always placed into empty squares, never on filled ones.</li> |
| 22 | + <li>The game ends when there are 3 of the same (non-empty) character filling any row, column, or diagonal.</li> |
| 23 | + <li>The game also ends if all squares are non-empty.</li> |
| 24 | + <li>No more moves can be played if the game is over.</li> |
| 25 | +</ul> |
| 26 | + |
| 27 | +<p>Given an array <code>moves</code> where each element is another array of size 2 corresponding to the row and column of the grid where they mark their respective character in the order in which <em>A</em> and <em>B</em> play.</p> |
| 28 | + |
| 29 | +<p>Return the winner of the game if it exists (<em>A</em> or <em>B</em>), in case the game ends in a draw return "Draw", if there are still movements to play return "Pending".</p> |
| 30 | + |
| 31 | +<p>You can assume that <code>moves</code> is <strong>valid</strong> (It follows the rules of Tic-Tac-Toe), the grid is initially empty and <em>A</em> will play <strong>first</strong>.</p> |
| 32 | + |
| 33 | +<p> </p> |
| 34 | +<p><strong>Example 1:</strong></p> |
| 35 | + |
| 36 | +<pre> |
| 37 | +<strong>Input:</strong> moves = [[0,0],[2,0],[1,1],[2,1],[2,2]] |
| 38 | +<strong>Output:</strong> "A" |
| 39 | +<strong>Explanation:</strong> "A" wins, he always plays first. |
| 40 | +"X " "X " "X " "X " "<strong>X</strong> " |
| 41 | +" " -> " " -> " X " -> " X " -> " <strong>X</strong> " |
| 42 | +" " "O " "O " "OO " "OO<strong>X</strong>" |
| 43 | +</pre> |
| 44 | + |
| 45 | +<p><strong>Example 2:</strong></p> |
| 46 | + |
| 47 | +<pre> |
| 48 | +<strong>Input:</strong> moves = [[0,0],[1,1],[0,1],[0,2],[1,0],[2,0]] |
| 49 | +<strong>Output:</strong> "B" |
| 50 | +<strong>Explanation:</strong> "B" wins. |
| 51 | +"X " "X " "XX " "XXO" "XXO" "XX<strong>O</strong>" |
| 52 | +" " -> " O " -> " O " -> " O " -> "XO " -> "X<strong>O</strong> " |
| 53 | +" " " " " " " " " " "<strong>O</strong> " |
| 54 | +</pre> |
| 55 | + |
| 56 | +<p><strong>Example 3:</strong></p> |
| 57 | + |
| 58 | +<pre> |
| 59 | +<strong>Input:</strong> moves = [[0,0],[1,1],[2,0],[1,0],[1,2],[2,1],[0,1],[0,2],[2,2]] |
| 60 | +<strong>Output:</strong> "Draw" |
| 61 | +<strong>Explanation:</strong> The game ends in a draw since there are no moves to make. |
| 62 | +"XXO" |
| 63 | +"OOX" |
| 64 | +"XOX" |
| 65 | +</pre> |
| 66 | + |
| 67 | +<p><strong>Example 4:</strong></p> |
| 68 | + |
| 69 | +<pre> |
| 70 | +<strong>Input:</strong> moves = [[0,0],[1,1]] |
| 71 | +<strong>Output:</strong> "Pending" |
| 72 | +<strong>Explanation:</strong> The game has not finished yet. |
| 73 | +"X " |
| 74 | +" O " |
| 75 | +" " |
| 76 | +</pre> |
| 77 | + |
| 78 | +<p> </p> |
| 79 | +<p><strong>Constraints:</strong></p> |
| 80 | + |
| 81 | +<ul> |
| 82 | + <li><code>1 <= moves.length <= 9</code></li> |
| 83 | + <li><code>moves[i].length == 2</code></li> |
| 84 | + <li><code>0 <= moves[i][j] <= 2</code></li> |
| 85 | + <li>There are no repeated elements on <code>moves</code>.</li> |
| 86 | + <li><code>moves</code> follow the rules of tic tac toe.</li> |
| 87 | +</ul> |
| 88 | + |
| 89 | +### Related Topics |
| 90 | + [[Array](https://github.com/openset/leetcode/tree/master/tag/array/README.md)] |
| 91 | + |
| 92 | +### Hints |
| 93 | +<details> |
| 94 | +<summary>Hint 1</summary> |
| 95 | +It's straightforward to check if A or B won or not, check for each row/column/diag if all the three are the same. |
| 96 | +</details> |
| 97 | + |
| 98 | +<details> |
| 99 | +<summary>Hint 2</summary> |
| 100 | +Then if no one wins, the game is a draw iff the board is full, i.e. moves.length = 9 otherwise is pending. |
| 101 | +</details> |
0 commit comments