hdu Swap

Swap

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 3   Accepted Submission(s) : 2
Special Judge
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
 

 

Source
2009 Multi-University Training Contest 1 - Host by TJU
题目意思是说通过交换行或者列来实现对角线(左上角到右下角)上都是1,首先,如果某行全是0或者某列全是0必然不满足情况输出-1,还有就是对角线上的N个1,它们各自在不同的行中出现至少一次才可以,比如样例2中,虽然有两个1,但是它们总是处在同一列,仍然不满足要求,这时候问题的矛盾也比较明显了,很明显不能一个行对应两个列,或者说,每一行都应该有至少一个与自己对应的列才能满足条件,那么将行作为X集合,列作为Y集合,如果map[i][j]==1,那么Xi->Yj连边,求最大匹配,这样的话没有任何一个行被两个列匹配,也就满足了我们的要求,如果最大匹配==N,那么必然有解,否则必然误解,至于结果的输出,保存mx[]和my[],表示与X匹配的点和与Y匹配的点,这样对于i(i∈[1,N]),如果mx[i]!=my[i]交换行i,my[i]就好了。
posted @ 2012-05-01 15:55  lmnx  阅读(319)  评论(0编辑  收藏  举报