่งฃๆณไธ
โ ๅฎๆดไปฃ็ขผ
่ฉๅ็ตๆ(ๅ่) ๏ผ AC (68ms, 352KB)
#include <bits/stdc++.h>
using namespace std;
int main(){
cin.tie(nullptr);
int t;
cin >> t;
for(int i=0; i<t; i++){
int n, m;
cin >> n >> m;
int array[n][n] = {0};
if(m == 1){
int pos[2] = {0, 0};
int dir = 0;
int temp = n, x = 0, cnt = 1;
for(int j=1; j<=n*n; j++){
x++;
array[pos[0]][pos[1]] = j;
if(x>=temp)
{
dir++; cnt++;
if(dir >= 4) dir = 0;
if(cnt >= 2) cnt = 0;
if(cnt == 0)
{
temp--;
}
x = 0;
}
if (dir == 0) pos[1]++;
else if(dir == 1) pos[0]++;
else if(dir == 2) pos[1]--;
else if(dir == 3) pos[0]--;
}
}
else if(m == 2){
int pos[2] = {0, 0};
int dir = 0;
int temp = n, x = 0, cnt = 1;
for(int j=1; j<=n*n; j++){
x++;
array[pos[0]][pos[1]] = j;
if(x>=temp){
dir++; cnt++;
if(dir >= 4) dir = 0;
if(cnt >= 2) cnt = 0;
if(cnt == 0){
temp--;
}
x = 0;
}
if (dir == 0) pos[0]++;
else if(dir == 1) pos[1]++;
else if(dir == 2) pos[0]--;
else if(dir == 3) pos[1]--;
}
}
for(int j=0; j<n; j++){
for(int k=0; k<n; k++){
printf("%5d", array[j][k]);
}
printf("\n");
}
}
return 0;
}