Blue Bridge Cup test questions

insert image description hereInscription

Before arranging each student, first check whether the previous exam room can allow this student to enter, that is, whether this student does not know all the students in this classroom in turn. Because it is not easy to prove that doing so is correct, to be on the safe side, consider opening a new examination room for this student. Pay attention to backtracking!!! There is also backtracking to determine whether the currently calculated number of test rooms is greater than or equal to the calculated minimum number of test rooms roomMin, if it is a direct retun.

#include <iostream>

using namespace std;

const int Maxn=100+5;

int relation[Maxn][Maxn];
int  room[Maxn][Maxn];

int roomMin;//房间数目的最小值
int n,m;

void dfs(int index,int res){
    
    
    if(res>=roomMin)      //房间数目比人数还多(直接不用考虑了)
        return;
    if(index>n){
    
       //安排的人数大于学生人数了
        if(res<roomMin)
            roomMin=res;
        return;
    }
    for(int i=1;i<=res;i++){
    
    
        int position=0;//如果这个学生可以进i考场,position来确定他的位置
        while(room[i][position]&&!relation[index][room[i][position]])
            position++;
        if(!room[i][position]){
    
    
            room[i][position]=index;
            dfs(index+1,res);
            room[i][position]=0;
        }
    }
    room[res+1][0]=index;//为这个学生开辟一个新的考场
    dfs(index+1,res+1);
    room[res+1][0]=0;
}

int main()
{
    
    
    ios::sync_with_stdio(false);
    int a,b;
    cin>>n;
    cin>>m;
    roomMin=n;

    for(int i=0;i<m;i++){
    
    
        cin>>a>>b;
        relation[a][b]=relation[b][a]=1;
    }
    dfs(1,0);
    cout<<roomMin<<endl;
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325346943&siteId=291194637