编程之美- 电梯调度算法(电梯会停在K个楼层)

#include <iostream> 
#include <time.h> 
#include <cassert> 
using namespace std; 
 
const int totalFloorNum = 10; 
const int totalStopNum = 4; 
int nPerson[totalFloorNum + 1]; 
int minFloors[totalFloorNum + 1][totalStopNum + 1]; 
int targetFloors[totalFloorNum + 1][totalStopNum + 1]; 
 
//stopNum floors between (totalFloorNum - floor + 1)th floor and (totalFloorNum)th floor 
//the last stop is totalFloorNum - floor 
int compute(int floor, int stopNum) 

    if(minFloors[floor][stopNum] >= 0) 
        return minFloors[floor][stopNum]; 
 
    //determine the first stop in the remaining (floor) floors 
    int min = totalFloorNum - floor + 1; 
    int max = totalFloorNum + 1 - stopNum; 
 
    assert(min <= max); 
    //the next possible step can range from min to max 
    int minFloor = -1; 
    int targetFloor = -1; 
 int i;
 int j;
 
    if(stopNum == 1) 
    { 
        //if only need to stop once, just copy the code from Beauty of Programming 
        int n1 = 0; 
        int n2 = nPerson[min]; 
        int n3 = 0; 
        minFloor = 0; 
        targetFloor = min; 
 
        for(i = min + 1; i <= totalFloorNum; i++) 
        { 
            n3 += nPerson[i]; 
            minFloor += nPerson[i] * (i - min); 
        } 
 
        for(i = min + 1; i <= totalFloorNum; i++) 
        { 
            if(n1 + n2 < n3) 
            { 
                targetFloor = i; 
                minFloor += (n1 + n2 - n3); 
                n1 += n2; 
                n2 = nPerson[i]; 
                n3 -= n2; 
            } 
            else 
                break; 
        } 
    } 
    else 
    { 
        for(i = min; i <= max; i++) 
        { 
            int mid = (min - 1 + i) / 2; 
            int temp = 0; 
            if(min == 1) 
            { 
                for(j = 1; j < i; j++) 
                { 
                    temp += (i - j) * nPerson[j]; 
                } 
            } 
            else 
            {            
                //passengers who want to go to from (min - 1) to mid will get off at (min - 1)th floor 
                for(j = min; j <= mid; j++) 
                { 
                    temp += (j - min + 1) * nPerson[j]; 
                } 
                //passengers who want to go to from (mid + 1) to (i) will get off at (i)th floor 
                for(j = mid + 1; j < i; j++) 
                { 
                    temp += (i - j) * nPerson[j]; 
                } 
            } 
   
            temp += compute(totalFloorNum - i, stopNum - 1); 
            if(temp < minFloor || minFloor == -1) 
            { 
                minFloor = temp; 
                targetFloor = i; 
            } 
        } 
    } 
    minFloors[floor][stopNum] = minFloor; 
    targetFloors[floor][stopNum] = targetFloor; 
    return minFloor; 

int main() 

    memset(nPerson, 0, (totalFloorNum + 1) * sizeof(int)); 
    srand(time(NULL)); 
 int i;
 int j;
    for(i = 1; i <= totalFloorNum; i++) 
    { 
        nPerson[i] = rand() % 100; 
    } 
 
    for(i = 1; i <= totalFloorNum; i++) 
    { 
        for(j = 1; j <= totalStopNum; j++) 
        { 
            minFloors[i][j] = -1; 
        } 
    } 
 
    for(i = 1; i <= totalFloorNum; i++) 
    { 
        cout <<i <<"th floor: " <<nPerson[i] <<" persons" <<endl; 
    } 
    int minFloor = compute(totalFloorNum, totalStopNum); 
    cout <<"The floors selected is: " <<endl; 
    int floor = totalFloorNum; 
    int stopNum = totalStopNum; 
    while(stopNum >= 1) 
    { 
        int selectedFloor = targetFloors[floor][stopNum]; 
        cout <<"The " <<(totalStopNum - stopNum + 1) <<"th selected floor is: " 
            <<selectedFloor <<endl; 
        floor = totalFloorNum - selectedFloor; 
        stopNum--; 
    } 
    cout <<"Min floors: " <<minFloor <<endl; 
    return 0;
}

转载于:https://www.cnblogs.com/xulu/archive/2012/05/08/2490678.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值