작성
·
97
0
학습 관련 질문을 남겨주세요. 어떤 부분이 고민인지, 무엇이 문제인지 상세히 작성하면 더 좋아요!
먼저 유사한 질문이 있었는지 검색해 보세요.
서로 예의를 지키며 존중하는 문화를 만들어가요.
아래처럼 Stack 대신 Queue 를 사용해 BFS 로 변경해도 문제가 없을까요?
private void openSurroundedCellsBFS(CellPosition cellPosition) {
Queue<CellPosition> queue = new LinkedList<>();
queue.add(cellPosition);
while (!queue.isEmpty()) {
openAndEnqueueCell(queue);
}
}
private void openAndEnqueueCell(Queue<CellPosition> queue) {
CellPosition currentCellPosition = queue.poll(); // 큐에서 꺼냄
if (isOpenedCell(currentCellPosition)) {
return;
}
if (isLandMineCellAt(currentCellPosition)) {
return;
}
openOneCellAt(currentCellPosition);
if (doesCellHaveLandMineCount(currentCellPosition)) {
return;
}
List<CellPosition> surroundedPositions = calculateSurroundedPositions(currentCellPosition, getRowSize(), getColSize());
for (CellPosition surroundedPosition : surroundedPositions) {
queue.add(surroundedPosition);
}
}
답변 2
0
안녕하세요, ewgregerg c 님!
네, 그럼요. 저는 설명의 편의를 위해 DFS를 선택했고요. 탐색 과정에 문제만 없다면 어떤 방법을 사용해도 괜찮습니다.
감사합니다. 🙂
0
안녕하세요, 인프런 AI 인턴입니다.
ewgregerg c님, 질문 주셔서 감사합니다.
질문하신 부분은 Stack을 사용하는 DFS(깊이 우선 탐색)를 Queue로 변경하여 BFS(너비 우선 탐색)로 구현한 코드 인 것 같습니다. 큐(Queue)를 사용함으로써 BFS 구조로 문제 없이 변경할 수 있습니다. BFS는 한 레벨씩 탐색을 진행하므로 각 단계에서 주변 노드들을 모두 탐색하도록 하기 위해 큐를 사용합니다.
제시해주신 코드는 BFS 방식으로 충분히 작동할 것으로 보입니다. Queue
를 사용하여 순차적으로 노드를 탐색하고 있으며, 현재 코드로는 문제 없이 작동할 것 같습니다. 기본적으로 BFS에서는 현재 지점의 주변 노드들을 큐에 추가하여 그 다음 레벨의 노드들을 탐색하는 방식입니다.
코드의 각 메소드들이 의도대로 동작하는지(예: isOpenedCell
, isLandMineCellAt
, openOneCellAt
등)를 확인하여 검증하실 수 있습니다.
추가적인 도움이 필요하시다면 언제든지 질문해 주세요! 😊
출처 링크를 참조하시면 관련된 더 많은 정보를 얻으실 수 있습니다.
Spring Boot Hot Swapping - Reload Java Classes without Restarting