课程排教室算法?

图片描述

十间教室 六门课程
教室支持课程如图所示

要求举例说明
安排甲课程时 可选择 a b c d e五间教室
假如甲课程占用b c d e教室 还剩余a 教室空闲

安排乙课程能选三间教室 首选 f g 教室
但因a 教室空闲 所以自动 把甲课程某一项移入a教室 空出公共的教室以供 乙课程使用

以此类推 后者优先 当没有教室可分配时 提示 无法分配

求算法或思路
技术不限 c# js 等等

阅读 2.5k
2 个回答

PHP:

<?php
/**
 * 教室安排
 * @param $dailyArrange 每日安排
 * @param $availableClassroom 日常可用教室
 * @return array | boolean
 */
function ArrangeClassroom($dailyArrange, $availableClassroom = ["A", "B", "C", "D", "E", "F", "G", "H", "J", "K"]){
    if(empty($dailyArrange)){
        return false;
    }
    //课程可用教室
    $courseInit = [
        "aCourse" => ["A","B","C","D","E"],
        "bCourse" => ["B","C","D","E","F","G"],
        "cCourse" => ["H","J","K"],
        "dCourse" => ["A","B","C"],
        "eCourse" => ["C","D","E","F","G"],
        "fCourse" => ["H","J","K"],
    ];
    //排序优先教室
    krsort($dailyArrange);
    //返回结果
    $result = [];
    foreach($dailyArrange as $course=>$count){
        //交集得到课程可用教室
        $intersect = array_intersect($availableClassroom, $courseInit[$course]);
        if($intersect){
            //从可用教室里面去所需课程数
            $used = array_slice($intersect, 0, $count);
            //差集删除已用教室
            $availableClassroom = array_diff($availableClassroom, $used);
            $result[$course] = $used;
        }else{
            $result[$course] = [];
        }
    }
    return $result;
}


//周一需要安排哪些课程,一个课程需要安排多少节
$mondayArrange = ["eCourse"=>3,"bCourse"=>3, "cCourse"=>1, "fCourse"=>2, "aCourse"=>2];
var_dump(ArrangeClassroom($mondayArrange));

/*
返回结果:
array (size=5)
  'fCourse' => 
    array (size=2)
      0 => string 'H' (length=1)
      1 => string 'J' (length=1)
  'eCourse' => 
    array (size=3)
      0 => string 'C' (length=1)
      1 => string 'D' (length=1)
      2 => string 'E' (length=1)
  'cCourse' => 
    array (size=1)
      0 => string 'K' (length=1)
  'bCourse' => 
    array (size=3)
      0 => string 'B' (length=1)
      1 => string 'F' (length=1)
      2 => string 'G' (length=1)
  'aCourse' => 
    array (size=1)
      0 => string 'A' (length=1)
*/
新手上路,请多包涵

<script>

Array.prototype.each = function(fn){
    fn = fn || Function.K;
    var a = [];
    var args = Array.prototype.slice.call(arguments, 1);
    for(var i = 0; i < this.length; i++){
        var res = fn.apply(this,[this[i],i].concat(args));
        if(res != null) a.push(res);
    }
    return a;
};

/**
 * 得到一个数组不重复的元素集合<br/>
 * 唯一化一个数组
 * @returns {Array} 由不重复元素构成的数组
 */
Array.prototype.uniquelize = function(){
    var ra = new Array();
    for(var i = 0; i < this.length; i ++){
        if(ra.indexOf(this[i])<0){
            ra.push(this[i]);
        }
    }
    return ra;
};
Array.intersect = function(a, b){
    return a.uniquelize().each(function(o){
        return b.indexOf(o)>=0 ? o : null
    });
};
Array.minus = function(a, b){
    return a.uniquelize().each(function(o){
        return b.indexOf(o)>=0 ? null : o
    });
};
$availableClassroom = ["A", "B", "C", "D", "E", "F", "G", "H", "J", "K"]
/**
 * 教室安排
 * @param $dailyArrange 每日安排
 * @param $availableClassroom 日常可用教室
 * @return array | boolean
 */
function ArrangeClassroom($dailyArrange) {
    $result = [];

    $courseInit = {
        "aCourse"
        :
            ["A", "B", "C", "D", "E"],
                    "bCourse"
        :
            ["B", "C", "D", "E", "F", "G"],
                    "cCourse"
        :
            ["H", "J", "K"],
                    "dCourse"
        :
            ["A", "B", "C"],
                    "eCourse"
        :
            ["C", "D", "E", "F", "G"],
                    "fCourse"
        :
            ["H", "J", "K"],
    };
    for (var $course in $dailyArrange) {
        if($courseInit[$course]==undefined)continue;
        $intersect = Array.intersect($availableClassroom,$courseInit[$course])
        if($intersect){
            //从可用教室里面去所需课程数
            $used =$intersect.splice(0,$dailyArrange[$course])
            //差集删除已用教室
            $availableClassroom = Array.minus($availableClassroom, $used);
            $result[$course] = $used;
            if($used.length==$dailyArrange[$course]){
                console.dir("课程"+$course+"分配教室为:"+JSON.stringify($used))
            }else{
                console.dir("课程"+$course+"分配教室不够")
            }
        }else{
            $result[$course] = [];
        }

    }
    return $result;
}
//周一需要安排哪些课程,一个课程需要安排多少节
$mondayArrange = [];
$mondayArrange["eCourse"]=3;
$mondayArrange["bCourse"]=3;
$mondayArrange["cCourse"]=1;
$mondayArrange["fCourse"]=2;
$mondayArrange["aCourse"]=2;
ArrangeClassroom($mondayArrange)

</script>

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
logo
Microsoft
子站问答
访问
宣传栏