标签:
#include<stdio.h> #include<string.h> #include<algorithm> #include<iostream> using namespace std; #define maxn 500 int G[maxn][maxn], vis[maxn]; int P[maxn]; int n, m; bool Find(int u) { for(int i=1; i<=n; i++) { if(G[u][i] && !vis[i]) { vis[i] = true; if(P[i] == -1 || Find(P[i]) ) { P[i] = u; return true; } } } return false; } void solve() { int ans = 0; memset(P, -1, sizeof(P)); for(int i=1; i<=n; i++) { memset(vis, false, sizeof(vis)); if(Find(i)) ans ++; } if(ans < n) { puts("-1"); return ; } int x[maxn], y[maxn], cnt = 0; for(int i=1; i<=n; i++) { if(P[i] == i) continue; for(int j=i+1; j<=n; j++) { if(P[j] == i) { x[cnt] = P[i]; y[cnt++] = P[j]; swap(P[i],P[j]); break; } } } printf("%d\n", cnt); for(int i=0; i<cnt; i++) printf("R %d %d\n", x[i], y[i]); } int main() { while(scanf("%d",&n) != EOF) { for(int i=1; i<=n; i++) { for(int j=1; j<=n; j++) scanf("%d", &G[i][j]); } solve(); } return 0; } /* 3 1 0 1 1 0 1 0 1 0 */
标签:
原文地址:http://www.cnblogs.com/chenchengxun/p/4718581.html