Tất cả thử thách
Trung bình 2 đã giải Tỷ lệ chấp nhận 100.0%

A* Search on a Weighted Grid

Find the cheapest path across a grid of terrain costs with A*, the search algorithm behind game and robot pathfinding.

Bạn sẽ học được gì

  • Find the cheapest path with a priority queue
  • Use the Manhattan distance as a heuristic
  • Understand why admissible heuristics stay optimal

Đề bài

Xem bản dịch

A* is the classic pathfinding algorithm of games and robotics. It explores cells in order of f = g + h, where g is the cost paid so far and h is a heuristic estimate of the cost still to go.

The grid holds a cost for every cell: entering a cell costs its value, and 0 means a wall that cannot be entered. You start on start (its cost is not paid) and may move up, down, left or right.

Use A* with the Manhattan distance |row - goal_row| + |col - goal_col| as the heuristic. Every open cell costs at least 1, so this heuristic never overestimates and A* returns an optimal answer.

Implement a_star_cost(grid, start, goal).

Input

grid: A matrix of cell costs (0 = wall).

start, goal: [row, col] positions.

Output

Return the minimum total cost to reach goal, or -1 if it cannot be reached (including when start or goal is a wall).

Ràng buộc

1 ≤ rows, cols ≤ 100

0 ≤ each cost ≤ 9

Ví dụ

Ví dụ 1
Đầu vào:
grid = [
    [1, 1, 1],
    [1, 0, 1],
    [1, 1, 1]
]
start = [0, 0]
goal = [2, 2]

Kết quả mong đợi:
expected = 4
Đầu vào:
std::vector<std::vector<long long>> grid = {
    {1, 1, 1},
    {1, 0, 1},
    {1, 1, 1}
};
std::vector<long long> start = {0, 0};
std::vector<long long> goal = {2, 2};

Kết quả mong đợi:
long long expected = 4;
Đầu vào:
long[][] grid = new long[][] {
    new long[] {1, 1, 1},
    new long[] {1, 0, 1},
    new long[] {1, 1, 1}
};
long[] start = new long[] {0, 0};
long[] goal = new long[] {2, 2};

Kết quả mong đợi:
long expected = 4;

Giải thích

The centre is a wall. Going right twice and down twice (or down then right) enters 4 cells of cost 1: total 4.

Ví dụ 2
Đầu vào:
grid = [
    [1, 9, 1],
    [1, 9, 1],
    [1, 1, 1]
]
start = [0, 0]
goal = [0, 2]

Kết quả mong đợi:
expected = 6
Đầu vào:
std::vector<std::vector<long long>> grid = {
    {1, 9, 1},
    {1, 9, 1},
    {1, 1, 1}
};
std::vector<long long> start = {0, 0};
std::vector<long long> goal = {0, 2};

Kết quả mong đợi:
long long expected = 6;
Đầu vào:
long[][] grid = new long[][] {
    new long[] {1, 9, 1},
    new long[] {1, 9, 1},
    new long[] {1, 1, 1}
};
long[] start = new long[] {0, 0};
long[] goal = new long[] {0, 2};

Kết quả mong đợi:
long expected = 6;

Giải thích

Crossing the 9 directly costs 9 + 1 = 10. Going down, along the bottom row and back up enters six cells of cost 1: total 6.

Hướng tiếp cận

Hãy tự thử trước, sau đó mở từng gợi ý một.

Gợi ý 1

Use a priority queue ordered by traveled cost plus Manhattan distance to the goal.

Gợi ý 2

Track the best cost found so far for each cell, and only expand a cell when this path improves it.

Gợi ý 3

Treat walls and blocked start or goal cells as impossible, and remember entering the start cell is free.

Độ phức tạp mong đợi

O(rows * cols log(rows * cols)) time, O(rows * cols) extra space

Tham khảo

Thử thách này đã được điều chỉnh cho CodeUtility. Hãy làm theo đề bài và các ràng buộc ở trên.

Lời giải đạt tốt nhất của mỗi người. Lời giải nhanh hơn, tốn ít bộ nhớ hơn và được đánh giá cao hơn sẽ đứng trên.

# Người giải Ngôn ngữ Thời gian Bộ nhớ AI đánh giá Lời giải
1 ННикита Python 849 ms - 5/5 Xem code
2 HTHolota Tudor Python 867 ms - 4/5 Xem code

Học hỏi từ người khác

Giải pháp cộng đồng

Cảnh báo: Có 2 lời giải đầy đủ bên dưới Hãy thử tự giải trước. Bạn vẫn có thể xem mã của người khác và đánh giá AI. Đăng nhập và giải thử thách để tham gia thảo luận.