-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSWEA1954.java
54 lines (43 loc) · 1.52 KB
/
SWEA1954.java
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
/*
* SWEA 1954번(D2)
*
*/
import java.util.Scanner;
public class SWEA1954 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int T = scan.nextInt();
for (int tc=1; tc<=T; tc++) {
int N = scan.nextInt(); // NxN 정사각형
int map[][] = new int[N][N];
int num = 1;
int startX = 0, startY = 0, endX = N - 1, endY = N - 1;
while (true) {
if (num > N * N) break; // 마지막 (종료조건)
for (int y = startY; y <= endY; y++) { // 오른쪽으로 이동
map[startX][y] = num++;
}
startX++; // x좌표+1
for (int x = startX; x <= endX; x++) { // 아래로 이동
map[x][endY] = num++;
}
endY--; // y좌표-1
for (int y = endY; y >= startY; y--) { // 다시 왼쪽으로 이동
map[endX][y] = num++;
}
endX--; // x좌표-1
for (int x = endX; x >= startX; x--) { // 오른쪽으로 이동
map[x][startY] = num++;
}
startY++; // y좌표+1
}
System.out.println("#" + tc);
for (int i=0; i<N; i++) {
for (int j=0; j<N; j++) {
System.out.print(map[i][j] + " ");
}
System.out.println();
}
}
}
}