68
68
69
69
<!-- solution:start -->
70
70
71
- ### 方法一
71
+ ### 方法一:枚举
72
+
73
+ 我们枚举所有可能的方向,对于每个方向 $(a, b)$,我们从 $(\textit{rMove}, \textit{cMove})$ 出发,用一个变量 $\textit{cnt}$ 记录我们走过的格子数,如果我们在走的过程中遇到了颜色为 $\textit{color}$ 的格子,且 $\textit{cnt} > 1$,那么我们就找到了一个好线段,返回 $\text{true}$。
74
+
75
+ 枚举结束后,如果我们没有找到任何好线段,那么返回 $\text{false}$。
76
+
77
+ 时间复杂度 $O(m + n)$,其中 $m$ 为 $\textit{board}$ 的行数,而 $n$ 为 $\textit{board}$ 的列数,本题中 $m = n = 8$。空间复杂度 $O(1)$。
72
78
73
79
<!-- tabs:start -->
74
80
@@ -79,44 +85,44 @@ class Solution:
79
85
def checkMove (
80
86
self , board : List[List[str ]], rMove : int , cMove : int , color : str
81
87
) -> bool :
82
- dirs = [(1 , 0 ), (0 , 1 ), (- 1 , 0 ), (0 , - 1 ), (1 , 1 ), (1 , - 1 ), (- 1 , 1 ), (- 1 , - 1 )]
83
- n = 8
84
- for a, b in dirs:
85
- i, j = rMove, cMove
86
- t = 0
87
- while 0 <= i + a < n and 0 <= j + b < n:
88
- t += 1
89
- i, j = i + a, j + b
90
- if board[i][j] in [' .' , color]:
91
- break
92
- if board[i][j] == color and t > 1 :
93
- return True
88
+ for a in range (- 1 , 2 ):
89
+ for b in range (- 1 , 2 ):
90
+ if a == 0 and b == 0 :
91
+ continue
92
+ i, j = rMove, cMove
93
+ cnt = 0
94
+ while 0 <= i + a < 8 and 0 <= j + b < 8 :
95
+ cnt += 1
96
+ i, j = i + a, j + b
97
+ if cnt > 1 and board[i][j] == color:
98
+ return True
99
+ if board[i][j] in (color, " ." ):
100
+ break
94
101
return False
95
102
```
96
103
97
104
#### Java
98
105
99
106
``` java
100
107
class Solution {
101
- private static final int [][] DIRS
102
- = {{1 , 0 }, {0 , 1 }, {- 1 , 0 }, {0 , - 1 }, {1 , 1 }, {1 , - 1 }, {- 1 , 1 }, {- 1 , - 1 }};
103
- private static final int N = 8 ;
104
-
105
108
public boolean checkMove (char [][] board , int rMove , int cMove , char color ) {
106
- for (int [] d : DIRS ) {
107
- int i = rMove, j = cMove;
108
- int t = 0 ;
109
- int a = d[0 ], b = d[1 ];
110
- while (0 <= i + a && i + a < N && 0 <= j + b && j + b < N ) {
111
- ++ t;
112
- i += a;
113
- j += b;
114
- if (board[i][j] == ' .' || board[i][j] == color) {
115
- break ;
109
+ for (int a = - 1 ; a <= 1 ; ++ a) {
110
+ for (int b = - 1 ; b <= 1 ; ++ b) {
111
+ if (a == 0 && b == 0 ) {
112
+ continue ;
113
+ }
114
+ int i = rMove, j = cMove;
115
+ int cnt = 0 ;
116
+ while (0 <= i + a && i + a < 8 && 0 <= j + b && j + b < 8 ) {
117
+ i += a;
118
+ j += b;
119
+ if (++ cnt > 1 && board[i][j] == color) {
120
+ return true ;
121
+ }
122
+ if (board[i][j] == color || board[i][j] == ' .' ) {
123
+ break ;
124
+ }
116
125
}
117
- }
118
- if (board[i][j] == color && t > 1 ) {
119
- return true ;
120
126
}
121
127
}
122
128
return false ;
@@ -129,21 +135,25 @@ class Solution {
129
135
``` cpp
130
136
class Solution {
131
137
public:
132
- vector<vector<int >> dirs = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};
133
- int n = 8;
134
-
135
138
bool checkMove(vector<vector<char >>& board, int rMove, int cMove, char color) {
136
- for (auto& d : dirs) {
137
- int a = d[0], b = d[1];
138
- int i = rMove, j = cMove;
139
- int t = 0;
140
- while (0 <= i + a && i + a < n && 0 <= j + b && j + b < n) {
141
- ++t;
142
- i += a;
143
- j += b;
144
- if (board[i][j] == '.' || board[i][j] == color) break;
139
+ for (int a = -1; a <= 1; ++a) {
140
+ for (int b = -1; b <= 1; ++b) {
141
+ if (a == 0 && b == 0) {
142
+ continue;
143
+ }
144
+ int i = rMove, j = cMove;
145
+ int cnt = 0;
146
+ while (0 <= i + a && i + a < 8 && 0 <= j + b && j + b < 8) {
147
+ i += a;
148
+ j += b;
149
+ if (++cnt > 1 && board[ i] [ j ] == color) {
150
+ return true;
151
+ }
152
+ if (board[ i] [ j ] == color || board[ i] [ j ] == '.') {
153
+ break;
154
+ }
155
+ }
145
156
}
146
- if (board[i][j] == color && t > 1 ) return true ;
147
157
}
148
158
return false;
149
159
}
@@ -154,28 +164,57 @@ public:
154
164
155
165
```go
156
166
func checkMove(board [][]byte, rMove int, cMove int, color byte) bool {
157
- dirs := [8 ][2 ]int {{1 , 0 }, {0 , 1 }, {-1 , 0 }, {0 , -1 }, {1 , 1 }, {1 , -1 }, {-1 , 1 }, {-1 , -1 }}
158
- n := 8
159
- for _ , d := range dirs {
160
- a , b := d[0 ], d[1 ]
161
- i , j := rMove, cMove
162
- t := 0
163
- for 0 <= i+a && i+a < n && 0 <= j+b && j+b < n {
164
- t++
165
- i += a
166
- j += b
167
- if board[i][j] == ' .' || board[i][j] == color {
168
- break
167
+ for a := -1; a <= 1; a++ {
168
+ for b := -1; b <= 1; b++ {
169
+ if a == 0 && b == 0 {
170
+ continue
171
+ }
172
+ i, j := rMove, cMove
173
+ cnt := 0
174
+ for 0 <= i+a && i+a < 8 && 0 <= j+b && j+b < 8 {
175
+ i += a
176
+ j += b
177
+ cnt++
178
+ if cnt > 1 && board[i][j] == color {
179
+ return true
180
+ }
181
+ if board[i][j] == color || board[i][j] == '.' {
182
+ break
183
+ }
169
184
}
170
- }
171
- if board[i][j] == color && t > 1 {
172
- return true
173
185
}
174
186
}
175
187
return false
176
188
}
177
189
```
178
190
191
+ #### TypeScript
192
+
193
+ ``` ts
194
+ function checkMove(board : string [][], rMove : number , cMove : number , color : string ): boolean {
195
+ for (let a = - 1 ; a <= 1 ; ++ a ) {
196
+ for (let b = - 1 ; b <= 1 ; ++ b ) {
197
+ if (a === 0 && b === 0 ) {
198
+ continue ;
199
+ }
200
+ let [i, j] = [rMove , cMove ];
201
+ let cnt = 0 ;
202
+ while (0 <= i + a && i + a < 8 && 0 <= j + b && j + b < 8 ) {
203
+ i += a ;
204
+ j += b ;
205
+ if (++ cnt > 1 && board [i ][j ] === color ) {
206
+ return true ;
207
+ }
208
+ if (board [i ][j ] === color || board [i ][j ] === ' .' ) {
209
+ break ;
210
+ }
211
+ }
212
+ }
213
+ }
214
+ return false ;
215
+ }
216
+ ```
217
+
179
218
<!-- tabs: end -->
180
219
181
220
<!-- solution: end -->
0 commit comments