Post

특별한 배열

문제

n, jump가 주어질때 배열을 생성해보자

n = 5, jump = 3

Desktop View

n = 4, jump = 2

Desktop View

n = 4, jump = 3

Desktop View



조건

2 <= n <= 100

1 <= jump <= n



🙋‍♂️ 들어가며

문제를 읽어보니 규칙이 있더라, 먼저 외부테두리를 돌고 내부테두리를 도는 것이다.

그리고 테두리의 크기는 특정한 규칙을 통해 갱신되는 것을 알 수 있었다

Desktop View

Desktop View

input-1

1
2
5
3

output-1

1
2
3
4
5
[1, 24, 15, 2, 10]
[6, 17, 13, 7, 21]
[23, 18, 9, 20, 3]
[12, 14, 8, 22, 19]
[5, 25, 16, 4, 11]


input-2

1
2
4
2

output-2

1
2
3
4
[1, 9, 2, 13]
[14, 7, 12, 3]
[6, 16, 8, 10]
[11, 5, 15, 4]


input-3

1
2
4
3

output-3

1
2
3
4
[1, 12, 10, 2]
[13, 5, 9, 7]
[11, 6, 15, 16]
[4, 8, 14, 3]





✅ 코드

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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;

import java.util.Arrays;


public class Solution {
	static int[] dr = {0,1,0,-1};
	static int[] dc = {1,0,-1,0};
	
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(br.readLine());
		int jump = Integer.parseInt(br.readLine());

		// 0-1. 초기값
		int[][] arr = new int[n][n];
		int num = 1;
		arr[0][0] = num;
		num++;
		
		// 0-2. 초기 세팅 (첫 시작점은 맨 왼쪽위 테두리에서 1개 전)
		int cr = 0;
		int cc = -1;
		int start_boundary = 0;
		int end_boundary = n;
		int size = n-2;
		int temp_jump_cnt = 0;
		
		// 1-1. 배열 생성
		while (true) {
			int d = 0;
			
			// 1-2. 테두리 전체 길이 처리
			int temp_boundary_total = 0;
			if (size < 0) {
				temp_boundary_total = 1;
			}
			else if (size >= 0) {
				temp_boundary_total = (size*4) + 4;
			}
			
			// 1-3. 테두리 갯수 구하기
			int boundary_cnt = 0;
			
			// 2-1. 테두리 검사
			while (boundary_cnt < temp_boundary_total) {
				int nr = cr + dr[d];
				int nc = cc + dc[d];
				
				// 2-2. 범위 처리
				if (nr < start_boundary || nr >= end_boundary || nc < start_boundary || nc >= end_boundary) {
					d = (d+1) % 4;
					continue;
				}
				
				// 2-3. 범위 내라면?
				
				// 2-4. 빈칸이라면? 
				if (arr[nr][nc] == 0)  {
					temp_jump_cnt++;
					// 2-4-a. 횟수가 jump 도달시
					if (temp_jump_cnt == jump) {
						arr[nr][nc] = num;
						num++;
						temp_jump_cnt = 0;
						
						// 2-4-b. 만약 배열 다채웠으면
						if (num > n*n) break;
					}
				}
				// 2-5. 빈칸이 아니라면? -> (좌표 갱신, 테두리 순회 횟수++)
				cr = nr;
				cc = nc;
				boundary_cnt++;
			}
			
			// 3. 만약 배열 다채웠다면
			if (num > n*n) break;
			
			// 4-1. 현 테두리 탐색 종료 후, 안쪽 테두리로 진입
			start_boundary++;
			end_boundary--;
			size -= 2;
			
			// 4-2-a. 만약 줄일 수 있다면
			if (start_boundary < end_boundary) {
				cr = start_boundary;
				cc = start_boundary - 1;
			}
			// 4-2-b. 만약 못 줄인다면 -> 다시 초기상태로 reset
			else if (start_boundary >= end_boundary) {
				cr = 0;
				cc = -1;
				start_boundary = 0;
				end_boundary = n;
				size = n-2;
			}
			
			
			
		}
		

		
		// 5. 출력
		for (int i = 0; i < n; i++) {
			System.out.println(Arrays.toString(arr[i]));
		}
		
		
	}
}
This post is licensed under CC BY 4.0 by the author.

Trending Tags

도움이 필요하면
나를 눌러 구리🐻
챗봇
너굴
bot
어서와 구리 ʕ ·ᴥ·ʔ
무엇이 필요하셔 구리?

Trending Tags

도움이 필요하면
나를 눌러 구리🐻
챗봇
너굴
bot
어서와 구리 ʕ ·ᴥ·ʔ
무엇이 필요하셔 구리?