这题也是异列异行问题,而且任意输出一个解即可。。。
#include <cstdlib> #include <cstdio> #include <cmath> #include <cstring> #include <algorithm> #include <fstream> #include <iostream> #define rep(i, l, r) for(int i=l; i<=r; i++) #define down(i, l, r) for(int i=l; i>=r; i--) #define N 123 using namespace std; int read() { int x=0, f=1; char ch=getchar(); while (ch<'0' || ch>'9') { if (ch=='-') f=-1; ch=getchar(); } while (ch>='0' && ch<='9') { x=x*10+ch-'0'; ch=getchar(); } return x*f; } int n, m, d[N][N], k[N], x[N], y[N]; bool b[N]; bool Find(int x) { rep(i, 1, n) if (d[x][i] && !b[i]) { b[i]=1; if (!k[i] || Find(k[i])) { k[i]=x; return true; } } return false; } int main() { while (~scanf("%d", &n)) { rep(i, 1, n) rep(j, 1, n) d[i][j]=read(); rep(i, 1, n) k[i]=0; bool can=true; rep(i, 1, n) { rep(j, 1, n) b[j]=0; if (!Find(i)) { can=false; break; } } if (!can) { printf("-1 "); continue; } else m=0; rep(i, 1, n) while (k[i]!=i) { m++; x[m]=i, y[m]=k[i]; int a; a=k[x[m]], k[x[m]]=k[y[m]], k[y[m]]=a; } printf("%d ", m); rep(i, 1, m) printf("C %d %d ", x[i], y[i]); } return 0; }