{"id":35178,"date":"2024-11-01T09:36:27","date_gmt":"2024-11-01T09:36:27","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=35178"},"modified":"2024-11-01T11:44:45","modified_gmt":"2024-11-01T11:44:45","slug":"course-on-kotlin-coding-test-finding-cities-at-a-specific-distance","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/35178\/","title":{"rendered":"Course on Kotlin Coding Test, Finding Cities at a Specific Distance"},"content":{"rendered":"<p><!-- Blog Post: Kotlin Coding Test Tutorial, Finding Cities at a Specific Distance --><\/p>\n<article>\n<header>\n<p>Written on: <time datetime=\"2023-11-01\">November 1, 2023<\/time><\/p>\n<\/header>\n<section>\n<h2>Introduction<\/h2>\n<p>\n            Hello, in this Kotlin coding test tutorial, we will cover the algorithmic problem of &#8216;Finding Cities at a Specific Distance&#8217;.<br \/>\n            This problem involves graph traversal and requires an algorithmic approach to find nodes in cities at a specific distance based on given conditions.<br \/>\n            We will look at how to solve the problem step by step using Kotlin.\n        <\/p>\n<\/section>\n<section>\n<h2>Problem Description<\/h2>\n<p>\n            The given graph consists of N cities and M bidirectional roads.<br \/>\n            Each city is identified by numbers from 1 to N, and the roads connect two cities.<br \/>\n            The goal is to find all cities that are exactly at the given distance K.\n        <\/p>\n<p>\n<strong>Input Format<\/strong><br \/>\n            The first line contains the number of cities N (1 \u2264 N \u2264 30000) and the number of roads M (1 \u2264 M \u2264 200000), followed by the distance K (0 \u2264 K \u2264 30000) we want to find.<br \/>\n            From the second line, M lines follow, each containing information about two connected cities A and B (1 \u2264 A, B \u2264 N).\n        <\/p>\n<p>\n<strong>Output Format<\/strong><br \/>\n            Output the numbers of cities that are exactly at distance K in ascending order; if there are no such cities, output -1.\n        <\/p>\n<\/section>\n<section>\n<h2>Problem Solving Approach<\/h2>\n<p>\n            This problem involves exploring vertices in a graph that match the given conditions.<br \/>\n            By using the BFS (Breadth-First Search) algorithm, we can find cities at a specific distance by searching to the appropriate depth.<br \/>\n            BFS can be implemented using an empty queue and an adjacency list.\n        <\/p>\n<\/section>\n<section>\n<h2>Implementation Method<\/h2>\n<p>\n            First, we construct an adjacency list based on the given city and road information.<br \/>\n            Then we use the BFS algorithm to find the cities that reach the given distance K.\n        <\/p>\n<p>The implementation steps are as follows:<\/p>\n<ol>\n<li>Process input values: Read the number of cities, number of roads, and distance K.<\/li>\n<li>Construct adjacency list: Store other cities connected to each city in list form.<\/li>\n<li>Initialize BFS: Explore from the starting city to the distance K.<\/li>\n<li>Store results: Save city numbers that have reached the distance K in a list.<\/li>\n<li>Output results: Sort and print the cities reached at distance K.<\/li>\n<\/ol>\n<\/section>\n<section>\n<h2>Code Implementation<\/h2>\n<p>Below is the solution code implemented using the BFS algorithm in Kotlin:<\/p>\n<pre><code>\nimport java.util.*\n\nfun main() {\n    val scanner = Scanner(System.`in`)\n    val n = scanner.nextInt() \/\/ Number of cities\n    val m = scanner.nextInt() \/\/ Number of roads\n    val k = scanner.nextInt() \/\/ Desired distance\n    val x = scanner.nextInt() \/\/ Starting city\n\n    \/\/ Initialize adjacency list\n    val graph = Array(n + 1) { mutableListOf<Int>() }\n\n    \/\/ Input road information\n    for (i in 0 until m) {\n        val a = scanner.nextInt()\n        val b = scanner.nextInt()\n        graph[a].add(b)\n        graph[b].add(a)\n    }\n\n    \/\/ BFS implementation\n    val distance = IntArray(n + 1) { -1 }\n    distance[x] = 0\n    val queue: Queue<Int> = LinkedList()\n    queue.add(x)\n\n    while (queue.isNotEmpty()) {\n        val current = queue.poll()\n        for (neighbor in graph[current]) {\n            if (distance[neighbor] == -1) {\n                distance[neighbor] = distance[current] + 1\n                queue.add(neighbor)\n            }\n        }\n    }\n\n    \/\/ Find result cities\n    val result = mutableListOf<Int>()\n    for (i in 1..n) {\n        if (distance[i] == k) {\n            result.add(i)\n        }\n    }\n\n    \/\/ Output results\n    if (result.isEmpty()) {\n        println(-1)\n    } else {\n        result.sort()\n        for (city in result) {\n            println(city)\n        }\n    }\n}\n        <\/Int><\/Int><\/Int><\/code><\/pre>\n<\/section>\n<section>\n<h2>Solution Analysis<\/h2>\n<p>\n            The above code is a typical BFS implementation designed to solve the given problem.<br \/>\n            In particular, by representing the road connectivity as an adjacency list, it optimizes memory usage and allows for fast searching.<br \/>\n            BFS operates by exploring all nodes and placing adjacent nodes into a queue.<br \/>\n            This way, it updates the distance to each node and ultimately collects nodes that stop at distance K.\n        <\/p>\n<p>\n            If no city is found at distance K, returning -1 is also a good programming practice.<br \/>\n            This code structure can be easily modified to suit different types of graph traversal problems.\n        <\/p>\n<\/section>\n<section>\n<h2>Conclusion<\/h2>\n<p>\n            Thus, we have learned about the process of implementing Kotlin code using the BFS algorithm to solve the &#8216;Finding Cities at a Specific Distance&#8217; problem.<br \/>\n            This problem helps in understanding the basics of graph problems and can develop problem-solving skills for various problem types.<br \/>\n            It will also be beneficial for effectively applying algorithms in work or personal projects.<br \/>\n            We plan to cover more diverse algorithm problems in the future, so please stay tuned.\n        <\/p>\n<\/section>\n<footer>\n<p>This post is based on experiences in solving algorithmic problems.<\/p>\n<p>Author: [Your Name]<\/p>\n<\/footer>\n<\/article>\n","protected":false},"excerpt":{"rendered":"<p>Written on: November 1, 2023 Introduction Hello, in this Kotlin coding test tutorial, we will cover the algorithmic problem of &#8216;Finding Cities at a Specific Distance&#8217;. This problem involves graph traversal and requires an algorithmic approach to find nodes in cities at a specific distance based on given conditions. We will look at how to &hellip; <a href=\"https:\/\/atmokpo.com\/w\/35178\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;Course on Kotlin Coding Test, Finding Cities at a Specific Distance&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[106],"tags":[],"class_list":["post-35178","post","type-post","status-publish","format-standard","hentry","category----en"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Course on Kotlin Coding Test, Finding Cities at a Specific Distance - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/atmokpo.com\/w\/35178\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Course on Kotlin Coding Test, Finding Cities at a Specific Distance - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Written on: November 1, 2023 Introduction Hello, in this Kotlin coding test tutorial, we will cover the algorithmic problem of &#8216;Finding Cities at a Specific Distance&#8217;. This problem involves graph traversal and requires an algorithmic approach to find nodes in cities at a specific distance based on given conditions. We will look at how to &hellip; \ub354 \ubcf4\uae30 &quot;Course on Kotlin Coding Test, Finding Cities at a Specific Distance&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/35178\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:36:27+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:44:45+00:00\" \/>\n<meta name=\"author\" content=\"root\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:site\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:label1\" content=\"\uae00\uc4f4\uc774\" \/>\n\t<meta name=\"twitter:data1\" content=\"root\" \/>\n\t<meta name=\"twitter:label2\" content=\"\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04\" \/>\n\t<meta name=\"twitter:data2\" content=\"3\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/35178\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/35178\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"Course on Kotlin Coding Test, Finding Cities at a Specific Distance\",\"datePublished\":\"2024-11-01T09:36:27+00:00\",\"dateModified\":\"2024-11-01T11:44:45+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/35178\/\"},\"wordCount\":537,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Kotlin coding test\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/35178\/\",\"url\":\"https:\/\/atmokpo.com\/w\/35178\/\",\"name\":\"Course on Kotlin Coding Test, Finding Cities at a Specific Distance - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:36:27+00:00\",\"dateModified\":\"2024-11-01T11:44:45+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/35178\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/35178\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/35178\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Course on Kotlin Coding Test, Finding Cities at a Specific Distance\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/atmokpo.com\/w\/#website\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/atmokpo.com\/w\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"ko-KR\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"contentUrl\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"width\":400,\"height\":400,\"caption\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\"},\"image\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/bebubo4\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\",\"name\":\"root\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"caption\":\"root\"},\"sameAs\":[\"http:\/\/atmokpo.com\/w\"],\"url\":\"https:\/\/atmokpo.com\/w\/author\/root\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Course on Kotlin Coding Test, Finding Cities at a Specific Distance - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/atmokpo.com\/w\/35178\/","og_locale":"ko_KR","og_type":"article","og_title":"Course on Kotlin Coding Test, Finding Cities at a Specific Distance - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Written on: November 1, 2023 Introduction Hello, in this Kotlin coding test tutorial, we will cover the algorithmic problem of &#8216;Finding Cities at a Specific Distance&#8217;. This problem involves graph traversal and requires an algorithmic approach to find nodes in cities at a specific distance based on given conditions. We will look at how to &hellip; \ub354 \ubcf4\uae30 \"Course on Kotlin Coding Test, Finding Cities at a Specific Distance\"","og_url":"https:\/\/atmokpo.com\/w\/35178\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:36:27+00:00","article_modified_time":"2024-11-01T11:44:45+00:00","author":"root","twitter_card":"summary_large_image","twitter_creator":"@bebubo4","twitter_site":"@bebubo4","twitter_misc":{"\uae00\uc4f4\uc774":"root","\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04":"3\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/35178\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/35178\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"Course on Kotlin Coding Test, Finding Cities at a Specific Distance","datePublished":"2024-11-01T09:36:27+00:00","dateModified":"2024-11-01T11:44:45+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/35178\/"},"wordCount":537,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Kotlin coding test"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/35178\/","url":"https:\/\/atmokpo.com\/w\/35178\/","name":"Course on Kotlin Coding Test, Finding Cities at a Specific Distance - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:36:27+00:00","dateModified":"2024-11-01T11:44:45+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/35178\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/35178\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/35178\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"Course on Kotlin Coding Test, Finding Cities at a Specific Distance"}]},{"@type":"WebSite","@id":"https:\/\/atmokpo.com\/w\/#website","url":"https:\/\/atmokpo.com\/w\/","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","description":"","publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/atmokpo.com\/w\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"ko-KR"},{"@type":"Organization","@id":"https:\/\/atmokpo.com\/w\/#organization","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","url":"https:\/\/atmokpo.com\/w\/","logo":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/","url":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","contentUrl":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","width":400,"height":400,"caption":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8"},"image":{"@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/bebubo4"]},{"@type":"Person","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7","name":"root","image":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","caption":"root"},"sameAs":["http:\/\/atmokpo.com\/w"],"url":"https:\/\/atmokpo.com\/w\/author\/root\/"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/35178","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/comments?post=35178"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/35178\/revisions"}],"predecessor-version":[{"id":35179,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/35178\/revisions\/35179"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=35178"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=35178"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=35178"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}