Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2dd558c

Browse files
feat: add swift implementation to lcp problem: No.56 (#3931)
1 parent cc4815a commit 2dd558c

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

‎lcp/LCP 56. 信物传送/README.md‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,57 @@ function conveyorBelt(matrix: string[], start: number[], end: number[]): number
271271
}
272272
```
273273

274+
#### Swift
275+
276+
```swift
277+
class Solution {
278+
func conveyorBelt(_ matrix: [String], _ start: [Int], _ end: [Int]) -> Int {
279+
let directions: [(Int, Int)] = [(-1, 0), (0, 1), (1, 0), (0, -1)]
280+
let directionMap: [Character: Int] = ["^": 0, ">": 1, "v": 2, "<": 3]
281+
282+
let rows = matrix.count
283+
let cols = matrix[0].count
284+
285+
var dist = Array(repeating: Array(repeating: Int.max, count: cols), count: rows)
286+
var deque: [(Int, Int)] = []
287+
288+
dist[start[0]][start[1]] = 0
289+
deque.append((start[0], start[1]))
290+
291+
while !deque.isEmpty {
292+
let (i, j) = deque.removeFirst()
293+
294+
if i == end[0] && j == end[1] {
295+
return dist[i][j]
296+
}
297+
298+
for (k, (di, dj)) in directions.enumerated() {
299+
let ni = i + di
300+
let nj = j + dj
301+
302+
if ni >= 0 && ni < rows && nj >= 0 && nj < cols {
303+
let currentChar = matrix[i][matrix[i].index(matrix[i].startIndex, offsetBy: j)]
304+
let additionalCost = directionMap[currentChar] == k ? 0 : 1
305+
let newDist = dist[i][j] + additionalCost
306+
307+
if newDist < dist[ni][nj] {
308+
dist[ni][nj] = newDist
309+
310+
if additionalCost == 0 {
311+
deque.insert((ni, nj), at: 0)
312+
} else {
313+
deque.append((ni, nj))
314+
}
315+
}
316+
}
317+
}
318+
}
319+
320+
return -1
321+
}
322+
}
323+
```
324+
274325
<!-- tabs:end -->
275326

276327
<!-- solution:end -->
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
class Solution {
2+
func conveyorBelt(_ matrix: [String], _ start: [Int], _ end: [Int]) -> Int {
3+
let directions: [(Int, Int)] = [(-1, 0), (0, 1), (1, 0), (0, -1)]
4+
let directionMap: [Character: Int] = ["^": 0, ">": 1, "v": 2, "<": 3]
5+
6+
let rows = matrix.count
7+
let cols = matrix[0].count
8+
9+
var dist = Array(repeating: Array(repeating: Int.max, count: cols), count: rows)
10+
var deque: [(Int, Int)] = []
11+
12+
dist[start[0]][start[1]] = 0
13+
deque.append((start[0], start[1]))
14+
15+
while !deque.isEmpty {
16+
let (i, j) = deque.removeFirst()
17+
18+
if i == end[0] && j == end[1] {
19+
return dist[i][j]
20+
}
21+
22+
for (k, (di, dj)) in directions.enumerated() {
23+
let ni = i + di
24+
let nj = j + dj
25+
26+
if ni >= 0 && ni < rows && nj >= 0 && nj < cols {
27+
let currentChar = matrix[i][matrix[i].index(matrix[i].startIndex, offsetBy: j)]
28+
let additionalCost = directionMap[currentChar] == k ? 0 : 1
29+
let newDist = dist[i][j] + additionalCost
30+
31+
if newDist < dist[ni][nj] {
32+
dist[ni][nj] = newDist
33+
34+
if additionalCost == 0 {
35+
deque.insert((ni, nj), at: 0)
36+
} else {
37+
deque.append((ni, nj))
38+
}
39+
}
40+
}
41+
}
42+
}
43+
44+
return -1
45+
}
46+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /