A - 50 years, 50 colors

On Octorber 21st, HDU 50-year-celebration, 50-color balloons floating around the campus, it's so nice, isn't it? To celebrate this meaningful day, the ACM team of HDU hold some fuuny games. Especially, there will be a game named "crashing color balloons".

There will be a n*n matrix board on the ground, and each grid will have a color balloon in it.And the color of the ballon will be in the range of [1, 50].After the referee shouts "go!",you can begin to crash the balloons.Every time you can only choose one kind of balloon to crash, we define that the two balloons with the same color belong to the same kind.What's more, each time you can only choose a single row or column of balloon, and crash the balloons that with the color you had chosen. Of course, a lot of students are waiting to play this game, so we just give every student k times to crash the balloons.

Here comes the problem: which kind of balloon is impossible to be all crashed by a student in k times.

InputThere 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.
OutputFor 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

题意:给你一个n*n的矩阵,每个点表示不同颜色的气球,给你k次机会,每次选择一行或一列,撞同颜色的气球,
问是否能够把所有颜色的气球都撞破,,如果不行,则按顺序打印出不能撞破的气球颜色编号
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=110;
int n,k;
int a[maxn][maxn];//存颜色
int color[maxn];//标记颜色
int li[maxn],vis[maxn];//li记录点的编号 
int dfs(int i,int x)//第i行颜色为x 
{
    for(int j=0;j<n;j++)
    {
        if(!vis[j]&&a[i][j]==x)
        {
            vis[j]=1;
            if(li[j]==-1||dfs(li[j],x))
            {
                li[j]=i;
                return 1;
            }
        }
    }
    return 0;
}
int pipei(int x)
{
    memset(li,-1,sizeof(li));
    int ans=0;
    for(int i=0;i<n;i++)
    {
        memset(vis,0,sizeof(vis));
        if(dfs(i,x))
          ans++;
    }
    return ans;
} 
int main()
{
    int ans[55],cnt;//不能消灭的气球颜色以及数量
    while(cin>>n>>k)
    {
        if(n==0||k==0) break;
        memset(a,0,sizeof(a));
        memset(color,0,sizeof(color));
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
             {
                 cin>>a[i][j];
                 color[a[i][j]]=1;
             }
        }
        cnt=0;
        for(int i=1;i<=50;i++)
        {
            if(color[i])//如果颜色存在
            {
                if(pipei(i)>k)
                  ans[cnt++]=i;
            }
        }
        sort(ans,ans+cnt);
        if(cnt==0)
          printf("-1");
        else
        {
            for(int i=0;i<cnt;i++)
            {
                if(i==0)  cout<<ans[i];
                else
                  cout<<" "<<ans[i];
            }
        }
        cout<<endl;
    } 
    return 0;
}

 


posted @ 2019-10-31 20:04  蓉~  阅读(114)  评论(0编辑  收藏  举报