📝 풀이 과정
🚨 주의 사항
- 달팽이 배열이 채워지는 순서는
우 → 하 → 좌 → 상
으로 고정되어 있다. - 좌표의 경계를 벗어나거나, 숫자가 이미 존재하는 경우 방향을 전환해야 한다.
👩💻 제출 코드
import java.util.Scanner;
public class 달팽이숫자_1954 {
// 달팽이 회전 방향: 우 -> 하 -> 좌 -> 상
static int[] dx = {0, 1, 0, -1};
static int[] dy = {1, 0, -1, 0};
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int test = sc.nextInt();
for (int t = 1; t <= test; t++) {
int N = sc.nextInt();
int[][] snail = new int[N][N];
int value = 1;
int x = 0, y = 0;
int idx = 0; // 방향 전환을 위한 인덱스
while(value <= N * N){
snail[x][y] = value++;
int nx = x + dx[idx];
int ny = y + dy[idx];
// 방향 전환 = 좌표의 경계를 벗어나거나, 숫자가 이미 존재하는 경우
if(nx < 0 || nx >= N || ny < 0 || ny >= N || snail[nx][ny] != 0){
idx = (idx + 1) % 4;
}
x = x + dx[idx];
y = y + dy[idx];
}
System.out.println("#" + t);
for(int i = 0; i < N; i++){
for(int j = 0; j < N; j++){
System.out.print(snail[i][j] + " ");
}
System.out.println();
}
}
}
}
'코딩 테스트 > SWEA' 카테고리의 다른 글
[SWEA] 2115 벌꿀채취 (조합, 부분집합/Java) (0) | 2024.11.08 |
---|---|
[SWEA] 5656. 벽돌 깨기 (시뮬레이션/Java) (0) | 2024.11.06 |
[SWEA] 1767. 프로세서 연결하기 (DFS/Java) (0) | 2024.11.05 |
[SWEA] 5644. 무선 충전 (시뮬레이션/Java) (0) | 2024.11.04 |
[SWEA] 5653. 줄기세포배양 (구현/Java) (0) | 2024.10.14 |