Vladislav invented a new algorithm of the distributed key construction.
The distributed key consists of 2n numerical sequences of length n. To construct the key Vladislav gets some permutation p of length n2. Next step is to put the elements of p into a square matrix a. The first n elements of p form the first row of a, next n elements form the second row, etc. Finally, 2n numerical sequences of the distributed key are rows and columns of the matrix a.
These 2n sequences were given to Vladislav in some arbitrary order. He spent a while trying to come up with an algorithm for recovering the permutation. Try to restore the permutation before the end of the round!
The first input line contains one integer n (1 ≤ n ≤ 100).
Each of the next 2n lines contains n integers from 1 to n2, rows and columns of the matrix a in some order.
It is guaranteed that the input is correct, there is at least one suitable permutation p.
Print a permutation p of integers from 1 to n2.
2 1 2 1 3 2 4 3 4
1 2 3 4
4 1 6 14 7 5 4 1 13 13 16 12 10 8 3 6 16 11 2 7 10 15 9 14 12 4 3 9 2 5 8 15 11
5 4 1 13 8 3 6 16 15 9 14 12 11 2 7 10
题意:
一个n*n的矩阵,由行列随机拆成2n行,还原他。
算法:
更据第一列中相同的2个元素所在的索引,找出第一行第一列。
之后暴力,矩阵的转置,2种情况不做区分。
代码:
#include<iostream>
#include<stdio.h>
#include<memory.h>
using namespace std;
int n;
int a[205][105];
int b[105][105];
int main()
{
memset(a,sizeof(a),0);
memset(b,sizeof(b),0);
scanf("%d",&n);
int first,second;
for(int i=1;i<=2*n;i++)
{
for(int j=1;j<=n;j++)
{
scanf("%d",&a[i][j]);
}
}
for(int i=1;i<2*n;i++)
{
for(int j=i+1;j<=2*n;j++)
{
if(a[i][1]==a[j][1])
{
first=i;
second=j;
}
}
}
for(int i=1;i<=n;i++)
{
printf("%d ",a[second][i]);
}
for(int i=2;i<=n;i++)
{
int j=0;
for(j=1;j<=(2*n);j++)
{
if(j==first)
continue;
if(a[j][1]==a[first][i])
break;
}
// cout<<j<<endl;
for(int z=1;z<=n;z++)
printf("%d ",a[j][z]);
}
return 0;
}