<titledata-rh="true">Recursion and backtracking with Robot Karel | mf</title><metadata-rh="true"name="viewport"content="width=device-width,initial-scale=1"><metadata-rh="true"name="twitter:card"content="summary_large_image"><metadata-rh="true"property="og:url"content="https://blog.mfocko.xyz/algorithms/recursion/karel-1/"><metadata-rh="true"property="og:locale"content="en"><metadata-rh="true"name="docusaurus_locale"content="en"><metadata-rh="true"name="docsearch:language"content="en"><metadata-rh="true"name="docusaurus_version"content="current"><metadata-rh="true"name="docusaurus_tag"content="docs-algorithms-current"><metadata-rh="true"name="docsearch:version"content="current"><metadata-rh="true"name="docsearch:docusaurus_tag"content="docs-algorithms-current"><metadata-rh="true"property="og:title"content="Recursion and backtracking with Robot Karel | mf"><metadata-rh="true"name="description"content="Aproblemwithtoomanyrestrictions.
<h2class="anchor anchorWithStickyNavbar_LWe7"id="introduction">Introduction<ahref="#introduction"class="hash-link"aria-label="Direct link to Introduction"title="Direct link to Introduction"></a></h2>
<p>In this exercise we will be working with a Robot Karel and with a »very« limited
resources. The point of this exercise is to show how powerful recursion and
backtracking can be even without anything else at your hand.</p>
<h2class="anchor anchorWithStickyNavbar_LWe7"id="your-environment-and-problem-description">Your environment and problem description<ahref="#your-environment-and-problem-description"class="hash-link"aria-label="Direct link to Your environment and problem description"title="Direct link to Your environment and problem description"></a></h2>
<h3class="anchor anchorWithStickyNavbar_LWe7"id="environment">Environment<ahref="#environment"class="hash-link"aria-label="Direct link to Environment"title="Direct link to Environment"></a></h3>
<p>You are given a robot that is present in a maze and is looking for an exit. Maze
consists of different walls and exit is marked with a single so-called “beeper”.</p>
<p>Walking into a wall results in a permanent damage of the robot.</p>
<h3class="anchor anchorWithStickyNavbar_LWe7"id="interface">Interface<ahref="#interface"class="hash-link"aria-label="Direct link to Interface"title="Direct link to Interface"></a></h3>
<p>You can control the robot using the following interface:</p>
<ul>
<li>actions — you can use them to change the current state of the robot and its
surroundings<!---->
<ul>
<li><code>robot.step()</code> — moves robot one step further</li>
<h3class="anchor anchorWithStickyNavbar_LWe7"id="problem">Problem<ahref="#problem"class="hash-link"aria-label="Direct link to Problem"title="Direct link to Problem"></a></h3>
<p>Your task is to decide whether there is an exit from the maze or not. You can see
an example of a maze here:</p>
<p><imgloading="lazy"alt="Image of the maze"src="/assets/images/maze-a374d908bc9445061e15faeddc71641e.png"width="770"height="839"class="img_ev3q"></p>
<h2class="anchor anchorWithStickyNavbar_LWe7"id="simple-problem-to-get-familiar-with-the-robot">Simple problem to get familiar with the robot<ahref="#simple-problem-to-get-familiar-with-the-robot"class="hash-link"aria-label="Direct link to Simple problem to get familiar with the robot"title="Direct link to Simple problem to get familiar with the robot"></a></h2>
<p>If you feel completely lost after the previous description, let me start you off
with a simpler problem.</p>
<p>You are standing in front of the stairs, your task is to walk up the stairs.</p>
<p>You can see an example of such map here:</p>
<p><imgloading="lazy"alt="Image of the stairs"src="/assets/images/stairs-5ee5d03905645aeb13eeaa7774451a64.png"width="1058"height="1161"class="img_ev3q"></p>
<h2class="anchor anchorWithStickyNavbar_LWe7"id="brainstorm-the-idea">Brainstorm the idea<ahref="#brainstorm-the-idea"class="hash-link"aria-label="Direct link to Brainstorm the idea"title="Direct link to Brainstorm the idea"></a></h2>
<p>As a first step write down any ideas and things that you have noticed or came to
<h2class="anchor anchorWithStickyNavbar_LWe7"id="rough-pseudocode">»Rough« pseudocode<ahref="#rough-pseudocode"class="hash-link"aria-label="Direct link to »Rough« pseudocode"title="Direct link to »Rough« pseudocode"></a></h2>
<p>As a next step write a <strong>mock up</strong> of a pseudocode solving the problem, you are
allowed to use comments as placeholders for bigger chunks of code.</p>
<p>Those comments are also a very good hints for decomposition and short, but
descriptive, commnets (if they are short enough and you decide not to factor them
out to separate functions).</p>
<divclass="theme-admonition theme-admonition-tip admonition_xJq3 alert alert--success"><divclass="admonitionHeading_Gvgb"><spanclass="admonitionIcon_Rf37"><svgviewBox="0 0 12 16"><pathfill-rule="evenodd"d="M6.5 0C3.48 0 1 2.19 1 5c0 .92.55 2.25 1 3 1.34 2.25 1.78 2.78 2 4v1h5v-1c.22-1.22.66-1.75 2-4 .45-.75 1-2.08 1-3 0-2.81-2.48-5-5.5-5zm3.64 7.48c-.25.44-.47.8-.67 1.11-.86 1.41-1.25 2.06-1.45 3.23-.02.05-.02.11-.02.17H5c0-.06 0-.13-.02-.17-.2-1.17-.59-1.83-1.45-3.23-.2-.31-.42-.67-.67-1.11C2.44 6.78 2 5.65 2 5c0-2.2 2.02-4 4.5-4 1.22 0 2.36.42 3.22 1.19C10.55 2.94 11 3.94 11 5c0 .66-.44 1.78-.86 2.48zM4 14h5c-.23 1.14-1.3 2-2.5 2s-2.27-.86-2.5-2z"></path></svg></span>tip</div><divclass="admonitionContent_BuS1"><p>The smaller the function is, the easier it is to test it and argue about its
correctness.</p></div></div>
<h2class="anchor anchorWithStickyNavbar_LWe7"id="proper-pseudocode">»Proper« pseudocode<ahref="#proper-pseudocode"class="hash-link"aria-label="Direct link to »Proper« pseudocode"title="Direct link to »Proper« pseudocode"></a></h2>
<p>If you are satisfied with the <em>»rough« pseudocode</em>, it's time to convert it into
a proper one. Get rid of the uncertain pieces of functionality and replace them
with proper pseudocode, i.e. list of the things that should happen in its place.</p>
<h2class="anchor anchorWithStickyNavbar_LWe7"id="library">Library<ahref="#library"class="hash-link"aria-label="Direct link to Library"title="Direct link to Library"></a></h2>
<p>If you got here, and you <strong>actually</strong> wrote down the pseudocode, you can try your
solution after downloading the sources linked at the beginning. If you download
the ZIP-file, you can there:</p>
<ul>
<li>
<p><code>generate_mazes.py</code> - that was used to generate the same maze with beepers in
different locations</p>
</li>
<li>
<p><code>karel_tk.py</code> - library which can run Karel given the his world</p>
<ul>
<li>documentation can be found <ahref="https://www.fi.muni.cz/~xfocko/ib111/10/docs/"target="_blank"rel="noopener noreferrer">here</a></li>
<li>also requires Tk Python library to be installed (it should be included in
majority of Python installations)</li>
</ul>
</li>
<li>
<p><code>*.kw</code> - which represent multiple worlds for Karel I have prepared</p>
</li>
<li>
<p><code>skeleton.py</code> - skeleton for your solution, needs to be put in the same directory
as <code>karel_tk.py</code> and takes path to the world as a first argument, example usage:</p>
<h2class="anchor anchorWithStickyNavbar_LWe7"id="solution">Solution<ahref="#solution"class="hash-link"aria-label="Direct link to Solution"title="Direct link to Solution"></a></h2>
<p>Solution to this problem will be released as a second part, so that you can try
it out by yourself without any influence of “example solution”.</p>
<p>If you want to get any feedback, feel free to mail me your solution (including
all the steps that lead to your final solution, if you wish to get feedback on
those too).</p></div><footerclass="theme-doc-footer docusaurus-mt-lg"><divclass="theme-doc-footer-tags-row row margin-bottom--sm"><divclass="col"><b>Tags:</b><ulclass="tags_jXut padding--none margin-left--sm"><liclass="tag_QGVx"><aclass="tag_zVej tagRegular_sFm0"href="/algorithms/tags/python/">python</a></li><liclass="tag_QGVx"><aclass="tag_zVej tagRegular_sFm0"href="/algorithms/tags/karel/">karel</a></li><liclass="tag_QGVx"><aclass="tag_zVej tagRegular_sFm0"href="/algorithms/tags/recursion/">recursion</a></li><liclass="tag_QGVx"><aclass="tag_zVej tagRegular_sFm0"href="/algorithms/tags/backtracking/">backtracking</a></li></ul></div></div><divclass="theme-doc-footer-edit-meta-row row"><divclass="col"><ahref="https://github.com/mfocko/blog/tree/main/algorithms/04-recursion/2022-11-29-karel-1.md"target="_blank"rel="noopener noreferrer"class="theme-edit-this-page"><svgfill="currentColor"height="20"width="20"viewBox="0 0 40 40"class="iconEdit_Z9Sw"aria-hidden="true"><g><pathd="m34.5 11.7l-3 3.1-6.3-6.3 3.1-3q0.5-0.5 1.2-0.5t1.1 0.5l3.9 3.9q0.5 0.4 0.5 1.1t-0.5 1.2z m-29.5 17.1l18.4-18.5 6.3 6.3-18.4 18.4h-6.3v-6.2z"></path></g></svg>Edit this page</a></div><divclass="col lastUpdated_vwxv"><spanclass="theme-last-updated">Last updated<!----> on <b><timedatetime="2022-11-29T00:00:00.000Z">Nov 29, 2022</time></b></span></div></div></footer></article><navclass="pagination-nav docusaurus-mt-lg"aria-label="Docs pages"><aclass="pagination-nav__link pagination-nav__link--prev"href="/algorithms/category/recursion/"><divclass="pagination-nav__sublabel">Previous</div><divclass="pagination-nav__label">Recursion</div></a><aclass="pagination-nav__link pagination-nav__link--next"href="/algorithms/recursion/pyramid-slide-down/"><divclass="pagination-nav__sublabel">Next</div><divclass="pagination-nav__label">Introduction to dynamic programming</div></a></nav></div></div><divclass="col col--3"><divclass="tableOfContents_bqdL thin-scrollbar theme-doc-toc-desktop"><ulclass="table-of-contents table-of-contents__left-border"><li><ahref="#introduction"class="table-of-contents__link toc-highlight">Introduction</a></li><li><ahref="#your-environment-and-problem-description"class="table-of-contents__link toc-highlight">Your environment and problem description</a><ul><li><ahref="#environment"class="table-of-contents__link toc-highlight">Environment</a></li><li><ahref="#interface"class="table-of-contents__link toc-highlight">Interface</a></li><li><ahref="#problem"class="table-of-contents__link toc-highlight">Problem</a></li></ul></li><li><ahref="#simple-problem-to-get-familiar-with-the-robot"class="table-of-contents__link toc-highlight">Simple problem to get familiar with the robot</a></li><li><ahref="#brainstorm-the-idea"class="table-of-contents__link toc-highlight">Brainstorm the idea</a></li><li><ahref="#rough-pseudocode"class="table-of-contents__link toc-highlight">»Rough« pseudocode</a></li><li><ahref="#proper-pseudocode"class="table-of-contents__link toc-highlight">»Proper« pseudocode</a></li><li><ahref="#library"class="table-of-contents__link toc-highlight">Library</a></li><li><ahref="#solution"class="table-of-contents__link toc-highlight">Solution</a></li></ul></div></div></div></div></main></div></div></div><footerclass="footer footer--dark"><divclass="container container-fluid"><divclass="row footer__links"><divclass="col footer__col"><divclass="footer__title">Git</div><ulclass="footer__items clean-list"><liclass="footer__item"><ahref="https://github.com/mfocko"target="_blank"rel="noopener noreferrer"class="footer__link-item">GitHub<svgwidth="13.5"height="13.5"aria-hidden="true"viewBox="0 0 24 24"class="iconExternalLink_nPIU"><pathfill="currentColor"d="M21 13v10h-21v-19h12v2h-10v15h17v-8h2zm3-12h-10.988l4.035 4-6.977 7.07 2.828 2.828 6.977-7.07 4.125 4.172v-11z"></path></svg></a></li><liclass="footer__item"><ahref="https://gitlab.com/mfocko"target="_blank"rel="noopener noreferrer"class="footer__link-item">GitLab<svgwidth="13.5"height="13.5"aria-hid