Skip to content

Commit 2ec66da

Browse files
authored
feat: add swift implementation to lcp problem: No.63 (#3973)
1 parent 5a62076 commit 2ec66da

File tree

2 files changed

+143
-0
lines changed

2 files changed

+143
-0
lines changed

lcp/LCP 63. 弹珠游戏/README.md

+74
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,80 @@ func ballGame(num int, plate []string) (ans [][]int) {
274274
}
275275
```
276276

277+
#### Swift
278+
279+
```swift
280+
class Solution {
281+
private var plate: [String] = []
282+
private var num: Int = 0
283+
private var m: Int = 0
284+
private var n: Int = 0
285+
private let dirs = [0, 1, 0, -1, 0]
286+
287+
func ballGame(_ num: Int, _ plate: [String]) -> [[Int]] {
288+
self.num = num
289+
self.plate = plate
290+
self.m = plate.count
291+
self.n = plate[0].count
292+
var ans: [[Int]] = []
293+
294+
for i in 1..<m - 1 {
295+
if plate[i][0] == "." && check(i, 0, 0) {
296+
ans.append([i, 0])
297+
}
298+
if plate[i][n - 1] == "." && check(i, n - 1, 2) {
299+
ans.append([i, n - 1])
300+
}
301+
}
302+
303+
for j in 1..<n - 1 {
304+
if plate[0][j] == "." && check(0, j, 1) {
305+
ans.append([0, j])
306+
}
307+
if plate[m - 1][j] == "." && check(m - 1, j, 3) {
308+
ans.append([m - 1, j])
309+
}
310+
}
311+
312+
return ans
313+
}
314+
315+
private func check(_ i: Int, _ j: Int, _ d: Int) -> Bool {
316+
var k = num
317+
var i = i, j = j, d = d
318+
319+
while plate[i][j] != "O" {
320+
if k == 0 {
321+
return false
322+
}
323+
324+
if plate[i][j] == "W" {
325+
d = (d + 3) % 4
326+
} else if plate[i][j] == "E" {
327+
d = (d + 1) % 4
328+
}
329+
330+
i += dirs[d]
331+
j += dirs[d + 1]
332+
333+
if i < 0 || i >= m || j < 0 || j >= n {
334+
return false
335+
}
336+
337+
k -= 1
338+
}
339+
340+
return true
341+
}
342+
}
343+
344+
private extension String {
345+
subscript(_ index: Int) -> Character {
346+
return self[self.index(self.startIndex, offsetBy: index)]
347+
}
348+
}
349+
```
350+
277351
<!-- tabs:end -->
278352

279353
<!-- solution:end -->
+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
class Solution {
2+
private var plate: [String] = []
3+
private var num: Int = 0
4+
private var m: Int = 0
5+
private var n: Int = 0
6+
private let dirs = [0, 1, 0, -1, 0]
7+
8+
func ballGame(_ num: Int, _ plate: [String]) -> [[Int]] {
9+
self.num = num
10+
self.plate = plate
11+
self.m = plate.count
12+
self.n = plate[0].count
13+
var ans: [[Int]] = []
14+
15+
for i in 1..<m - 1 {
16+
if plate[i][0] == "." && check(i, 0, 0) {
17+
ans.append([i, 0])
18+
}
19+
if plate[i][n - 1] == "." && check(i, n - 1, 2) {
20+
ans.append([i, n - 1])
21+
}
22+
}
23+
24+
for j in 1..<n - 1 {
25+
if plate[0][j] == "." && check(0, j, 1) {
26+
ans.append([0, j])
27+
}
28+
if plate[m - 1][j] == "." && check(m - 1, j, 3) {
29+
ans.append([m - 1, j])
30+
}
31+
}
32+
33+
return ans
34+
}
35+
36+
private func check(_ i: Int, _ j: Int, _ d: Int) -> Bool {
37+
var k = num
38+
var i = i, j = j, d = d
39+
40+
while plate[i][j] != "O" {
41+
if k == 0 {
42+
return false
43+
}
44+
45+
if plate[i][j] == "W" {
46+
d = (d + 3) % 4
47+
} else if plate[i][j] == "E" {
48+
d = (d + 1) % 4
49+
}
50+
51+
i += dirs[d]
52+
j += dirs[d + 1]
53+
54+
if i < 0 || i >= m || j < 0 || j >= n {
55+
return false
56+
}
57+
58+
k -= 1
59+
}
60+
61+
return true
62+
}
63+
}
64+
65+
private extension String {
66+
subscript(_ index: Int) -> Character {
67+
return self[self.index(self.startIndex, offsetBy: index)]
68+
}
69+
}

0 commit comments

Comments
 (0)