在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ In an In one move the snake can:
Return the minimum number of moves to reach the target. If there is no way to reach the target, return
Example 1: Input: grid = [[0,0,0,0,0,1], [1,1,0,0,1,0], [0,0,0,0,1,1], [0,0,1,0,1,0], [0,1,1,0,0,0], [0,1,1,0,0,0]] Output: 11 Explanation: One possible solution is [right, right, rotate clockwise, right, down, down, down, down, rotate counterclockwise, right, down]. Example 2: Input: grid = [[0,0,1,1,1,1], [0,0,0,0,1,1], [1,1,0,0,0,1], [1,1,1,0,0,1], [1,1,1,0,0,1], [1,1,1,0,0,0]] Output: 9
Constraints:
你还记得那条风靡全球的贪吃蛇吗? 我们在一个 每次移动,蛇可以这样走:
返回蛇抵达目的地所需的最少移动次数。 如果无法到达目的地,请返回
示例 1: 输入:grid = [[0,0,0,0,0,1], [1,1,0,0,1,0], [0,0,0,0,1,1], [0,0,1,0,1,0], [0,1,1,0,0,0], [0,1,1,0,0,0]] 输出:11 解释: 一种可能的解决方案是 [右, 右, 顺时针旋转, 右, 下, 下, 下, 下, 逆时针旋转, 右, 下]。 示例 2: 输入:grid = [[0,0,1,1,1,1], [0,0,0,0,1,1], [1,1,0,0,0,1], [1,1,1,0,0,1], [1,1,1,0,0,1], [1,1,1,0,0,0]] 输出:9
提示:
Runtime: 252 ms
Memory Usage: 21.2 MB
1 class Solution { 2 func minimumMoves(_ grid: [[Int]]) -> Int { 3 let n:Int = grid.count 4 var mem:[[[Bool]]] = [[[Bool]]](repeating: [[Bool]](repeating: [Bool](repeating: false, count: 2), count: n), count: n) 5 var q:[Pos] = [Pos]() 6 q.append(Pos(0, 0, 0, 0)) 7 while(!q.isEmpty) 8 { 9 let cur:Pos = q.removeFirst() 10 if cur.s == 0 && cur.x == n - 1 && cur.y == n - 2 11 { 12 return cur.step 13 } 14 for i in 0..<4 15 { 16 if canMove(cur, i, grid, mem) 17 { 18 let next:Pos = move(cur, i) 19 mem[next.x][next.y][next.s] = true 20 q.append(next) 21 } 22 } 23 } 24 return -1 25 } 26 27 func canMove(_ cur:Pos,_ s:Int,_ grid:[[Int]],_ mem:[[[Bool]]]) -> Bool 28 { 29 let n:Int = grid.count 30 let x:Int = cur.x 31 let y:Int = cur.y 32 if s == 0 33 { 34 if cur.s == 0 35 { 36 return y + 2 < n && !mem[x][y + 1][0] && grid[x][y + 2] != 1 37 } 38 else 39 { 40 return y + 1 < n && !mem[x][y + 1][1] && grid[x][y + 1] != 1 && grid[x + 1][y + 1] != 1 41 } 42 } 43 else if s == 1 44 { 45 if cur.s == 0 46 { 47 return x + 1 < n && !mem[x + 1][y][0] && grid[x + 1][y] != 1 && grid[x + 1][y + 1] != 1 48 } 49 else 50 { 51 return x + 2 < n && !mem[x + 1][y][1] && grid[x + 2][y] != 1 52 } 53 } 54 else if s == 2 55 { 56 if cur.s != 0 57 { 58 return false 59 } 60 return x + 1 < n && !mem[x][y][1] && grid[x + 1][y] != 1 && grid[x + 1][y + 1] != 1 61 } 62 else 63 { 64 if cur.s != 1 65 { 66 return false 67 } 68 return y + 1 < n && !mem[x][y][0] && grid[x][y + 1] != 1 && grid[x + 1][y + 1] != 1 69 } 70 } 71 72 func move(_ cur:Pos,_ s:Int) -> Pos 73 { 74 var x:Int = cur.x 75 var y:Int = cur.y 76 var pp = cur.s 77 if s == 0 78 { 79 y += 1 80 } 81 else if s == 1 82 { 83 x += 1 84 } 85 else if s == 2 86 { 87 pp = 1 88 } 89 else 90 { 91 pp = 0 92 } 93 return Pos(x, y, pp, cur.step + 1) 94 } 95 } 96 97 struct Pos 98 { 99 var x:Int = 0 100 var y:Int = 0 101 var s:Int = 0 102 var step:Int = 0 103 init(_ x:Int,_ y:Int,_ s:Int,_ step:Int) 104 { 105 self.x = x 106 self.y = y 107 self.s = s 108 self.step = step 109 } 110 } 236ms
1 class Solution { 2 func minimumMoves(_ grid: [[Int]]) -> Int { 3 let rows = grid.count, cols = grid[0].count 4 var queue = [(Int, Int, Int)]() 5 queue.append((0, 0, 0)) 6 7 var dist = [[[Int]]](repeating: [[Int]](repeating: [Int](repeating: Int.max, count: 2), count: cols), count: rows) 8 9 func valid(_ r: Int, _ c: Int) -> Bool { 10 return r >= 0 && r < rows && 0 <= c && c < cols 11 } 12 13 func bfs_check(_ q: inout [(Int, Int, Int)], _ r: Int, _ c: Int, _ dir: Int, _ curDist: Int) { 14 if curDist < dist[r][c][dir] { 15 dist[r][c][dir] = curDist; 16 q.append((r, c, dir)) 17 } 18 } 19 bfs_check(&queue, 0, 0, 0, 0) 20 while queue.count > 0 { 21 let (r, c, dir) = queue.removeFirst() 22 let curdist = dist[r][c][dir] 23 let r2 = dir == 0 ? r : r + 1 24 let c2 = dir == 0 ? c + 1 : c 25 26 if valid(r, c + 1) && valid(r2, c2 + 1) && grid[r][c + 1] == 0 && grid[r2][c2 + 1] == 0 { 27 bfs_check(&queue, r, c + 1, dir, curdist + 1) 28 } 29 30 if valid(r + 1, c) && valid(r2 + 1, c2) && grid[r + 1][c] == 0 && grid[r2 + 1][c2] == 0 { 31 bfs_check(&queue, r + 1, c, dir, curdist + 1) 32 } 33 34 if dir == 0 { 35 if valid(r + 1, c) && valid(r + 1, c + 1) && grid[r + 1][c] == 0 && grid[r + 1][c + 1] == 0 { 36 bfs_check(&queue, r, c, 1, curdist + 1) 37 } 38 } else { 39 if valid(r, c + 1) && valid(r + 1, c + 1) && grid[r][c + 1] == 0 && grid[r + 1][c + 1] == 0 { 40 bfs_check(&queue, r, c, 0, curdist + 1) 41 } 42 } 43 } 44 let answer = dist[rows - 1][cols - 2][0] 45 return answer < Int.max ? answer : -1 46 } 47 }
|
请发表评论