我正在编写一个程序,详尽地计算所有可能的时间表组合与回溯。
当我写“Classe”时,我指的是与学生一起上课(我不能翻译课程)
我有10个学校班级:
public enum Classe{ IA, IIA, IIIA, IB, IIB, IIIB, IC, IIC, IIIC, IID };
和24名教师。
教师是一个对象:名称,主题,自由日和a 使用配对地图:Classe,此课程的小时数
我的TimeTable看起来像这样:TimeTable
我使用 Iterative BackTracking 来填充此表格,如“IA”这样的类。
每列代表每一天的第1天,第2天,第3天,第4天和第5天。
当行中的教师在课堂上有几个小时并且在学校时,它会填充一列。
当它到达星期六的最后一小时时,它从下一个classe的第一天开始。
尝试所有组合。
问题是我的算法的指数性。这需要很多时间。
这是我的BackTracking的代码;抱歉意大利语评论。
int lev = 0;
boolean review = false;
ArrayList<Classe> arrayClasse = new ArrayList<>( Arrays.asList( Classe.values() ) );
int nClasseActual = 0;
if( arrayClasse.isEmpty() ){ System.err.println("There is not classe to assign!"); System.exit(-1); }
Classe classe = arrayClasse.get(nClasseActual); //Take first classe
if ( !firstChoice( classe, lev) ){ //Make first choice
System.err.println("There is not first choice for the class "+classe);
return;
}//if
while (lev >= -1){
if( solComplete(liv) ){ //If the level is the last,
/**I place a classe and now I look for next*/
if( arrayClasse.size()-1 > nClasseActual ){ //There is next classe
nClasseActual++;
classe = arrayClasse.get( nClasseActual ); //Take the next
lev = -1; //Assign the next classe beginnig from lev -1 (next it will be increased)
review = false; //I go on without reviewing
} else { //There is not next class, it was the last
print();
review = true; //I will try other combinations
lev++;
}//if lit
}else{
liv++; //Otherwise I increase level
if( !firstChoice( classe, lev) ) //and I do the first choice of the next level
review= true; //If it doesn't fit, I need to review
}//else
while( review && lev >= 0){ //while it doesn't fit
lev--; //go back with levels
if( lev == -1 ){
/**I have no solutions going forward and I try to modify a previous classe*/
if( nClasseActual > 0 ){ //There is a previous classe and I have to retry
nClasseActual--;
classe = arrayClasse.get(nClasseActual);
lev = DAYS_OF_WEEK * HOURS_OF_DAY -1; //retry starting from last level of previous classe
} else{ //There is not previous classe
System.out.println("I finished combinations");
return; //There is not any choice
}//if lit
}//if -1
if( nextChoice(classe, lev) ) //Do next choice and
review= false; //if it fits, I haven't to review
}//while
}//while
你能建议我一个更有效的解决方案吗? 如何减少处理时间?