How would you look at developing an algorithm for this hotel problem?

前端 未结 11 1946
误落风尘
误落风尘 2020-12-13 10:51

There is a problem I am working on for a programming course and I am having trouble developing an algorithm to suit the problem. Here it is:

You are

11条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-13 11:25

    Following is the MATLAB code for hotel problem.

    clc
    clear all
    
    % Data     
    % a = [0;50;180;150;50;40];    
    % a = [0, 200, 400, 600, 601];    
      a = [0,10,180,350,450,600];    
    % a = [0,1,2,3,201,202,203,403];
    
    n = length(a);    
    opt(1) = 0;    
    prev(1)= 1;
    
    for i=2:n
        opt(i) =Inf;
        for j = 1:i-1
            if(opt(i)>(opt(j)+ (200-a(i)+a(j))^2))
                opt(i)= opt(j)+ (200-a(i)+a(j))^2;
                prev(i) = j; 
            end
        end
        S(i) = opt(i);
    end
    
    k = 1;
    i = n;
    sol(1) = n;
    
    while(i>1)
       k = k+1;
       sol(k)=prev(i);
       i = prev(i);   
    end
    
    for i =k:-1:1
        stops(i) = sol(i);
    end
    stops
    

提交回复
热议问题