• 문제 링크
https://www.acmicpc.net/problem/29865
• 풀이 코드
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.HashSet;
import java.util.Set;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringBuilder sb = new StringBuilder();
String s = br.readLine();
int y = s.charAt(0) - 96, x = s.charAt(1) - 48;
int[][] dir = {{1, 2}, {1, -2}, {-1, 2}, {-1, -2}, {2, 1}, {2, -1}, {-2, 1}, {-2, -1}};
Set<String> set = new HashSet<>();
for (int[] a : dir) {
int y1 = y + a[0], x1 = x + a[1];
if (y1 < 1 || y1 > 8 || x1 < 1 || x1 > 8) continue;
for (int[] b : dir) {
int y2 = y1 + b[0], x2 = x1 + b[1];
if (y2 < 1 || y2 > 8 || x2 < 1 || x2 > 8) continue;
set.add((char) (y2 + 96) + Integer.toString(x2));
}
}
for (String c : set) sb.append(c).append("\n");
bw.write(sb.toString());
bw.flush();
}
}
'Problem Solving > Baekjoon' 카테고리의 다른 글
[백준] 12186 Sort a scrambled itinerary (Small), 12187 Sort a scrambled itinerary (Large) - Graph Theory / Java (0) | 2024.08.17 |
---|---|
[백준] 4556 Relative Relatives - Graph Theory / Java (0) | 2024.08.16 |
[백준] 4363 Snow Clearing - Graph Theory / Java (0) | 2024.08.14 |
[백준] 6752 Time on task - Greedy / Java (0) | 2024.08.13 |
[백준] 13472 Sticky Situation - Greedy / Java (0) | 2024.08.12 |
댓글