티스토리 뷰
728x90
https://www.acmicpc.net/problem/1913
1913번: 달팽이
N개의 줄에 걸쳐 표를 출력한다. 각 줄에 N개의 자연수를 한 칸씩 띄어서 출력하면 되며, 자릿수를 맞출 필요가 없다. N+1번째 줄에는 입력받은 자연수의 좌표를 나타내는 두 정수를 한 칸 띄어서
www.acmicpc.net
방향을 가리키는 변수 설정이 중요한 문제입니다.
행이 증가할 땐, 밑으로
열이 증가할 땐, 우측으로 증가하는 특성을 이용해서 달팽이 모양을 만들 때, 하단 + 오른쪽 (+) / 상단 + 왼쪽 (-)
으로 움직이게 방향 값을 설정한 뒤, 주어진 M 값이 있는 위치를 출력하면 되는 문제입니다.
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
|
import java.util.*;
public class Main {
static int arr[][];
static int N = 0, M = 0;
static StringBuilder sb = new StringBuilder();
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
N = sc.nextInt();
M = sc.nextInt();
arr = new int[N][N];
int dir = 1, row = -1, col = 0;
int x = 0, y = 0;
int n = N * N;
int origin_n = N;
while (N > 0) {
for (int i = 0; i < N; i++) {
row += dir;
arr[row][col] = n;
if (chk_num(row, col)) {
x = row + 1;
y = col + 1;
}
n--;
}
N--;
for (int j = 0; j < N; j++) {
col += dir;
arr[row][col] = n;
if (chk_num(row, col)) {
x = row + 1;
y = col + 1;
}
n--;
}
dir *= -1;
}
for (int i = 0; i < origin_n; i++) {
for (int j = 0; j < origin_n; j++) {
sb.append(arr[i][j] + " ");
}
sb.append("\n");
}
sb.append(x + " " + y);
System.out.print(sb);
sc.close();
}
private static boolean chk_num(int x, int y) {
if (arr[x][y] == M) {
return true;
}
return false;
}
}
|
cs |
'코딩 > 자바 백준' 카테고리의 다른 글
백준 16435 스네이크버드 (0) | 2021.09.02 |
---|---|
백준 1439 뒤집기 (JAVA) (0) | 2021.09.02 |
백준 2947 나무 조각 (JAVA) (0) | 2021.09.01 |
백준 16395 파스칼의 삼각형(JAVA) (0) | 2021.08.31 |
백준 17478 재귀함수가 뭔가요? (JAVA) (0) | 2021.08.31 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 백준 9205
- 백준 23253
- 백준 1916
- 백준 13335
- 백준 1351 파이썬
- 백준 10825
- 백준 13335 파이썬
- 백준 2304 파이썬
- 백준 2491 파이썬
- 백준 4446
- 백준 1916 파이썬
- 백준 23253 파이썬
- 백준 10825 파이썬
- 백준 1504 파이썬
- 백준 20362
- 백준 12788
- 백준 6593 파이썬
- 백준 2075
- 백준 4446 파이썬
- 백준 20362 파이썬
- 백준 12034
- 백준 11123 파이썬
- 백준 2075 파이썬
- 백준 12034 파이썬
- 백준 11123
- 백준 6593
- 백준 1351
- 백준 2304
- 백준 9205 파이썬
- 백준 12788 파이썬
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함