蓝桥杯 分考场 JAVA

 1 import java.util.Scanner;
 2 public class Test5 {
 3     public static int res=1061109567;
 4     public static int n,m;
 5     public static int[][] gra;
 6     public static int[][] cun;
 7     public static int[] cnt;
 8     public static void solve(int id,int num){    //id表示学生,num表示当前考场的编号  
 9         if(num>=res){              //当现在安排的数量已经大于了最小的教室数量的话,返回  
10             return;  
11         }  
12         if(id>n){                 //安排的学生已经大于所有的学生了,就是安排完所有的学生了已经  
13             res=Math.min(res,num);  
14             return;  
15         }  
16         for(int i=1;i<=num;i++){   //首先看看之前的房间里面能不能放进去  
17             int sz=cnt[i];  
18             int jishu=0;      //得到的是和这个人不认识的人数  
19             for(int j=1;j<=sz;j++){  
20                 if(gra[id][cun[i][j]]==0){  
21                     jishu++;  
22                 }  
23             }  
24             if(jishu==sz){   //如果这里面的学生都和现在遍历的都不认输  
25                 cun[i][++cnt[i]]=id; //将这个学生存到这个考场中  
26                 solve(id+1,num);  
27                 cnt[i]--;  
28             }  
29         }  
30         //重新开一个房间  
31         cun[num+1][++cnt[num+1]]=id;  //没有的话就把它放到下一个教室里  
32         solve(id+1,num+1);  
33         --cnt[num+1];  
34     } 
35     public static void main(String[] args) {
36         Scanner in = new Scanner(System.in);
37         n = in.nextInt();
38         m = in.nextInt();
39         gra=new int[110][110];   //是否存在关系,存在关系就是1,不存在关系就是0  
40         cun=new int[110][110];   //第一维度表示的是考场,二位度里面放的是这个考场里面的学生  
41         //cun[1][1] = 2,cun[1][2] = 0;表示考场1里面第一个存在的人是2,然后后面一位是0,也就是不存在人了,那么这时的cun[1] = 1;表示的是考场里面人的数量  
42         cnt=new int[110];         //是cun数组存的是学生的数量  
43         while(m-- >0){  
44             int a = in.nextInt();
45             int b = in.nextInt();  
46             gra[a][b]=gra[b][a]=1;  
47         }  
48         solve(1,0);  
49         System.out.println(res);
50     }
51 }
原文地址:https://www.cnblogs.com/blzm742624643/p/10383905.html