Uncategorized

3 7 1 너비 우선 탐색 BFS(Breadth-first search)

Written by

🚀 **이 문서는 보다 쉽게 이해할 수 있도록 정리되었습니다.**

BFS는 가장 기본적인 탐색 알고리즘이다.

“`
public static void bfs(int start, int end) {
int count = -1;

boolean visited[] = new boolean[10000];

//int 배열로 초기화한 queue
Queue qu = new LinkedList();
qu.add(new int[] {start,0}); //처음에 큐에 {start,0} 넣음

while(!qu.isEmpty()) {
//빼기전에 num, cnt에 넣기
int num = qu.peek()[0];
int cnt = qu.peek()[1];

qu.poll();//빼고
if(num == end) {
if(count == -1 || count > cnt)
count = cnt;
continue;
}

if(num<1000 || visited[num]) continue; //이미 방문했던곳이 아니라면 visited[num] = true; //순차적으로 1000이 넘으면서 소수인지 확인 //i는 자리수, j는 0~9까지 for(int i=1; i<=4; i++) { for(int j=0; j<10; j++) { int temp = changeNum(num,i,j); if(temp>=1000 && prime[temp]) {
qu.offer(new int[] {temp,cnt+1});
}
}
}
}//while

if(count == -1)
System.out.println(“Impossible”);
else
System.out.println(count);

}
“`

개발자, 기술사, 삼성, 외국계 IT기업 20년차 기술노트 알렉이 직접 작성한

IT기업 기술 면접을 위한 CS + 면접 노하우 PDF
[https://kmong.com/self-marketing/539751/LUA54VnQsP](https://kmong.com/self-marketing/539751/LUA54VnQsP)
자주 나오는 CS 질문과 답변 그리고 100번 이상 면접관으로 참여하면서 느꼈던

면접자가 알아야 할 팁 13가지 포함

백엔드 개발자를 위한 클라우드 강의, AWS

[https://inf.run/o1NX](https://inf.run/o1NX)

이제는 비전공자도, 일반이도 개발할 수 있다.
ChatGPT를 이용한 누구나 앱개발 with 알렉
[https://inf.run/rpX4](https://inf.run/rpX4)

백엔드 직접 번역한 도서
[https://www.yes24.com/Product/Goods/122536127](https://www.yes24.com/Product/Goods/122536127)

IT기술의 거의 모든 것을 다루는 기술노트with알렉 유투브

[https://www.youtube.com/c/%EA%B8%B0%EC%88%A0%EB%85%B8%ED%8A%B8with%EC%95%8C%EB%A0%89](https://www.youtube.com/c/%EA%B8%B0%EC%88%A0%EB%85%B8%ED%8A%B8with%EC%95%8C%EB%A0%89)

Leave a Comment