Week9 서영탁 14725 개미굴 풀이 #149
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
풀이
개미굴을 탐색할 때 방을 따라 내려가면서 음식들의 정보를 주고 있습니다.
그리고 방의 깊이에 따라 "--"로 구분하며 자식들의 음식 정보를 출력해야합니다.
따라서, 저는
Room이라는 클래스를 만들고 음식, 깊이, 자식들을 저장할HashMap을 멤버변수로 설정했습니다.입력을 받기 전 깊이를 -1로 설정하여 개미굴 입구(
Room root)를 만들었습니다.그 후, 입력을 받으면서 개미굴 입구에서부터 탐색한 정보들을 추가했습니다.
각자
Room에 대해서 부모에게 자신과 동일한 음식 이름의 자식이 없다면 부모의 자식으로 새롭게 추가해주었고,동일한 음식 이름의 자식이 있다면 다음 자식을 보도록 넘어갔습니다.
출력 단계에서 층 순서로 출력하고, 층이 같다면 음식 이름을 사전 순으로 출력하라고 했기 때문에
Room클래스에Comparable<>을 구현하여 깊이 순 -> 음식 이름 순으로 우선순위를 설정하였고,우선순위 큐를 사용하여 출력하였습니다.
리뷰 요청 사항
궁금한 점이나 더 좋은 방법이 있다면 리뷰 남겨주세요.
느낀점
저는 자바 11을 사용해서
String.repeat()를 사용해서 "--"를 반복해서 출력했는데, 자바 8에서는 지원을 하지 않는 메서드네요.자바 8에서는 반복문을 사용해야 할 것 같습니다.