{"id":33974,"date":"2024-11-01T09:22:35","date_gmt":"2024-11-01T09:22:35","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=33974"},"modified":"2024-11-01T10:54:40","modified_gmt":"2024-11-01T10:54:40","slug":"c-coding-test-course-breadth-first-search","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/33974\/","title":{"rendered":"C# Coding Test Course, Breadth-First Search"},"content":{"rendered":"<p>Hello, everyone! Today, we will take a deep dive into one of the important algorithms dealt with in coding tests using C#, which is Breadth-First Search (BFS). BFS is an algorithm for exploring graph or tree data structures, and it is useful for solving shortest path problems or finding specific nodes.<\/p>\n<h2>1. What is Breadth-First Search (BFS)?<\/h2>\n<p>BFS is a search algorithm implemented using the Queue data structure. This algorithm starts from a given node, explores all the adjacent nodes to that node, and then continues to explore the adjacent nodes of the nodes it has just explored. This approach gradually explores wider areas, hence the name &#8216;Breadth-First&#8217;.<\/p>\n<h2>2. Characteristics of BFS<\/h2>\n<ul>\n<li><strong>Shortest path exploration:<\/strong> BFS is suitable for finding the shortest path when all edge weights are the same.<\/li>\n<li><strong>Sunlight exploration:<\/strong> BFS visits closer nodes first, exploring in a way that can be likened to sunlight shining.<\/li>\n<li><strong>Uses a queue:<\/strong> It uses a queue to store the order of exploration, managing the nodes to be visited in a FIFO (First In First Out) manner.<\/li>\n<\/ul>\n<h2>3. How BFS Works<\/h2>\n<p>The basic operation of BFS is as follows:<\/p>\n<ol>\n<li>Add the starting node to the queue and mark it as visited.<\/li>\n<li>Remove a node from the queue and check all its adjacent nodes.<\/li>\n<li>If an adjacent node has not been visited yet, add it to the queue and mark it as visited.<\/li>\n<li>Repeat steps 2 and 3 until the queue is empty.<\/li>\n<\/ol>\n<h2>4. Problem Solving<\/h2>\n<p>Now, let\u2019s understand BFS through a problem that is likely to come up in a real coding test.<\/p>\n<h3>Problem: &#8216;Labeling Complexes&#8217;<\/h3>\n<p>We have an array of size n \u00d7 n consisting of 0s and 1s. Here, 1 indicates where a house exists, and 0 indicates where there are no houses. We consider connected houses as a single complex, and we will use BFS to label the complexes. The problem is to count the number of houses belonging to the same complex and output the size of each complex.<\/p>\n<h4>Input<\/h4>\n<pre>\n5\n01101\n11011\n11100\n00011\n00000\n<\/pre>\n<h4>Output<\/h4>\n<pre>\n3\n2\n1\n<\/pre>\n<h2>5. C# Code Implementation<\/h2>\n<p>Below is the C# code written to solve this problem using BFS.<\/p>\n<pre><code>\nusing System;\nusing System.Collections.Generic;\n\nclass Program\n{\n    static int n;\n    static int[,] map;\n    static bool[,] visited;\n    static int[] dx = { 1, -1, 0, 0 };\n    static int[] dy = { 0, 0, 1, -1 };\n\n    static void Main()\n    {\n        n = int.Parse(Console.ReadLine());\n        map = new int[n, n];\n        visited = new bool[n, n];\n\n        for (int i = 0; i &lt; n; i++)\n        {\n            string line = Console.ReadLine();\n            for (int j = 0; j &lt; n; j++)\n            {\n                map[i, j] = line[j] - '0';\n            }\n        }\n\n        List<int> results = new List<int>();\n\n        for (int i = 0; i &lt; n; i++)\n        {\n            for (int j = 0; j &lt; n; j++)\n            {\n                if (map[i, j] == 1 &amp;&amp; !visited[i, j])\n                {\n                    results.Add(BFS(i, j));\n                }\n            }\n        }\n\n        results.Sort();\n        Console.WriteLine(results.Count);\n        foreach (int count in results)\n        {\n            Console.WriteLine(count);\n        }\n    }\n\n    static int BFS(int startX, int startY)\n    {\n        Queue&lt;(int, int)&gt; queue = new Queue&lt;(int, int)&gt;();\n        queue.Enqueue((startX, startY));\n        visited[startX, startY] = true;\n\n        int count = 0;\n\n        while (queue.Count &gt; 0)\n        {\n            var (x, y) = queue.Dequeue();\n            count++;\n\n            for (int i = 0; i &lt; 4; i++)\n            {\n                int nx = x + dx[i];\n                int ny = y + dy[i];\n\n                if (nx &gt;= 0 &amp;&amp; ny &gt;= 0 &amp;&amp; nx &lt; n &amp;&amp; ny &lt; n &amp;&amp; map[nx, ny] == 1 &amp;&amp; !visited[nx, ny])\n                {\n                    queue.Enqueue((nx, ny));\n                    visited[nx, ny] = true;\n                }\n            }\n        }\n\n        return count;\n    }\n}\n<\/int><\/int><\/code><\/pre>\n<h2>6. Code Explanation<\/h2>\n<p>The C# code above works as follows:<\/p>\n<ol>\n<li>It receives n as input and stores the house placement information in a 2D array of size n \u00d7 n.<\/li>\n<li>The visited array records whether each house has been visited.<\/li>\n<li>Nesting for loops are used to explore all houses, executing BFS only for unvisited houses.<\/li>\n<li>The BFS method uses a queue to explore connected houses from the starting point and counts the number of connected houses.<\/li>\n<li>It stores the sizes of all complexes in a list, sorts them, and then outputs them.<\/li>\n<\/ol>\n<h2>7. Conclusion<\/h2>\n<p>In this lecture, we explored how to solve problems in coding tests using Breadth-First Search (BFS). BFS is a powerful tool applicable to various problems. Understanding BFS is crucial in situations like exploring graphs or finding the shortest path.<\/p>\n<p>Continue to build your skills by practicing BFS with examples and tackling more complex problems. If you have any questions or comments, feel free to leave them in the comments. In the next session, we will cover another algorithm!<\/p>\n<footer>\n<p>\u00a9 2023 Coding Test Lecture. All rights reserved.<\/p>\n<\/footer>\n","protected":false},"excerpt":{"rendered":"<p>Hello, everyone! Today, we will take a deep dive into one of the important algorithms dealt with in coding tests using C#, which is Breadth-First Search (BFS). BFS is an algorithm for exploring graph or tree data structures, and it is useful for solving shortest path problems or finding specific nodes. 1. What is Breadth-First &hellip; <a href=\"https:\/\/atmokpo.com\/w\/33974\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;C# Coding Test Course, Breadth-First Search&#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-33974","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, Breadth-First Search - \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\/33974\/\" \/>\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, Breadth-First Search - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Hello, everyone! Today, we will take a deep dive into one of the important algorithms dealt with in coding tests using C#, which is Breadth-First Search (BFS). BFS is an algorithm for exploring graph or tree data structures, and it is useful for solving shortest path problems or finding specific nodes. 1. What is Breadth-First &hellip; \ub354 \ubcf4\uae30 &quot;C# Coding Test Course, Breadth-First Search&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/33974\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:22:35+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T10:54:40+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=\"4\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/33974\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33974\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"C# Coding Test Course, Breadth-First Search\",\"datePublished\":\"2024-11-01T09:22:35+00:00\",\"dateModified\":\"2024-11-01T10:54:40+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33974\/\"},\"wordCount\":521,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"C# Coding Test Tutorials\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/33974\/\",\"url\":\"https:\/\/atmokpo.com\/w\/33974\/\",\"name\":\"C# Coding Test Course, Breadth-First Search - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:22:35+00:00\",\"dateModified\":\"2024-11-01T10:54:40+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33974\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/33974\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/33974\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"C# Coding Test Course, Breadth-First Search\"}]},{\"@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, Breadth-First Search - \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\/33974\/","og_locale":"ko_KR","og_type":"article","og_title":"C# Coding Test Course, Breadth-First Search - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Hello, everyone! Today, we will take a deep dive into one of the important algorithms dealt with in coding tests using C#, which is Breadth-First Search (BFS). BFS is an algorithm for exploring graph or tree data structures, and it is useful for solving shortest path problems or finding specific nodes. 1. What is Breadth-First &hellip; \ub354 \ubcf4\uae30 \"C# Coding Test Course, Breadth-First Search\"","og_url":"https:\/\/atmokpo.com\/w\/33974\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:22:35+00:00","article_modified_time":"2024-11-01T10:54:40+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":"4\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/33974\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/33974\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"C# Coding Test Course, Breadth-First Search","datePublished":"2024-11-01T09:22:35+00:00","dateModified":"2024-11-01T10:54:40+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/33974\/"},"wordCount":521,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["C# Coding Test Tutorials"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/33974\/","url":"https:\/\/atmokpo.com\/w\/33974\/","name":"C# Coding Test Course, Breadth-First Search - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:22:35+00:00","dateModified":"2024-11-01T10:54:40+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/33974\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/33974\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/33974\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"C# Coding Test Course, Breadth-First Search"}]},{"@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\/33974","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=33974"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/33974\/revisions"}],"predecessor-version":[{"id":33975,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/33974\/revisions\/33975"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=33974"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=33974"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=33974"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}