您的位置:首页 > 其它

hdu 2819 Swap

2015-08-11 20:45 323 查看
http://acm.hdu.edu.cn/showproblem.php?pid=2819


Swap

Problem Description

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
分析:要想主对角线全为1很明显要有N个行列不想同的点就行了,可以用二分图匹配计算出来多能有几个。如果小与N就不能。输出要是对的就行,不必和答案一样

这道题不会,参考了别人的代码,还是不太理解,先把代码放到这儿,等我以后明白了再来更博


#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <limits>
#include <queue>
#include <stack>
#include <vector>
#include <map>

using namespace std;

#define N 1350
#define INF 0xfffffff
#define PI acos (-1.0)
#define EPS 1e-8

int n, used
, g

, vis
;

int DFS (int u);

int main ()
{
    while (~scanf ("%d", &n))
    {
        memset (used, 0, sizeof (used));
        memset (g, 0, sizeof (g));

        for (int i=1; i<=n; i++)
            for (int j=1; j<=n; j++)
                scanf ("%d", &g[i][j]);

        int ans = 0;
        for (int i=1; i<=n; i++)
        {
            memset (vis, 0, sizeof (vis));
            if (DFS (i)) ans++;
        }
        if (ans < n)
        {
            puts ("-1");
            continue;
        }
        printf ("%d\n", n);

        int i, j;
        for (i=1; i<=n; i++)
        {
            for (j=i; j<=n; j++)
                if (used[j] == i) break;
            used[j] = used[i];
            printf ("C %d %d\n", i, j);
        }
    }
    return 0;
}

int DFS (int u)
{
    for (int i=1; i<=n; i++)
    {
        if (!vis[i] && g[u][i])
        {
            vis[i] = true;
            if (!used[i] || DFS (used[i]))
            {
                used[i] = u;
                return true;
            }
        }
    }
    return false;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: