-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMin_Matrices.cpp
More file actions
56 lines (52 loc) · 1.01 KB
/
Copy pathMin_Matrices.cpp
File metadata and controls
56 lines (52 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
int now;
void solve() {
int curr =0;
int n,k;cin>>n>>k;
if (k==0||k<n||2*n == k) {
cout<<-1<<endl;
return;
}
int cnt = 2*n-k;
vector<vector<int>> a(n,vector<int>(n,0));
vector<int> first(n*n);
for (int i=0;i<n*n;i++) {
first[i]=i+1;
}
for (int i=0;i<cnt;i++) {
a[i][i]=first[i];
curr =i;
}
now =curr;
for (int i=curr+1;i<n;i++) {
a[i][now]=first[++curr];
}
for (int i=now+1;i<n;i++) {
a[now][i]=first[++curr];
}
for (int i=0;i<n;i++) {
for (int j=0;j<n;j++) {
if (a[i][j]==0) {
a[i][j]= first[++curr];
}
}
}
for (int i=0;i<n;i++) {
for (int j=0;j<n;j++) {
cout<<a[i][j]<<" ";
}
cout<<endl;
}
}
int main () {
ios::sync_with_stdio(0);
cin.tie(0);
int T =1;
cin>>T;
while (T--) {
solve();
}
return 0;
}