fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int N = 1005;
  4.  
  5. int t, n, k, A[N][N] = {};
  6.  
  7. void del(){
  8. for (int i=0; i<n; i++) {
  9. fill(A[i], A[i]+n, 0);
  10. }
  11. }
  12.  
  13. void print(){
  14. for (int i=0; i<n; i++){
  15. for (int j=0; j<n; j++){
  16. cout << A[i][j]<< " ";
  17. }
  18. cout << "\n";
  19. }
  20. }
  21.  
  22. void fillin(int &cnt) {
  23. for (int i=0; i<n; i++){
  24. for (int j=0; j<n; j++){
  25. if (!A[i][j])A[i][j]= cnt++;
  26. }
  27. }
  28. }
  29.  
  30. void progress() {
  31. int cnt =1;
  32. if (k<n||k>n*2-1) {cout << "-1\n"; return;}
  33. else if (k==n){
  34. while(cnt<=n) {
  35. A[cnt-1][cnt-1]=cnt;
  36. cnt++;
  37. }
  38. fillin(cnt);
  39. print();
  40. return;
  41. }
  42. k%=n;
  43. for (int i=0; i<n; i++){
  44. if(i==n-k-1){
  45. for (int j=i; j<n; j++){
  46. A[i][j]=cnt++;
  47. }
  48. i++;
  49. }
  50. if (i==n) break;
  51. A[i][i]=cnt++;
  52. }
  53. fillin(cnt);
  54. print();
  55. }
  56.  
  57. int main() {
  58. ios_base::sync_with_stdio(0);
  59. cin.tie(0);
  60.  
  61. //input
  62. cin >> t;
  63.  
  64. //call
  65. for (int i=0; i<t; i++) {
  66. cin >> n >> k;
  67. progress();
  68. del();
  69. }
  70.  
  71.  
  72.  
  73. return 0;
  74. }
Success #stdin #stdout 0.01s 5288KB
stdin
5
3 0
3 5
5 5
4 3
1 1
stdout
-1
1 2 3 
6 4 7 
8 9 5 
1 6 7 8 9 
10 2 11 12 13 
14 15 3 16 17 
18 19 20 4 21 
22 23 24 25 5 
-1
1