diff --git "a/lcp/LCP 63. \345\274\271\347\217\240\346\270\270\346\210\217/README.md" "b/lcp/LCP 63. \345\274\271\347\217\240\346\270\270\346\210\217/README.md" index 2d2823b8085a5..87edf23ce9852 100644 --- "a/lcp/LCP 63. \345\274\271\347\217\240\346\270\270\346\210\217/README.md" +++ "b/lcp/LCP 63. \345\274\271\347\217\240\346\270\270\346\210\217/README.md" @@ -274,6 +274,80 @@ func ballGame(num int, plate []string) (ans [][]int) { } ``` +#### Swift + +```swift +class Solution { + private var plate: [String] = [] + private var num: Int = 0 + private var m: Int = 0 + private var n: Int = 0 + private let dirs = [0, 1, 0, -1, 0] + + func ballGame(_ num: Int, _ plate: [String]) -> [[Int]] { + self.num = num + self.plate = plate + self.m = plate.count + self.n = plate[0].count + var ans: [[Int]] = [] + + for i in 1.. Bool { + var k = num + var i = i, j = j, d = d + + while plate[i][j] != "O" { + if k == 0 { + return false + } + + if plate[i][j] == "W" { + d = (d + 3) % 4 + } else if plate[i][j] == "E" { + d = (d + 1) % 4 + } + + i += dirs[d] + j += dirs[d + 1] + + if i < 0 || i >= m || j < 0 || j >= n { + return false + } + + k -= 1 + } + + return true + } +} + +private extension String { + subscript(_ index: Int) -> Character { + return self[self.index(self.startIndex, offsetBy: index)] + } +} +``` + diff --git "a/lcp/LCP 63. \345\274\271\347\217\240\346\270\270\346\210\217/Solution.swift" "b/lcp/LCP 63. \345\274\271\347\217\240\346\270\270\346\210\217/Solution.swift" new file mode 100644 index 0000000000000..6a9b7c2f07875 --- /dev/null +++ "b/lcp/LCP 63. \345\274\271\347\217\240\346\270\270\346\210\217/Solution.swift" @@ -0,0 +1,69 @@ +class Solution { + private var plate: [String] = [] + private var num: Int = 0 + private var m: Int = 0 + private var n: Int = 0 + private let dirs = [0, 1, 0, -1, 0] + + func ballGame(_ num: Int, _ plate: [String]) -> [[Int]] { + self.num = num + self.plate = plate + self.m = plate.count + self.n = plate[0].count + var ans: [[Int]] = [] + + for i in 1.. Bool { + var k = num + var i = i, j = j, d = d + + while plate[i][j] != "O" { + if k == 0 { + return false + } + + if plate[i][j] == "W" { + d = (d + 3) % 4 + } else if plate[i][j] == "E" { + d = (d + 1) % 4 + } + + i += dirs[d] + j += dirs[d + 1] + + if i < 0 || i >= m || j < 0 || j >= n { + return false + } + + k -= 1 + } + + return true + } +} + +private extension String { + subscript(_ index: Int) -> Character { + return self[self.index(self.startIndex, offsetBy: index)] + } +} \ No newline at end of file