• 문제 링크
https://www.acmicpc.net/problem/1913
• 풀이 코드
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
public class Main {
public static void main(String[] args) throws IOException {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringBuilder sb = new StringBuilder();
int n = read(), m = read(), k = n * n, y = 0, x = 0, d = 0;
String s = "1 1";
int[][] mat = new int[n][n], dir = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
mat[0][0] = k--;
while (k > 0) {
int ny = y + dir[d][0], nx = x + dir[d][1];
if (ny < 0 || ny == n || nx < 0 || nx == n || mat[ny][nx] > 0) d = ++d % 4;
else {
if (k == m) s = (ny + 1) + " " + (nx + 1);
mat[y = ny][x = nx] = k--;
}
}
for (int i = 0; i < n; i++, sb.append("\n")) for (int j = 0; j < n; j++) sb.append(mat[i][j]).append(" ");
bw.write(sb.append(s).toString());
bw.flush();
}
private static int read() throws IOException {
int c, n = System.in.read() & 15;
while ((c = System.in.read()) > 32) n = (n << 3) + (n << 1) + (c & 15);
return n;
}
}
'Problem Solving > Baekjoon' 카테고리의 다른 글
[백준] 2607 비슷한 단어 - Implementation / Java (0) | 2025.02.19 |
---|---|
[백준] 5217 쌍의 합 - Implementation / Java (0) | 2025.02.18 |
[백준] 1924 2007년 - Implementation / Java (0) | 2025.02.16 |
[백준] 12603 Store Credit (Small), 12604 Store Credit (Large) - Brute Force / Java (0) | 2025.02.15 |
[백준] 15633 Fan Death - Brute Force / Java (0) | 2025.02.14 |
댓글