• hdu 2819 Swap


    Swap

    Given an N*N matrix with each entry equal to 0 or 1. You can swap any two rows or any two columns. Can you find a way to make all the diagonal entries equal to 1?
    Input
    There are several test cases in the input. The first line of each test case is an integer N (1 <= N <= 100). Then N lines follow, each contains N numbers (0 or 1), separating by space, indicating the N*N matrix.
    Output
    For each test case, the first line contain the number of swaps M. Then M lines follow, whose format is “R a b” or “C a b”, indicating swapping the row a and row b, or swapping the column a and column b. (1 <= a, b <= N). Any correct answer will be accepted, but M should be more than 1000. 


    If it is impossible to make all the diagonal entries equal to 1, output only one one containing “-1”. 
    Sample Input
    2
    0 1
    1 0
    2
    1 0
    1 0
    Sample Output
    1
    R 1 2
    -1

    题意:交换任意一行或者一列使对角线上都是1。

    先判断最大匹配是不是有n个,在交换匹配后的行和列再记录。

    #include<stdio.h>
    #include<iostream>
    #include<algorithm>
    #include<string.h>
    #include<string>
    #define maxn 110
    using namespace std;
    typedef struct
    {
        int a,b;
    } node;
    node t[maxn*maxn];
    int g[maxn][maxn];
    bool used[maxn];
    int linker[maxn];
    int n;
    bool dfs(int u)
    {
        for(int i=1; i<=n; i++)
        {
            if(g[u][i]&&!used[i])
            {
                used[i]=true;
                if(linker[i]==-1||dfs(linker[i]))
                {
                    linker[i]=u;
                    return true;
                }
            }
        }
        return false;
    }
    int hungary()
    {
        int res=0;
        memset(linker,-1,sizeof(linker));
        for(int i=1; i<=n; i++)
        {
            memset(used,false,sizeof(used));
            if(dfs(i))res++;
        }
        return res;
    }
    int main()
    {
        while(~scanf("%d",&n))
        {
            memset(g,0,sizeof(g));
             memset(t,0,sizeof(t));
            for(int i=1; i<=n; i++)
            {
                for(int j=1; j<=n; j++)
                    scanf("%d",&g[i][j]);
            }
            if(hungary()<n)
            {
                cout<<-1<<endl;
                continue;
            }
            int l=0;
            for(int i=1; i<=n; i++)
            {
                if(linker[i]!=i)
                {
                    int j;
                    for(j=linker[i]; linker[j]!=i; j=linker[j]);
                    t[l].a=i;
                    t[l].b=j;
                    l++;
                    linker[j]=linker[i];
                    linker[i]=i;
                }
            }
            cout<<l<<endl;
            for(int i=0;i<l;i++)
                printf("C %d %d
    ",t[i].a,t[i].b);
        }
    }
    



  • 相关阅读:
    Shell 脚本基本操作练习
    Unix 环境高级编程---线程创建、同步、
    ubuntu 安装ssh-server时出现错误:openssh-server: Depends: openssh-client (= 1:5.3p1-3ubuntu3) but 1:5.3p1-3ubuntu4 is to be installed
    python set 集合
    python 深浅拷贝
    用户权限管理
    vim 编辑器的使用
    linux系统初体验
    平滑升级nginx
    在windows下如何使用密钥对远程登录服务器?
  • 原文地址:https://www.cnblogs.com/da-mei/p/9053297.html
Copyright © 2020-2023  润新知