Artyom_Kopan

Untitled

Jun 2nd, 2022 (edited)
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 0.82 KB | None | 0 0
  1. fun bfs(startArticle: String, searchDepth: Int): Int {
  2.     // first = ссылка, second = число шагов, за которое до неё можно дойти
  3.     val articles: Queue<Pair<String, Int>> = LinkedList()
  4.     articles.add(Pair(startArticle, 0))
  5.  
  6.     while (articles.isNotEmpty()) {
  7.         val (ref, priority) = articles.poll()
  8.         if (ref == HITLER) {
  9.             return priority
  10.         }
  11.         if (priority == searchDepth) {
  12.             continue
  13.         }
  14.         val wikipediaPages = searchRefs(getHtmlDocument(ref))
  15.         for (page in wikipediaPages) {
  16.             /* для отладки */ // println(page)
  17.             if (page == HITLER) {
  18.                 return priority + 1
  19.             }
  20.             articles.add(Pair(page, priority + 1))
  21.         }
  22.     }
  23.  
  24.     return NOT_FOUND
  25. }
Add Comment
Please, Sign In to add comment