@@ -85,45 +85,35 @@ toe.move(2, 1, 1); -> 函数返回 1 (此时,玩家 1 赢得了该场比赛
85
85
86
86
<!-- solution:start -->
87
87
88
- ### 方法一
88
+ ### 方法一:计数
89
+
90
+ 我们可以使用一个长度为 $n \times 2 + 2$ 的数组来记录每个玩家在每一行、每一列、两条对角线上的棋子数。我们需要两个这样的数组,分别记录两个玩家的棋子数。
91
+
92
+ 当一个玩家在某一行、某一列、某一对角线上的棋子数等于 $n$ 时,该玩家获胜。
93
+
94
+ 时间复杂度方面,每次落子的时间复杂度为 $O(1)$。空间复杂度为 $O(n)$,其中 $n$ 为棋盘的边长。
89
95
90
96
<!-- tabs:start -->
91
97
92
98
#### Python3
93
99
94
100
``` python
95
101
class TicTacToe :
102
+
96
103
def __init__ (self , n : int ):
97
- """
98
- Initialize your data structure here.
99
- """
100
104
self .n = n
101
- self .counter = [[ 0 ] * ((n << 1 ) + 2 ) for _ in range ( 2 )]
105
+ self .cnt = [defaultdict( int ), defaultdict( int )]
102
106
103
107
def move (self , row : int , col : int , player : int ) -> int :
104
- """
105
- Player {player} makes a move at ({row}, {col}).
106
- @param row The row of the board.
107
- @param col The column of the board.
108
- @param player The player, can be either 1 or 2.
109
- @return The current winning condition, can be either:
110
- 0: No one wins.
111
- 1: Player 1 wins.
112
- 2: Player 2 wins.
113
- """
108
+ cur = self .cnt[player - 1 ]
114
109
n = self .n
115
- self .counter[player - 1 ] [row] += 1
116
- self .counter[player - 1 ][col + n ] += 1
110
+ cur [row] += 1
111
+ cur[n + col ] += 1
117
112
if row == col:
118
- self .counter[player - 1 ] [n << 1 ] += 1
113
+ cur [n << 1 ] += 1
119
114
if row + col == n - 1 :
120
- self .counter[player - 1 ][(n << 1 ) + 1 ] += 1
121
- if (
122
- self .counter[player - 1 ][row] == n
123
- or self .counter[player - 1 ][col + n] == n
124
- or self .counter[player - 1 ][n << 1 ] == n
125
- or self .counter[player - 1 ][(n << 1 ) + 1 ] == n
126
- ):
115
+ cur[n << 1 | 1 ] += 1
116
+ if any (cur[i] == n for i in (row, n + col, n << 1 , n << 1 | 1 )):
127
117
return player
128
118
return 0
129
119
@@ -138,35 +128,24 @@ class TicTacToe:
138
128
``` java
139
129
class TicTacToe {
140
130
private int n;
141
- private int [][] counter ;
131
+ private int [][] cnt ;
142
132
143
- /* * Initialize your data structure here. */
144
133
public TicTacToe (int n ) {
145
- counter = new int [2 ][(n << 1 ) + 2 ];
146
134
this . n = n;
135
+ cnt = new int [2 ][(n << 1 ) + 2 ];
147
136
}
148
137
149
- /**
150
- Player {player} makes a move at ({row}, {col}).
151
- @param row The row of the board.
152
- @param col The column of the board.
153
- @param player The player, can be either 1 or 2.
154
- @return The current winning condition, can be either:
155
- 0: No one wins.
156
- 1: Player 1 wins.
157
- 2: Player 2 wins.
158
- */
159
138
public int move (int row , int col , int player ) {
160
- counter[player - 1 ][row] += 1 ;
161
- counter[player - 1 ][col + n] += 1 ;
139
+ int [] cur = cnt[player - 1 ];
140
+ ++ cur[row];
141
+ ++ cur[n + col];
162
142
if (row == col) {
163
- counter[player - 1 ][ n << 1 ] += 1 ;
143
+ ++ cur[ n << 1 ];
164
144
}
165
145
if (row + col == n - 1 ) {
166
- counter[player - 1 ][( n << 1 ) + 1 ] += 1 ;
146
+ ++ cur[ n << 1 | 1 ];
167
147
}
168
- if (counter[player - 1 ][row] == n || counter[player - 1 ][col + n] == n
169
- || counter[player - 1 ][n << 1 ] == n || counter[player - 1 ][(n << 1 ) + 1 ] == n) {
148
+ if (cur[row] == n || cur[n + col] == n || cur[n << 1 ] == n || cur[n << 1 | 1 ] == n) {
170
149
return player;
171
150
}
172
151
return 0 ;
@@ -180,6 +159,124 @@ class TicTacToe {
180
159
*/
181
160
```
182
161
162
+ #### C++
163
+
164
+ ``` cpp
165
+ class TicTacToe {
166
+ private:
167
+ int n;
168
+ vector<vector<int >> cnt;
169
+
170
+ public:
171
+ TicTacToe(int n)
172
+ : n(n)
173
+ , cnt(2, vector<int >((n << 1) + 2, 0)) {
174
+ }
175
+
176
+ int move(int row, int col, int player) {
177
+ vector<int>& cur = cnt[player - 1];
178
+ ++cur[row];
179
+ ++cur[n + col];
180
+ if (row == col) {
181
+ ++cur[n << 1];
182
+ }
183
+ if (row + col == n - 1 ) {
184
+ ++cur[n << 1 | 1];
185
+ }
186
+ if (cur[row] == n || cur[n + col] == n || cur[n << 1] == n || cur[n << 1 | 1] == n) {
187
+ return player;
188
+ }
189
+ return 0;
190
+ }
191
+ };
192
+
193
+ /* *
194
+ * Your TicTacToe object will be instantiated and called as such:
195
+ * TicTacToe* obj = new TicTacToe(n);
196
+ * int param_1 = obj->move(row,col,player);
197
+ */
198
+ ```
199
+
200
+ #### Go
201
+
202
+ ``` go
203
+ type TicTacToe struct {
204
+ n int
205
+ cnt [][]int
206
+ }
207
+
208
+ func Constructor (n int ) TicTacToe {
209
+ cnt := make ([][]int , 2 )
210
+ for i := range cnt {
211
+ cnt[i] = make ([]int , (n<<1 )+2 )
212
+ }
213
+ return TicTacToe{n, cnt}
214
+ }
215
+
216
+ func (this *TicTacToe ) Move (row int , col int , player int ) int {
217
+ cur := this.cnt [player-1 ]
218
+ cur[row]++
219
+ cur[this.n +col]++
220
+ if row == col {
221
+ cur[this.n <<1 ]++
222
+ }
223
+ if row+col == this.n -1 {
224
+ cur[this.n <<1 |1 ]++
225
+ }
226
+ if cur[row] == this.n || cur[this.n +col] == this.n || cur[this.n <<1 ] == this.n || cur[this.n <<1 |1 ] == this.n {
227
+ return player
228
+ }
229
+ return 0
230
+ }
231
+
232
+ /* *
233
+ * Your TicTacToe object will be instantiated and called as such:
234
+ * obj := Constructor(n);
235
+ * param_1 := obj.Move(row,col,player);
236
+ */
237
+ ```
238
+
239
+ #### TypeScript
240
+
241
+ ``` ts
242
+ class TicTacToe {
243
+ private n: number ;
244
+ private cnt: number [][];
245
+
246
+ constructor (n : number ) {
247
+ this .n = n ;
248
+ this .cnt = [Array ((n << 1 ) + 2 ).fill (0 ), Array ((n << 1 ) + 2 ).fill (0 )];
249
+ }
250
+
251
+ move(row : number , col : number , player : number ): number {
252
+ const cur = this .cnt [player - 1 ];
253
+ cur [row ]++ ;
254
+ cur [this .n + col ]++ ;
255
+ if (row === col ) {
256
+ cur [this .n << 1 ]++ ;
257
+ }
258
+ if (row + col === this .n - 1 ) {
259
+ cur [(this .n << 1 ) | 1 ]++ ;
260
+ }
261
+ if (
262
+ cur [row ] === this .n ||
263
+ cur [this .n + col ] === this .n ||
264
+ cur [this .n << 1 ] === this .n ||
265
+ cur [(this .n << 1 ) | 1 ] === this .n
266
+ ) {
267
+ return player ;
268
+ }
269
+ return 0 ;
270
+ }
271
+ }
272
+
273
+ /**
274
+ * Your TicTacToe object will be instantiated and called as such:
275
+ * var obj = new TicTacToe(n)
276
+ * var param_1 = obj.move(row,col,player)
277
+ */
278
+ ```
279
+
183
280
<!-- tabs:end -->
184
281
185
282
<!-- solution:end -->
0 commit comments