php 递归方式 计算学生寝室分配问题 算法

  • Post author:
  • Post category:php


需求是分配学生进入各个寝室,寝室床位数目5-8个

每个寝室如果出现混寝。同个班级的学生必须大于等于2 ,防止孤立

学生数组 key为班级id

寝室数组

array(3) {
  [0] => array(13) {
    ["id"] => int(7)
    ["dorm_name"] => string(4) "3101"
    ["floor_id"] => int(3)
    ["pcapacity"] => int(5)
    ["user_sex"] => int(1)
    ["housemaster"] => NULL
    ["remark"] => string(0) ""
    ["status"] => int(1)
    ["sort"] => int(0)
    ["create_time"] => string(19) "2018-07-23 20:06:01"
    ["update_time"] => string(19) "2022-08-25 18:04:35"
    ["bed_set"] => string(9) "1,2,3,4,5"
    ["bed_num"] => int(5)
  }
  [1] => array(13) {
    ["id"] => int(9)
    ["dorm_name"] => string(4) "3102"
    ["floor_id"] => int(6)
    ["pcapacity"] => int(5)
    ["user_sex"] => int(1)
    ["housemaster"] => NULL
    ["remark"] => string(0) ""
    ["status"] => int(1)
    ["sort"] => int(0)
    ["create_time"] => string(19) "2022-08-24 16:08:32"
    ["update_time"] => string(19) "2022-08-25 18:03:35"
    ["bed_set"] => string(9) "1,2,3,5,6"
    ["bed_num"] => int(5)
  }
  [2] => array(13) {
    ["id"] => int(10)
    ["dorm_name"] => string(4) "3103"
    ["floor_id"] => int(6)
    ["pcapacity"] => int(0)
    ["user_sex"] => int(1)
    ["housemaster"] => NULL
    ["remark"] => string(0) ""
    ["status"] => int(1)
    ["sort"] => int(0)
    ["create_time"] => string(19) "2022-08-25 15:08:34"
    ["update_time"] => string(19) "2022-08-25 18:03:39"
    ["bed_set"] => string(11) "1,2,3,4,5,6"
    ["bed_num"] => int(6)
  }
}

bed_num为寝室床位数据

代码:

$class_man_list  学生数组
$man_dorm寝室数组
$fenpei=[];
foreach($class_man_list as $classid=>$stulist){
    $zhanyou=[];
    $stu_cunt=count($stulist);
    $this->recursion($man_dorm,$stu_cunt,$zhanyou);
    $man_dorm_shengyu=[];
    foreach($zhanyou as $num=>$list){
        if($list['shengyu']>0){
            $room=$list["room"];
            $room['bed_num']=$list['shengyu'];
            $man_dorm_shengyu[]=$room;
        }
        $array_splice=array_splice($stulist,0,$list['zhanyou']);
        if(!empty($fenpei[$list["id"]])){
            $fenpei[$list["id"]]=array_merge($fenpei[$list["id"]],$array_splice);
        }else{
            $fenpei[$list["id"]]=$array_splice;
        }
    }
    if(count($man_dorm_shengyu)>0 && count($man_dorm)>0){
        $man_dorm=array_merge($man_dorm_shengyu,$man_dorm);
    }else if(count($man_dorm_shengyu) && count($man_dorm)==0){
        $man_dorm=$man_dorm_shengyu;
    }
}
dump($fenpei);die;

//$data 宿舍数组  $max 某班级内学生人数
private function recursion(&$data = [],$max,&$arr)
{
    $first=array_shift($data);
    $roomid=$first['id'];
    $bed_count=$first['bed_num'];//床位数目
    $zhanyoushu=$max>=$bed_count?$bed_count:$max;//如果最大值大于床位数 则
    $shengyu=$bed_count-$zhanyoushu;
    if($shengyu==1 && count($data)>0){
        $zhanyoushu-=2;//如果6个床位剩余了1 那么会孤立 所以 6-2  占有4个  或者5-2
        $shengyu+=2;//
    }
    $arr[]=['id'=>$roomid,'zhanyou'=>$zhanyoushu,'shengyu'=>$shengyu,"room"=>$first];
    $max-=$zhanyoushu;
    if($max>0 && count($data)>0){
        $this->recursion($data ,$max,$arr);
    }else{
        return;
    }
}

打印结果:



版权声明:本文为hexiaoniao原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。