杭电1498(50 years, 50 colors)

Input
There will be multiple input cases.Each test case begins with two integers n, k. n is the number of rows and columns of the balloons (1 <= n <= 100), and k is the times that ginving to each student(0 < k <= n).Follow a matrix A of n*n, where Aij denote the color of the ballon in the i row, j column.Input ends with n = k = 0.
 
Output
For each test case, print in ascending order all the colors of which are impossible to be crashed by a student in k times. If there is no choice, print "-1".
 
Sample Input
1 1
1
2 1
1 1
1 2
2 1
1 2
2 2
5 4
1 2 3 4 5
2 3 4 5 1
3 4 5 1 2
4 5 1 2 3
5 1 2 3 4
3 3
50 50 50
50 50 50
50 50 50
0 0
 
Sample Output
-1
1
2
1 2 3 4 5
-1
View Code
 1 #include<stdio.h>
 2 #include<string.h>
 3 int n,m;
 4 int map[110][110],a[110],b[110];
 5 int count[100],f[100];
 6 int find(int x,int y)
 7 {
 8     int i;
 9     for(i=1;i<=n;i++)
10     {
11         if(map[x][i]==y&&b[i]==0)//颜色要一致,且没有覆盖过
12         {
13             b[i]=1;//表示此次覆盖
14              if(a[i]==0||find(a[i],y))
15              {
16                  a[i]=x;//形成新的交替
17                  return 1;
18              }
19         }
20     }
21     return 0;
22 }
23 int met(int x)
24 {
25     int i,sum;
26     sum=0;
27     memset(a,0,sizeof(a));//每一种颜色a都要初始化
28     for(i=1;i<=n;i++)
29     {
30         memset(b,0,sizeof(b));
31         if(find(i,x))
32             sum++;
33     }
34     return sum;
35 }
36 int main()
37 {
38     int i,j,k,num;
39     while(scanf("%d%d",&n,&m),n!=0||m!=0)
40     {
41         memset(map,0,sizeof(map));//图标用来记录气球分布
42         memset(count,0,sizeof(count));//记数,记录各颜色的气球出现的次数
43         memset(f,0,sizeof(f));//记录复合题意的气球
44         for(i=1;i<=n;i++)
45             for(j=1;j<=n;j++)
46             {
47                 scanf("%d",&map[i][j]);
48                 count[map[i][j]]++;
49             }
50             k=0;
51             for(i=1;i<=50;i++)
52             {
53                 if(count[i]&&met(i)>m)//如果i气球出现过,并且满足>m的条件
54                     f[k++]=i;
55             }
56             if(k==0)
57                 printf("-1\n");
58             else
59             {
60                 for(i=0;i<k-1;i++)
61                     printf("%d ",f[i]);
62                 printf("%d\n",f[k-1]);
63             }
64     }
65     return 0;
66 }
原文地址:https://www.cnblogs.com/zlyblog/p/2620104.html