-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrassfire_algorithm.m
More file actions
53 lines (43 loc) · 1.94 KB
/
grassfire_algorithm.m
File metadata and controls
53 lines (43 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
function distances = grassfire_algorithm(m, n, goalCell, obstacles)
% Initialize the distances matrix with Inf
distances = Inf(m, n);
% Convert goalCell to row and column indices (row-major order)
[goalRow, goalCol] = index_to_rowcol(goalCell, m, n);
% Set the goal cell distance to 0
distances(goalRow, goalCol) = 0;
% Mark obstacles in the distances matrix as -1 initially
for i = 1:length(obstacles)
[obsRow, obsCol] = index_to_rowcol(obstacles(i), m, n);
distances(obsRow, obsCol) = -1; % Mark obstacles as -1
end
% Create a queue (list) and add the goal cell
queue = [goalRow, goalCol];
% Directions for moving up, down, left, right
directions = [0, 1; 0, -1; -1, 0; 1, 0];
while ~isempty(queue)
% Get the current cell and remove it from the queue
current = queue(1, :);
queue(1, :) = [];
currRow = current(1);
currCol = current(2);
% Check all 4 adjacent cells
for i = 1:size(directions, 1)
newRow = currRow + directions(i, 1);
newCol = currCol + directions(i, 2);
% Check if the new cell is within bounds
if newRow >= 1 && newRow <= m && newCol >= 1 && newCol <= n
% Check if the new cell is not marked and is not an obstacle
if distances(newRow, newCol) == -1
continue; % Skip obstacles (remain -1)
end
% Update distance for unmarked cells
if distances(newRow, newCol) > distances(currRow, currCol) + 1
distances(newRow, newCol) = distances(currRow, currCol) + 1; % Update distance
queue(end + 1, :) = [newRow, newCol]; % Add to the queue
end
end
end
end
% After everything, convert -1 back to Inf for output
distances(distances == -1) = Inf;
end