LeetCode 348. Design Tic-Tac-Toe

原题链接在这里:https://leetcode.com/problems/design-tic-tac-toe/

题目:

Design a Tic-tac-toe game that is played between two players on a n x n grid.

You may assume the following rules:

  1. A move is guaranteed to be valid and is placed on an empty block.
  2. Once a winning condition is reached, no more moves is allowed.
  3. A player who succeeds in placing n of their marks in a horizontal, vertical, or diagonal row wins the game.

 

Example:

Given n = 3, assume that player 1 is "X" and player 2 is "O" in the board.

TicTacToe toe = new TicTacToe(3);

toe.move(0, 0, 1); -> Returns 0 (no one wins)
|X| | |
| | | |    // Player 1 makes a move at (0, 0).
| | | |

toe.move(0, 2, 2); -> Returns 0 (no one wins)
|X| |O|
| | | |    // Player 2 makes a move at (0, 2).
| | | |

toe.move(2, 2, 1); -> Returns 0 (no one wins)
|X| |O|
| | | |    // Player 1 makes a move at (2, 2).
| | |X|

toe.move(1, 1, 2); -> Returns 0 (no one wins)
|X| |O|
| |O| |    // Player 2 makes a move at (1, 1).
| | |X|

toe.move(2, 0, 1); -> Returns 0 (no one wins)
|X| |O|
| |O| |    // Player 1 makes a move at (2, 0).
|X| |X|

toe.move(1, 0, 2); -> Returns 0 (no one wins)
|X| |O|
|O|O| |    // Player 2 makes a move at (1, 0).
|X| |X|

toe.move(2, 1, 1); -> Returns 1 (player 1 wins)
|X| |O|
|O|O| |    // Player 1 makes a move at (2, 1).
|X|X|X|

Follow up:
Could you do better than O(n2) per move() operation?

题解:

If the player wants to win after placing on current coordinate. 

Either the entire row or column is this player's number. 

If current coordinate is on diagonal or anti-diagonal, and entire diagonal is this player's number, player could win.

Thus have a row array, col array to track target. 

d1, d2 to track diagonal and anti-diagonal. Since there is only one diagonal and one anti-diagonal.

Have player one add +1, and player two add -1.

When it hits +5, player one wins. When it hits -5, player two wins.

TIme Complexity: move, O(1).

Space: O(n).

AC Java:

 1 class TicTacToe {
 2     int [] r;
 3     int [] c;
 4     int d1;
 5     int d2;
 6     int n;
 7     
 8     /** Initialize your data structure here. */
 9     public TicTacToe(int n) {
10         r = new int[n];
11         c = new int[n];
12         d1 = 0;
13         d2 = 0;
14         this.n = n;
15     }
16     
17     /** Player {player} makes a move at ({row}, {col}).
18         @param row The row of the board.
19         @param col The column of the board.
20         @param player The player, can be either 1 or 2.
21         @return The current winning condition, can be either:
22                 0: No one wins.
23                 1: Player 1 wins.
24                 2: Player 2 wins. */
25     public int move(int row, int col, int player) {
26         int toAdd = player == 1 ? 1 : -1;
27         int target = player == 1 ? n : -n;
28         
29         if(row == col){
30             d1 += toAdd;
31             if(d1 == target){
32                 return player;
33             }
34         }
35         
36         if(row + col + 1 == n){
37             d2 += toAdd;
38             if(d2 == target){
39                 return player;
40             }
41         }
42         
43         r[row] += toAdd;
44         c[col] += toAdd;
45         if(r[row] == target || c[col] == target){
46             return player;
47         }
48         
49         return 0;
50     }
51 }
52 
53 /**
54  * Your TicTacToe object will be instantiated and called as such:
55  * TicTacToe obj = new TicTacToe(n);
56  * int param_1 = obj.move(row,col,player);
57  */

 

上一篇:通过 docker images 获取 Dockerfile


下一篇:Tic-Tac-Toe with AI