ปานกลาง 0 ข้อที่แก้แล้ว อัตราผ่าน 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.

สิ่งที่คุณจะได้เรียนรู้

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

โจทย์

ดูฉบับแปล

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).

เงื่อนไข

1 ≤ rows, cols ≤ 100

0 ≤ each cost ≤ 9

ตัวอย่าง

ตัวอย่างที่ 1
อินพุต:
goal = [2, 2]
grid = [
    [1, 1, 1],
    [1, 0, 1],
    [1, 1, 1]
]
start = [0, 0]

ผลลัพธ์ที่คาดหวัง:
expected = 4
อินพุต:
std::vector<long long> goal = {2, 2};
std::vector<std::vector<long long>> grid = {
    {1, 1, 1},
    {1, 0, 1},
    {1, 1, 1}
};
std::vector<long long> start = {0, 0};

ผลลัพธ์ที่คาดหวัง:
long long expected = 4;
อินพุต:
long[] goal = new long[] {2, 2};
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 expected = 4;

คำอธิบาย

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

ตัวอย่างที่ 2
อินพุต:
goal = [0, 2]
grid = [
    [1, 9, 1],
    [1, 9, 1],
    [1, 1, 1]
]
start = [0, 0]

ผลลัพธ์ที่คาดหวัง:
expected = 6
อินพุต:
std::vector<long long> goal = {0, 2};
std::vector<std::vector<long long>> grid = {
    {1, 9, 1},
    {1, 9, 1},
    {1, 1, 1}
};
std::vector<long long> start = {0, 0};

ผลลัพธ์ที่คาดหวัง:
long long expected = 6;
อินพุต:
long[] goal = new long[] {0, 2};
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 expected = 6;

คำอธิบาย

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.

แหล่งอ้างอิง

โจทย์นี้ปรับให้เหมาะกับ CodeUtility กรุณาทำตามคำชี้แจงและข้อจำกัดด้านบน

เรียนรู้จากคนอื่น

แนวทางจากชุมชน

ยังไม่มีวิธีทำที่เผยแพร่ ลองแก้โจทย์นี้เพื่อแชร์วิธีแรก