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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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?

Hint:

  1. Could you trade extra space such that move() operation can be done in O(1)?
  2. You need two arrays: int rows[n], int cols[n], plus two variables: diagonal, anti_diagonal.

一盘棋,要赢的话,要任意行,列,斜线都是相同的棋子才可以。总共有2n + 2总赢法。行数+列数+正斜线+反斜线。

用一个长为2n * 2的数组,来统计行,列,斜线的棋子出现次数。counter[0, n - 1] 统计行,counter[n, 2n - 1] 统计列,counter[2n]为正斜线,counter[2n + 1]为反斜线。player1下棋,counter + 1, player2下棋, counter - 1。

当下完一步棋的时候,检查当前行,列,或斜线中是否有一个数的绝对值为n. 为n的时候说明player1获胜,为-n的时候说明player2获胜。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
public class TicTacToe {
int[] counter;
int n;
public TicTacToe(int n) {
counter = new int[2 * n + 2];
this.n = n;
}
public int move(int row, int col, int player) {
if (player == 1) {
set(row, col, 1);
} else {
set(row, col, -1);
}
int c = player == 1 ? n : -n;
if (counter[row] == c || counter[col + n] == c || counter[2 * n] == c || counter[2 * n + 1] == c)
return player;
return 0;
}
private void set(int row, int col, int diff) {
counter[row] += diff;
counter[col + n] += diff;
if (row == col) {
counter[2 * n] += diff;
}
if (row + col == n - 1) {
counter[2 * n + 1] += diff;
}
}
}

下面是Discuss里看到的最快的做法,虽然比较浪费空间,但是速度很快

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class TicTacToe {
int[][] count;
public TicTacToe(int n) {
count = new int[6 * n][3];
}
public int move(int row, int col, int player) {
int n = count.length / 6;
for (int x : new int[]{row, n + col, 2 * n + row + col, 5 * n + row - col})
if (++count[x][player] == n)
return player;
return 0;
}
}

Ref: https://discuss.leetcode.com/topic/44592/7-8-lines-o-1-java-python