[SWEA] 1954 달팽이 숫자

Time Lapse :13min 2sec

1954.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
import java.util.*;

public class Solution {
public static void main(String[] args) throws Exception {
Scanner scanner = new Scanner(System.in);
int tc = 1, T, N, dir, x, y, n, ny, nx;
int[] dx = {0,1,0,-1};
int[] dy = {-1,0,1,0};
T = scanner.nextInt();
while(T-- != 0) {
N = scanner.nextInt();
int[][] map = new int[N][N];
for(int[] row : map)
Arrays.fill(row, -1);
dir = 1; n = 1;
y = 0; x = 0;
while(true) {
map[y][x] = n++;
ny = y + dy[dir];
nx = x + dx[dir];
if(0 <= nx && nx < N && 0 <= ny && ny < N && map[ny][nx] == -1) {
y = ny;
x = nx;
}
else{
dir = (++dir) % 4;
y = y + dy[dir];
x = x + dx[dir];
if(0 > x || x >= N || 0 > y || y >= N || map[y][x] != -1)
break;
}
}

System.out.println("#" + tc++);
for(int[] row : map) {
for (int val : row)
System.out.print(val + " ");
System.out.println();
}
}
}
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2020/08/04/PS/SWEA/1954/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.