排班系統——DFS解決!
課設題目描述:
學校實驗樓有7名保安人員:錢、趙、孫、李、周、吳、陳。由於工作需要進行輪休制度,一星期中每人休息一天。預先讓每一個人選擇自己認爲合適的休息日。請編制程序,打印輪休的所有可能方案。當然使每個人都滿意,例如每人選擇的休息日如下:
錢:星期一、星期六
趙:星期二、星期四
孫:星期三、星期日
李:星期五
周:星期一、星期四、星期六
吳:星期二、星期五
陳:星期三、星期六、星期日
運行結果:
Solution: 1
趙 錢 孫 李 周 吳 陳
星期四 星期一 星期三 星期五 星期六 星期二 星期日
Solution: 2
趙 錢 孫 李 周 吳 陳
星期四 星期一 星期日 星期五 星期六 星期二 星期三
Solution: 3
趙 錢 孫 李 周 吳 陳
星期四 星期六 星期三 星期五 星期一 星期二 星期日
Solution: 4
趙 錢 孫 李 周 吳 陳
星期四 星期六 星期日 星期五 星期一 星期二 星期三
課設源代碼:
#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
using namespace std;
int n;
int rest[7][7];
string choice;
char week[7][10]={"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
char weekkk[7][15]={"星期日、","星期一、","星期二、","星期三、","星期四、","星期五、","星期六、"};
char people[7][5]={"趙","錢","孫","李","周","吳","陳"};
void distinguish(){
for(int i=0;i<7;++i){
cout<<people[i]<<":";
for(int j=0;j<6;++j){
cin>>choice;
for(int k=0;k<7;++k){
if(choice==weekkk[k]){
rest[i][k]=1;
choice="";
break;
}
}
if(choice=="") continue;
for(int k=0;k<7;++k){
if(choice==week[k]){
rest[i][k]=1;
choice="";
break;
}
}
if(choice=="") break;
}
}
}
bool judge(int x,int y){
for(int i=0;i<x;++i)
if(rest[i][y]==8)
return false;
return true;
}
void show(){
cout<<"Solution: "<<n<<endl;
cout<<"趙 錢 孫 李 周 吳 陳"<<endl;
cout<<"============================================================="<<endl;
for(int i=0;i<7;++i){
for(int j=0;j<7;++j){
if(rest[i][j]==8){
cout<<week[j]<<" ";
break;
}
}
}
cout<<endl<<endl;
}
void dfs(int person){
if(person==7){
n++;
show();
return;
}
for(int j=0;j<7;++j){
if(rest[person][j]==1&&judge(person,j)){
rest[person][j]=8;
dfs(person+1);
rest[person][j]=1;
}
}
}
int main()
{
memset(rest,0,sizeof(rest));
cout<<"注意!每次輸入完“、”後請輸入空格再繼續輸入!"<<endl;
cout<<"注意!每次輸入完“、”後請輸入空格再繼續輸入!"<<endl<<endl;
distinguish();
cout<<endl;
dfs(0);
return 0;
}
運行結果:
課程實際報告:
如需實驗報告請添加QQ:1511432180