File tree 2 files changed +123
-0
lines changed
2 files changed +123
-0
lines changed Original file line number Diff line number Diff line change @@ -292,6 +292,70 @@ func abs(x int) int {
292
292
}
293
293
```
294
294
295
+ #### Swift
296
+
297
+ ``` swift
298
+ class Solution {
299
+ private var m = 0
300
+ private var n = 0
301
+ private var chessboard: [String ] = []
302
+
303
+ func flipChess (_ chessboard : [String ]) -> Int {
304
+ self .m = chessboard.count
305
+ self .n = chessboard[0 ].count
306
+ self .chessboard = chessboard
307
+ var ans = 0
308
+
309
+ for i in 0 ..< m {
310
+ for j in 0 ..< n {
311
+ if Array (chessboard[i])[j] == " ." {
312
+ ans = max (ans, bfs (i, j))
313
+ }
314
+ }
315
+ }
316
+ return ans
317
+ }
318
+
319
+ private func bfs (_ i : Int , _ j : Int ) -> Int {
320
+ var queue: [[Int ]] = [[i, j]]
321
+ var g = chessboard.map { Array ($0 ) }
322
+ g[i][j] = " X"
323
+ var count = 0
324
+
325
+ while ! queue.isEmpty {
326
+ let p = queue.removeFirst ()
327
+ let i = p[0 ], j = p[1 ]
328
+
329
+ for a in -1 ... 1 {
330
+ for b in -1 ... 1 {
331
+ if a == 0 && b == 0 { continue }
332
+
333
+ var x = i + a, y = j + b
334
+ while x >= 0 && x < m && y >= 0 && y < n && g[x][y] == " O" {
335
+ x += a
336
+ y += b
337
+ }
338
+
339
+ if x >= 0 && x < m && y >= 0 && y < n && g[x][y] == " X" {
340
+ x -= a
341
+ y -= b
342
+ count += max (abs (x - i), abs (y - j))
343
+
344
+ while x != i || y != j {
345
+ g[x][y] = " X"
346
+ queue.append ([x, y])
347
+ x -= a
348
+ y -= b
349
+ }
350
+ }
351
+ }
352
+ }
353
+ }
354
+ return count
355
+ }
356
+ }
357
+ ```
358
+
295
359
<!-- tabs:end -->
296
360
297
361
<!-- solution:end -->
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ private var m = 0
3
+ private var n = 0
4
+ private var chessboard : [ String ] = [ ]
5
+
6
+ func flipChess( _ chessboard: [ String ] ) -> Int {
7
+ self . m = chessboard. count
8
+ self . n = chessboard [ 0 ] . count
9
+ self . chessboard = chessboard
10
+ var ans = 0
11
+
12
+ for i in 0 ..< m {
13
+ for j in 0 ..< n {
14
+ if Array ( chessboard [ i] ) [ j] == " . " {
15
+ ans = max ( ans, bfs ( i, j) )
16
+ }
17
+ }
18
+ }
19
+ return ans
20
+ }
21
+
22
+ private func bfs( _ i: Int , _ j: Int ) -> Int {
23
+ var queue : [ [ Int ] ] = [ [ i, j] ]
24
+ var g = chessboard. map { Array ( $0) }
25
+ g [ i] [ j] = " X "
26
+ var count = 0
27
+
28
+ while !queue. isEmpty {
29
+ let p = queue. removeFirst ( )
30
+ let i = p [ 0 ] , j = p [ 1 ]
31
+
32
+ for a in - 1 ... 1 {
33
+ for b in - 1 ... 1 {
34
+ if a == 0 && b == 0 { continue }
35
+
36
+ var x = i + a, y = j + b
37
+ while x >= 0 && x < m && y >= 0 && y < n && g [ x] [ y] == " O " {
38
+ x += a
39
+ y += b
40
+ }
41
+
42
+ if x >= 0 && x < m && y >= 0 && y < n && g [ x] [ y] == " X " {
43
+ x -= a
44
+ y -= b
45
+ count += max ( abs ( x - i) , abs ( y - j) )
46
+
47
+ while x != i || y != j {
48
+ g [ x] [ y] = " X "
49
+ queue. append ( [ x, y] )
50
+ x -= a
51
+ y -= b
52
+ }
53
+ }
54
+ }
55
+ }
56
+ }
57
+ return count
58
+ }
59
+ }
You can’t perform that action at this time.
0 commit comments