Monday, March 28, 2011

Exercise 12, Task 3

The function starts in the final point and calculates the minimum distances of the cells until it reaches the start point.

function matr = makewave(startcell,finalcell)
global map;
distmap = zeros(size(map,1),size(map,2));
distmap(finalcell(1),finalcell(2))=2;
queue = finalcell;

reached = 0;
while(size(queue,1) && reached==0)
    [crt,queue] = retrieve(queue);
    n = neighbours(crt);
    for i=1:size(n,1)
        if(map(n(i,1),n(i,2))==0)
           d = distmap(crt(1),crt(2)) + celldist(crt,n(i,:));
           if(distmap(n(i,1),n(i,2))==0) 
                distmap(n(i,1),n(i,2))= d;
                queue = insert(n(i,:),queue);
           elseif(distmap(n(i,1),n(i,2)) > d) 
                distmap(n(i,1),n(i,2)) = d;
           end
        end
        if(n(i,:)==startcell)
            reached = 1;
        end    
    end
end    
if(reached==0)
    fprintf('The wave did not reach the starting point.')
end
matr = distmap;
end

No comments:

Post a Comment