-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSolution.java
44 lines (35 loc) Β· 1.21 KB
/
Solution.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
package Array.swea1954;
import java.io.*;
public class Solution {
static int T, N;
static int[][] arr;
static int[] di = {0, 1, 0, -1};
static int[] dj = {1, 0, -1, 0};
public static void main(String[] args) throws Exception {
System.setIn(new FileInputStream("src/Array/swea1954/input.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
T = Integer.parseInt(br.readLine());
for (int t = 1; t <= T; t++) {
N = Integer.parseInt(br.readLine());
arr = new int[N][N];
int num = 1;
int i = 0, j = 0, dir = 0;
while (num <= N*N) {
arr[i][j] = num++;
if (!canGo(i + di[dir % 4], j + dj[dir % 4])) dir ++;
i += di[dir % 4];
j += dj[dir % 4];
}
System.out.println("#" + t);
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
}
static boolean canGo(int i, int j) {
return 0 <= i && i < N && 0 <= j && j < N && arr[i][j] == 0;
}
}