{"id":33938,"date":"2024-11-01T09:22:12","date_gmt":"2024-11-01T09:22:12","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=33938"},"modified":"2024-11-01T10:54:58","modified_gmt":"2024-11-01T10:54:58","slug":"c-coding-test-course-finding-cities-at-a-specific-distance","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/33938\/","title":{"rendered":"C# Coding Test Course, Finding Cities at a Specific Distance"},"content":{"rendered":"<p><body><\/p>\n<p>Hello everyone. Today, we will learn how to solve the &#8220;Finding Cities at a Specific Distance&#8221; problem using C#. This problem often appears in coding tests and utilizes graph and BFS (Breadth-First Search) algorithms. Therefore, this course will be a great help in enhancing your understanding of graph theory and algorithms.<\/p>\n<h2>Problem Description<\/h2>\n<p>\n    Let me introduce a problem based on given conditions. <\/p>\n<p><strong>Problem<\/strong>: <br \/> <br \/>\n    There are N cities and M bidirectional roads. Each city is numbered from 1 to N. When there is a road connecting two cities A and B, the length of the road connecting A and B is always considered as 1. Given a specific city X and distance K, you need to output all cities that are exactly at distance K. <\/p>\n<p><strong>Input Format<\/strong>: <br \/>\n    The first line contains the number of cities N, the number of roads M, distance K, and starting city X.<br \/>\n    Following that, M lines will contain the two cities connected by each road. <\/p>\n<p><strong>Output Format<\/strong>: <br \/>\n    Output the numbers of the cities at distance K in ascending order.<br \/>\n    If there are no such cities, output -1.\n<\/p>\n<h3>Input Example<\/h3>\n<pre>\n4 4 2 1\n1 2\n1 3\n2 3\n2 4\n<\/pre>\n<h3>Output Example<\/h3>\n<pre>\n4\n<\/pre>\n<h2>Problem Analysis<\/h2>\n<p>\n    To solve this problem, we need a method to represent and search the graph.<br \/>\n    Each city can be represented as a vertex, and roads as edges.<br \/>\n    We need to find a specific distance using BFS or DFS algorithms.<br \/>\n    In this case, we are looking for distance K, so BFS is more suitable because it explores from the closest nodes, thus naturally applying the concept of distance.\n<\/p>\n<h2>Problem Solving Process<\/h2>\n<h3>Step 1: Define Data Structures<\/h3>\n<p>\n    First, we will use lists and a queue to represent cities and roads.<br \/>\n    &#8211; The list will store the roads connected to each city.<br \/>\n    &#8211; The queue is the data structure necessary for performing BFS.\n<\/p>\n<h3>Step 2: Create the Graph<\/h3>\n<p>\n    We will take city and road information as input to create an adjacency list.<br \/>\n    To use the data starting from index 1, we will set the size to N + 1.\n<\/p>\n<h3>Step 3: Implement BFS<\/h3>\n<p>\n    We will implement the BFS function starting from the departure city X.<br \/>\n    We will find cities at distance K and store the results.\n<\/p>\n<h3>Step 4: Output Results<\/h3>\n<p>\n    Sort the result list and output it.<br \/>\n    If there are no cities at distance K, output -1.\n<\/p>\n<h2>C# Code Implementation<\/h2>\n<p>Here is the C# code to solve the given problem.<\/p>\n<pre>\nusing System;\nusing System.Collections.Generic;\nusing System.Linq;\n\npublic class Program\n{\n    static List<int>[] graph;\n    static bool[] visited;\n    static List<int> result;\n    \n    public static void Main(string[] args)\n    {\n        \/\/ Input\n        string[] input = Console.ReadLine().Split(' ');\n        int N = int.Parse(input[0]); \/\/ Number of cities\n        int M = int.Parse(input[1]); \/\/ Number of roads\n        int K = int.Parse(input[2]); \/\/ Distance\n        int X = int.Parse(input[3]); \/\/ Starting city\n        \n        \/\/ Initialize graph\n        graph = new List<int>[N + 1];\n        for (int i = 1; i <= N; i++)\n        {\n            graph[i] = new List<int>();\n        }\n        \n        \/\/ Input road information\n        for (int i = 0; i < M; i++)\n        {\n            input = Console.ReadLine().Split(' ');\n            int a = int.Parse(input[0]);\n            int b = int.Parse(input[1]);\n            graph[a].Add(b);\n            graph[b].Add(a);\n        }\n        \n        \/\/ BFS and result generation\n        visited = new bool[N + 1];\n        result = new List<int>();\n        BFS(X, 0, K);\n        \n        \/\/ Output results\n        result.Sort();\n        if (result.Count == 0)\n        {\n            Console.WriteLine(-1);\n        }\n        else\n        {\n            foreach (int city in result)\n            {\n                Console.WriteLine(city);\n            }\n        }\n    }\n    \n    static void BFS(int start, int distance, int K)\n    {\n        Queue<Tuple<int, int>> queue = new Queue<Tuple<int, int>>();\n        queue.Enqueue(new Tuple<int, int>(start, distance));\n        visited[start] = true;\n        \n        while (queue.Count > 0)\n        {\n            var current = queue.Dequeue();\n            int currentCity = current.Item1;\n            int currentDistance = current.Item2;\n            \n            \/\/ If a city at distance K is found\n            if (currentDistance == K)\n            {\n                result.Add(currentCity);\n                continue;\n            }\n            \n            \/\/ Explore adjacent cities\n            foreach (var neighbor in graph[currentCity])\n            {\n                if (!visited[neighbor])\n                {\n                    visited[neighbor] = true;\n                    queue.Enqueue(new Tuple<int, int>(neighbor, currentDistance + 1));\n                }\n            }\n        }\n    }\n}\n<\/pre>\n<h2>Code Explanation<\/h2>\n<p>\n    I will briefly explain the process of solving the problem through the C# code above.\n<\/p>\n<ul>\n<li>\n<strong>Data Input<\/strong>: Read the values for the number of cities, number of roads, distance K, and starting city X from the first line, and input M road information.\n    <\/li>\n<li>\n<strong>Graph Construction<\/strong>: Construct an adjacency list to represent cities as vertices and roads as edges.\n    <\/li>\n<li>\n<strong>BFS Algorithm Implementation<\/strong>: Use a Queue to execute BFS and calculate the distance to each city.\n    <\/li>\n<li>\n<strong>Output Results<\/strong>: Return the sorted results, and if there are no cities matching distance K, output -1.\n    <\/li>\n<\/ul>\n<h2>Conclusion<\/h2>\n<p>\n    In this lecture, we learned how to solve the &#8220;Finding Cities at a Specific Distance&#8221; problem using C#.<br \/>\n    I hope you felt the importance of graph data structures and search algorithms through the distance-based exploration process using BFS.<br \/>\n    In the next lecture, we will solve even more diverse problems together.\n<\/p>\n<div class=\"note\">\n<strong>Note:<\/strong> As problems become more complex, it is important to discern the time of using BFS and DFS.<br \/>\n    Choosing the right data structure also helps improve performance.\n<\/div>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello everyone. Today, we will learn how to solve the &#8220;Finding Cities at a Specific Distance&#8221; problem using C#. This problem often appears in coding tests and utilizes graph and BFS (Breadth-First Search) algorithms. Therefore, this course will be a great help in enhancing your understanding of graph theory and algorithms. Problem Description Let me &hellip; <a href=\"https:\/\/atmokpo.com\/w\/33938\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;C# Coding Test Course, 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":[90],"tags":[],"class_list":["post-33938","post","type-post","status-publish","format-standard","hentry","category-c-coding-test-tutorials"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>C# Coding Test Course, 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\/33938\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"C# Coding Test Course, Finding Cities at a Specific Distance - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Hello everyone. Today, we will learn how to solve the &#8220;Finding Cities at a Specific Distance&#8221; problem using C#. This problem often appears in coding tests and utilizes graph and BFS (Breadth-First Search) algorithms. Therefore, this course will be a great help in enhancing your understanding of graph theory and algorithms. Problem Description Let me &hellip; \ub354 \ubcf4\uae30 &quot;C# Coding Test Course, Finding Cities at a Specific Distance&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/33938\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:22:12+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T10:54:58+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\/33938\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33938\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"C# Coding Test Course, Finding Cities at a Specific Distance\",\"datePublished\":\"2024-11-01T09:22:12+00:00\",\"dateModified\":\"2024-11-01T10:54:58+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33938\/\"},\"wordCount\":565,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"C# Coding Test Tutorials\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/33938\/\",\"url\":\"https:\/\/atmokpo.com\/w\/33938\/\",\"name\":\"C# Coding Test Course, Finding Cities at a Specific Distance - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:22:12+00:00\",\"dateModified\":\"2024-11-01T10:54:58+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33938\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/33938\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/33938\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"C# Coding Test Course, 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":"C# Coding Test Course, 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\/33938\/","og_locale":"ko_KR","og_type":"article","og_title":"C# Coding Test Course, Finding Cities at a Specific Distance - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Hello everyone. Today, we will learn how to solve the &#8220;Finding Cities at a Specific Distance&#8221; problem using C#. This problem often appears in coding tests and utilizes graph and BFS (Breadth-First Search) algorithms. Therefore, this course will be a great help in enhancing your understanding of graph theory and algorithms. Problem Description Let me &hellip; \ub354 \ubcf4\uae30 \"C# Coding Test Course, Finding Cities at a Specific Distance\"","og_url":"https:\/\/atmokpo.com\/w\/33938\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:22:12+00:00","article_modified_time":"2024-11-01T10:54:58+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\/33938\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/33938\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"C# Coding Test Course, Finding Cities at a Specific Distance","datePublished":"2024-11-01T09:22:12+00:00","dateModified":"2024-11-01T10:54:58+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/33938\/"},"wordCount":565,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["C# Coding Test Tutorials"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/33938\/","url":"https:\/\/atmokpo.com\/w\/33938\/","name":"C# Coding Test Course, Finding Cities at a Specific Distance - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:22:12+00:00","dateModified":"2024-11-01T10:54:58+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/33938\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/33938\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/33938\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"C# Coding Test Course, 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\/33938","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=33938"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/33938\/revisions"}],"predecessor-version":[{"id":33939,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/33938\/revisions\/33939"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=33938"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=33938"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=33938"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}