{"id":34184,"date":"2024-11-01T09:25:15","date_gmt":"2024-11-01T09:25:15","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=34184"},"modified":"2024-11-01T10:58:16","modified_gmt":"2024-11-01T10:58:16","slug":"c-coding-test-course-exploring-mazes","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/34184\/","title":{"rendered":"C++ Coding Test Course, Exploring Mazes"},"content":{"rendered":"<p><body><\/p>\n<h2>Problem Description<\/h2>\n<p>\n        This is a problem of exploring a given maze to find a path to the exit. The maze is given as a 2D array,<br \/>\n        where 0 represents a walkable path, and 1 represents a wall. The starting point is (0, 0) and the exit is (n-1, m-1).<br \/>\n        The size of the maze is <code>n x m<\/code>, and you need to implement an algorithm to explore the maze.\n    <\/p>\n<h2>Input Format<\/h2>\n<p>\n        The first line contains the size of the maze <code>n<\/code> and <code>m<\/code>. (1 \u2264 n, m \u2264 100)<br \/>\n        The next <code>n<\/code> lines will provide the information of the maze consisting of 0s and 1s.\n    <\/p>\n<h2>Output Format<\/h2>\n<p>\n        If it is possible to reach the exit, print the length of that path; if it is impossible, print <code>-1<\/code>.\n    <\/p>\n<h2>Example Input<\/h2>\n<pre>\n    5 5\n    0 1 0 0 0\n    0 1 0 1 0\n    0 0 0 1 0\n    1 1 0 1 0\n    0 0 0 0 0\n    <\/pre>\n<h2>Example Output<\/h2>\n<pre>\n    9\n    <\/pre>\n<h2>Problem Solving Approach<\/h2>\n<p>\n        This problem can be explored using BFS (Breadth-First Search) or DFS (Depth-First Search).<br \/>\n        Since the first discovered path in BFS is the shortest path, it is recommended to use BFS for grid exploration.\n    <\/p>\n<p>\n        If using BFS, you can explore the nodes that can be moved to from the current position using a queue,<br \/>\n        check for visit status, and then add the new position to the queue.<br \/>\n        This method allows you to find the shortest path to the exit.\n    <\/p>\n<h2>C++ Code Implementation<\/h2>\n<p>Below is the C++ code to solve the maze exploration problem.<\/p>\n<pre><code>\n#include &lt;iostream&gt;\n#include &lt;vector&gt;\n#include &lt;queue&gt;\n#include &lt;utility&gt;\n\nusing namespace std;\n\n\/\/ Directions: Up, Down, Left, Right\nconst int dx[] = {-1, 1, 0, 0};\nconst int dy[] = {0, 0, -1, 1};\n\nint bfs(const vector<vector<int>&gt;&amp; maze, int n, int m) {\n    queue<pair<int, int=\"\">&gt; q; \/\/ Queue declaration\n    q.push({0, 0}); \/\/ Starting position (0, 0)\n    \n    vector<vector<int>&gt; distance(n, vector<int>(m, -1)); \/\/ Distance array\n    distance[0][0] = 1; \/\/ Initialize the distance for the starting point to 1\n\n    while (!q.empty()) {\n        auto current = q.front(); q.pop();\n        int x = current.first;\n        int y = current.second;\n\n        \/\/ If the target point is reached\n        if (x == n - 1 &amp;&amp; y == m - 1) {\n            return distance[x][y]; \/\/ Return the path length\n        }\n\n        \/\/ Explore adjacent nodes\n        for (int i = 0; i &lt; 4; i++) {\n            int nx = x + dx[i];\n            int ny = y + dy[i];\n\n            \/\/ Check bounds and if movement is possible\n            if (nx &gt;= 0 &amp;&amp; nx &lt; n &amp;&amp; ny &gt;= 0 &amp;&amp; ny &lt; m &amp;&amp; \n                maze[nx][ny] == 0 &amp;&amp; distance[nx][ny] == -1) {\n                q.push({nx, ny}); \/\/ Add to queue\n                distance[nx][ny] = distance[x][y] + 1; \/\/ Update the distance\n            }\n        }\n    }\n    return -1; \/\/ If unable to reach the exit\n}\n\nint main() {\n    int n, m;\n    cin &gt;&gt; n &gt;&gt; m; \/\/ Input the size of the maze\n    vector<vector<int>&gt; maze(n, vector<int>(m));\n    \n    for (int i = 0; i &lt; n; i++) {\n        for (int j = 0; j &lt; m; j++) {\n            cin &gt;&gt; maze[i][j]; \/\/ Input the maze\n        }\n    }\n\n    cout &lt;&lt; bfs(maze, n, m) &lt;&lt; endl; \/\/ Output the result\n    return 0;\n}\n    <\/int><\/vector<int><\/int><\/vector<int><\/pair<int,><\/vector<int><\/code><\/pre>\n<h2>Code Explanation<\/h2>\n<p>\n        The code uses a 2D vector to represent the maze and calculates the distance to the exit using BFS.<br \/>\n        The main components are:\n    <\/p>\n<ul>\n<li><strong>Queue:<\/strong> Used to store the currently exploring nodes to determine the next moving node.<\/li>\n<li><strong>Distance Array:<\/strong> Records the distance to each node to avoid revisiting already visited nodes.<\/li>\n<li><strong>Direction Arrays:<\/strong> The dx and dy arrays are set to enable movement in the Up, Down, Left, Right directions.<\/li>\n<\/ul>\n<p>\n        When the program runs, it performs BFS starting from the starting point, exploring all adjacent nodes.<br \/>\n        When reaching the exit, it returns the length to that point; if it cannot be reached, it returns <code>-1<\/code>.\n    <\/p>\n<h2>Time Complexity<\/h2>\n<p>\n        The time complexity is <code>O(n * m)<\/code> because each node is visited once.<br \/>\n        The space complexity is also <code>O(n * m)<\/code> due to the use of the distance array and queue.\n    <\/p>\n<h2>Conclusion<\/h2>\n<p>\n        In this lesson, we implemented the BFS algorithm to solve the maze exploration problem using C++.<br \/>\n        I hope this helps you understand the basic concept of the algorithm and aids in writing actual code through practice.<br \/>\n        Therefore, I recommend gaining experience by tackling various forms of maze exploration problems.\n    <\/p>\n<p>Creating various test cases to further improve your code is also a good learning method.<\/p>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem Description This is a problem of exploring a given maze to find a path to the exit. The maze is given as a 2D array, where 0 represents a walkable path, and 1 represents a wall. The starting point is (0, 0) and the exit is (n-1, m-1). The size of the maze is &hellip; <a href=\"https:\/\/atmokpo.com\/w\/34184\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;C++ Coding Test Course, Exploring Mazes&#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":[111],"tags":[],"class_list":["post-34184","post","type-post","status-publish","format-standard","hentry","category-c-coding-test-tutorials-2"],"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, Exploring Mazes - \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\/34184\/\" \/>\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, Exploring Mazes - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Problem Description This is a problem of exploring a given maze to find a path to the exit. The maze is given as a 2D array, where 0 represents a walkable path, and 1 represents a wall. The starting point is (0, 0) and the exit is (n-1, m-1). The size of the maze is &hellip; \ub354 \ubcf4\uae30 &quot;C++ Coding Test Course, Exploring Mazes&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/34184\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:25:15+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T10:58:16+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=\"1\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/34184\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34184\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"C++ Coding Test Course, Exploring Mazes\",\"datePublished\":\"2024-11-01T09:25:15+00:00\",\"dateModified\":\"2024-11-01T10:58:16+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34184\/\"},\"wordCount\":415,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"C++ Coding Test Tutorials\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/34184\/\",\"url\":\"https:\/\/atmokpo.com\/w\/34184\/\",\"name\":\"C++ Coding Test Course, Exploring Mazes - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:25:15+00:00\",\"dateModified\":\"2024-11-01T10:58:16+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34184\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/34184\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/34184\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"C++ Coding Test Course, Exploring Mazes\"}]},{\"@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, Exploring Mazes - \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\/34184\/","og_locale":"ko_KR","og_type":"article","og_title":"C++ Coding Test Course, Exploring Mazes - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Problem Description This is a problem of exploring a given maze to find a path to the exit. The maze is given as a 2D array, where 0 represents a walkable path, and 1 represents a wall. The starting point is (0, 0) and the exit is (n-1, m-1). The size of the maze is &hellip; \ub354 \ubcf4\uae30 \"C++ Coding Test Course, Exploring Mazes\"","og_url":"https:\/\/atmokpo.com\/w\/34184\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:25:15+00:00","article_modified_time":"2024-11-01T10:58:16+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":"1\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/34184\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/34184\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"C++ Coding Test Course, Exploring Mazes","datePublished":"2024-11-01T09:25:15+00:00","dateModified":"2024-11-01T10:58:16+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/34184\/"},"wordCount":415,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["C++ Coding Test Tutorials"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/34184\/","url":"https:\/\/atmokpo.com\/w\/34184\/","name":"C++ Coding Test Course, Exploring Mazes - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:25:15+00:00","dateModified":"2024-11-01T10:58:16+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/34184\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/34184\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/34184\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"C++ Coding Test Course, Exploring Mazes"}]},{"@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\/34184","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=34184"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34184\/revisions"}],"predecessor-version":[{"id":34185,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34184\/revisions\/34185"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=34184"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=34184"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=34184"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}