on
day2
day2
반응형
2021년 12월 20일
bfs
1. 코틀린 queue
Queue는 인터페이스이고 구현클래스인 LinkedList를 사용한다.
import java.util.*
fun main() {
// 큐 선언하기
var queue: Queue = LinkedList()
}
add
poll : retrive and remove , or null
peek : retrieve, but does not remove, null
remove : retrive and remove
Summary of Queue methods
Throws exception. Returns special value
Insert add(e) offer(e)
Remove. remove() poll()
Examine element() peek()
2. bfs 만들어보기
package bfs import java.util.* var graph = Array(8){ mutableListOf() } fun bfs(start:Int){ var q: Queue = LinkedList() var c = MutableList(8){false}; q.add(start) c[start] = true; while(q.peek() != null){ var x = q.remove(); println(x) for(i in 0..graph[x].size-1){ var y = graph[x][i]; if(!c[y]){ q.add(y) c[y] = true } } } } fun main(){ println("hello") graph[1].add(2) graph[1].add(3) graph[2].add(1) graph[2].add(4) graph[2].add(5) graph[3].add(1) graph[3].add(6) graph[3].add(7) graph[4].add(2) graph[5].add(2) graph[6].add(3) graph[7].add(3) bfs(1) }
공유하기 글 요소 저작자표시
from http://paulaner80.tistory.com/197 by ccl(A) rewrite - 2021-12-20 05:01:43