Skip to content

Commit c2f2cca

Browse files
committed
[Array] swea-1954
1 parent 418af6e commit c2f2cca

File tree

4 files changed

+61
-0
lines changed

4 files changed

+61
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@
9999
| 06 | | [Codility-Lesson4 MaxCounters](./src/Array/MaxCounters) | |
100100
| 07 | | [SWEA-1208 Flatten](./src/Array/swea1208) | |
101101
| 08 | | [SWEA-1210 Ladder1](./src/Array/swea1210) | |
102+
| 09 | | [SWEA-1954 달팽이 숫자](./src/Array/swea1954) | |
102103

103104
### Stack
104105

src/Array/swea1954/README.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
## [SW Expert Academy - 1954] 달팽이 숫자
2+
3+
![image](https://user-images.githubusercontent.com/22045163/106583980-02648880-6589-11eb-9638-e0250d586d3e.png)
4+
![image](https://user-images.githubusercontent.com/22045163/106584090-22944780-6589-11eb-9de2-97f484931762.png)
5+
![image](https://user-images.githubusercontent.com/22045163/106584206-45bef700-6589-11eb-8001-a2d9bda538c8.png)

src/Array/swea1954/Solution.java

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package Array.swea1954;
2+
3+
import java.io.*;
4+
5+
public class Solution {
6+
7+
static int T, N;
8+
static int[][] arr;
9+
10+
static int[] di = {0, 1, 0, -1};
11+
static int[] dj = {1, 0, -1, 0};
12+
13+
public static void main(String[] args) throws Exception {
14+
System.setIn(new FileInputStream("src/Array/swea1954/input.txt"));
15+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
16+
17+
T = Integer.parseInt(br.readLine());
18+
for (int t = 1; t <= T; t++) {
19+
N = Integer.parseInt(br.readLine());
20+
arr = new int[N][N];
21+
22+
int num = 1;
23+
int i = 0, j = 0, dir = 0;
24+
while (num <= N*N) {
25+
arr[i][j] = num++;
26+
if (!canGo(i + di[dir % 4], j + dj[dir % 4])) dir ++;
27+
i += di[dir % 4];
28+
j += dj[dir % 4];
29+
}
30+
31+
System.out.println("#" + t);
32+
for (i = 0; i < N; i++) {
33+
for (j = 0; j < N; j++) {
34+
System.out.print(arr[i][j] + " ");
35+
}
36+
System.out.println();
37+
}
38+
}
39+
}
40+
41+
static boolean canGo(int i, int j) {
42+
return 0 <= i && i < N && 0 <= j && j < N && arr[i][j] == 0;
43+
}
44+
}

src/Array/swea1954/input.txt

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
10
2+
1
3+
2
4+
3
5+
4
6+
5
7+
6
8+
7
9+
8
10+
9
11+
10

0 commit comments

Comments
 (0)