• 문제 링크
5247번: Mining Maps
Connections between mines are described beginning with the line ”GRAPH BEGIN”. Additional lines lists individual mines (nodes), followed (on the same line) by their neighboring mines (edges). The line ”GRAPH END” ends the list of path descriptions.
www.acmicpc.net
• 풀이 코드
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();
while ((br.readLine()) != null) {
Set<Character> node = new HashSet<>();
boolean[][] graph = new boolean[26][26];
String s;
while (!(s = br.readLine()).equals("GRAPH END")) {
char[] arr = s.replace(" ", "").toCharArray();
for (char c : arr) node.add(c);
int root = arr[0] - 'a';
for (int i = 1; i < arr.length; i++)
graph[Math.min(root, arr[i] - 'a')][Math.max(root, arr[i] - 'a')] = true;
}
int cnt = 0;
for (int i = 0; i < 26; i++) for (int j = i + 1; j < 26; j++) if (graph[i][j]) cnt++;
sb.append("NODES ").append(node.size()).append(" EDGES ").append(cnt).append("\n");
}
bw.write(sb.toString());
bw.flush();
}
}
'Problem Solving > Baekjoon' 카테고리의 다른 글
[백준] 9372 상근이의 여행 - Graph Theory / Java (0) | 2024.03.10 |
---|---|
[백준] 22035 Bus Lines - Graph Theory / Java (0) | 2024.03.09 |
[백준] 20914 QWERTY 자판 - Graph Theory / Java (0) | 2024.03.07 |
[백준] 6179 Oh Those Rollers - Graph Theory / Java (0) | 2024.03.06 |
[백준] 10451 순열 사이클 - Graph Theory / Java (0) | 2024.03.05 |
댓글