<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Steve on Medium]]></title>
        <description><![CDATA[Stories by Steve on Medium]]></description>
        <link>https://medium.com/@steve2972?source=rss-4109517a15cd------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*Oc3cM9tsLUPqGrva02cj8A.jpeg</url>
            <title>Stories by Steve on Medium</title>
            <link>https://medium.com/@steve2972?source=rss-4109517a15cd------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 09:21:44 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@steve2972/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Samsung Coding Test Prep: Baby Shark]]></title>
            <link>https://medium.com/@steve2972/samsung-coding-test-prep-baby-shark-2994c44a8db?source=rss-4109517a15cd------2</link>
            <guid isPermaLink="false">https://medium.com/p/2994c44a8db</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[samsung]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[algorithms]]></category>
            <category><![CDATA[coding-test]]></category>
            <dc:creator><![CDATA[Steve]]></dc:creator>
            <pubDate>Sun, 11 Dec 2022 14:14:58 GMT</pubDate>
            <atom:updated>2022-12-11T14:14:58.306Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*r8V13D3nI5MMGdn2" /><figcaption>Photo by <a href="https://unsplash.com/@_actually_?utm_source=medium&amp;utm_medium=referral">Ali Abdul Rahman</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Difficulty level: <em>medium low</em></p><p>This is the beginning of a series of articles where I tackle the various programming problems required in the Samsung Coding Proficiency Test (SCPC).</p><p>Unlike many problems in LeetCode or any other coding websites, Samsung’s coding problems are centered around <em>simulation</em> and <em>implementation</em>. In other words, they test how well you are able to <strong>precisely</strong> <strong>simulate</strong> a given experiment under tight time constraints (I guess this reflects Samsung’s business policy?). Examples include simulating a Rubik’s cube or re-creating the popular game <a href="https://play2048.co/">2048</a>.</p><p>This article — and all following articles — is organized as follows:</p><ol><li>An overview of the problem.</li><li>My thought process when first tackling the problem.</li><li>My solution.</li><li>Insights on efficient methods to solving the problem.</li></ol><p>So without any further ado, let’s start with the first notoriously famous problem: Baby Shark.</p><h3><strong>Problem Description</strong></h3><p>There are M fish and one baby shark in a NxN grid. The grid is divided into squares that are 1x1 in size, with each square containing <strong>at most </strong>1 fish.</p><p>Both the fish and the baby shark have an intrinsic size. This size is a positive integer. Baby shark has an initial size of 2 and can move one square at a time in one of the cardinal directions (north, south, east, west) every second.</p><p>Baby shark cannot cross a square containing a fish with a bigger size than baby shark, but can otherwise cross any other square. Baby shark can only eat fish that are smaller than itself. Consequently, although baby shark cannot eat a fish that is the same size as itself, it can move into a square containing said fish.</p><p>Baby shark decides its next move based on the following conditions:</p><ul><li>When baby shark can no longer eat any fish in the grid, it calls for mother shark.</li><li>If there exists a fish that baby shark can eat, it moves in that direction.</li><li>If there are more than 1 fish that baby shark can eat, it moves in the direction of the closest fish.</li><li>The distance between baby shark and a fish is determined by the shortest available path.</li><li>If there are many fish that lie at the shortest distance, baby shark starts with the topmost fish. If there are several topmost fish, baby shark starts with the leftmost fish.</li></ul><p>Assume that moving one square takes one second, while eating a fish takes no time at all. Thus, when baby shark moves into a square containing a fish, it eats the fish at the same time as it moves into the square. When baby shark eats a fish, that square becomes an empty square.</p><p>Baby shark’s size increases by 1 when it eats the same number of fish as its size. For example, a baby shark with size 3 will become size 4 if it eats 3 fish (note: it doesn’t matter if it eats size 1 or 2 fish in this process).</p><p>Given information about the grid, write a program that can find the number of seconds baby shark can go without needing to call mother shark for help.</p><h3>Tackling the Problem</h3><p>Whew! This problem was a mouthful. As is often the case with implementation/simulation-type questions, there are many constraints and specific details to look out for.</p><p>When tackling this problem, I started with the most basic function: getting the shark to move.</p><p>As specified by the problem, baby shark can move in one of the four cardinal directions. In a grid, we can concisely write this by using dx, dy, and the lambda function.</p><pre>dx = [0,0,1,-1]<br>dy = [1,-1,0,0]<br><br>move = lambda r,c,d: (r+dy[d],c+dx[d])</pre><p>Thus, given baby shark’s position (r,c) we can call move(r,c,d) where d is an integer between 0 and 3, with the directions representing (down, up, right, left) respectively.</p><p>Next, we want to define a function that can find the closest fish that baby shark can eat. In order to do so, we utilize BFS, which let’s us find the shortest path to all available fish. In our case, we only need to find all fish of a certain distance from baby shark. Note that we need to keep track of the distance traveled in order to calculate the number of seconds it takes baby shark to travel to the nearest fish.</p><p>This can be implemented as follows:</p><pre># n = the size of the nxn grid<br># grid = the grid containing all shark/fish information<br><br>def find_closest_fish(r, c, size):<br>    # Keep a list of edible fish available at a certain distance<br>    visited, fish = set(), []<br>    # Keep track of the number of seconds elapsed (distance traveled)<br>    elps = 0<br>    <br>    # We initialize BFS using the queue data structure<br>    # First we store the initial row/col of the shark<br>    queue = [(r,c)]<br>    while queue:<br>        qsize = len(queue)<br>        while qsize &gt; 0:<br>            r, c = queue.pop(0) # FCFS left pop<br>            if (r,c) not in visited:<br>                visited.add((r,c))<br>                if 0 &lt; grid[r][c] &lt; size:<br>                    # If the grid contains a fish that is smaller than<br>                    # baby shark, add the fish to <br>                    fish.append((r,c))<br>                for d in range(4):<br>                    # Check each available direction<br>                    nr,nc=move(r,c,d)<br>                    if 0&lt;=nr&lt;n and 0&lt;=nc&lt;n and grid[nr][nc] &lt;= size:<br>                        queue.append((nr,nc))<br>            qsize -= 1<br>        if len(fish) &gt; 0: break<br>        # If there are no available fish, shark moves another square.<br>        elps += 1<br>    return fish, elps</pre><p>Awesome! Now that we know the closest available fish, we can finally put the code all together.</p><p>First, we need to declare the size of the baby shark at the start (which is 2), the number of fish baby shark has eaten, and finally the total number of seconds that baby shark was on the grid.</p><pre>shark = 2<br>fish_eaten = 0<br>elps = 0</pre><p>Next, we want to run the program until there is no more edible fish. In order to do, we run while True until the list returned from find_closest_fish no longer contains any more fish.</p><pre>while True:<br>    fish, dis = find_closest_fish(shark[0], shark[1], size)<br>    if len(fish) == 0: break<br>    else:<br>        target = fish[0]<br>        <br>        # If there are multiple fish, choose the topmost, leftmost fish!<br>        for f in fish[1:]:<br>            if f[0] &lt; target[0]: target = f<br>            elif f[0] == target[0]:<br>                if f[1] &lt; target[1]: target = f<br><br>        # elapsed seconds is increased by distance traveled<br>        elps += dis<br>        shark = target<br>        grid[shark[0]][shark[1]] = 0<br>        fish_eaten += 1<br>        if fish_eaten == size:<br>            size += 1<br>            fish_eaten = 0</pre><p>When the number of fish eaten is equal to the size of baby shark, baby shark becomes bigger by 1!</p><h3>Closing Remarks</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SPDHfLnzpi3PzpOKEUGCLg.png" /><figcaption>Our code is super efficient! It ran the fastest out of 14,150 submissions.</figcaption></figure><p>Using BFS and a simple heuristic to determine how baby shark moves proved to be enough to solve this problem.</p><p>Running the code with the online code judge shows that my code ran the fastest out of 14,150 total submissions, proving that implementation problems need not overly complicated solutions.</p><p>Sometimes the cleanest code may be the most optimal one after all.</p><p>Now we just need to worry what will happen when baby shark becomes too big after eating all those fish…</p><p>The full code can be seen <a href="https://github.com/steve2972/Algorithms/blob/master/Baekjoon/16236.py">here</a>.</p><h4>Original Problem:</h4><p><a href="https://www.acmicpc.net/problem/16236">16236번: 아기 상어</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2994c44a8db" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>