hdu 2819 Swap
2015-08-11 20:45
323 查看
http://acm.hdu.edu.cn/showproblem.php?pid=2819
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
Sample Output
题意:如果可以交换行列,问主对角线能不能全为1
分析:要想主对角线全为1很明显要有N个行列不想同的点就行了,可以用二分图匹配计算出来多能有几个。如果小与N就不能。输出要是对的就行,不必和答案一样
这道题不会,参考了别人的代码,还是不太理解,先把代码放到这儿,等我以后明白了再来更博
Swap
Problem DescriptionGiven 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; }
相关文章推荐
- 浅谈C++中类的const成员变量,static成员变量和const static成员变量的区别
- machine learning in coding(python):使用交叉验证【选择模型超参数】
- (剑指Offer)面试题61:按之字形顺序打印二叉树
- 踢足球
- iOS-开发技巧-三种收起键盘的方法
- [6] 算法路 - 双向冒泡排序的Shaker
- vi学习(1)
- Spring配置文件外部化配置及.properties的通用方法
- lamp高级配置
- 无法远程,提示“你的凭据不工作”
- C语言将一个字节逆序
- hdu 5379 Mahjong tree 树形dp
- An internal error occurred during: "J2EE Component Mapping Update".
- 在虚拟机中安装windows
- Python爬取CSDN博客专家系列——移动开发
- JDBC小Demo(原创)
- 寻找缺失的数-LintCode
- linux进程故障调试工具strace(转)
- 在虚拟机中安装windows
- JVM 学习笔记 1. JVM 运行模型