Answers to: SNAKY - Editorialhttps://discuss.codechef.com/questions/4165/snaky-editorial<h1>PROBLEM LINKS</h1>
<p><a href="http://www.codechef.com/problems/SNAKY/">Practice</a><br>
<a href="http://www.codechef.com/COOK08/problems/SNAKY/">Contest</a></p>
<h1>DIFFICULTY</h1>
<p>EASY</p>
<h1>EXPLANATION</h1>
<p>For each cell <strong>(r, c)</strong> of the grid we can compute the time <strong>T[r][c]</strong> when the snake makes this cell free. It means that for the next <strong>T[r][c]-1</strong> moves this cell will be occupied by the snake and at the move <strong>T[r][c]</strong> tail will leave it (this cell can be immediately occupied by the head but we don't worry about that). For this just simply model the snake movements that given in the input and mark <strong>k</strong>-th cell in this sequence by the number <strong>k</strong>. (First cell is occupied by the tail and we marked it by 1 and really snake will leave it just in one move. Second cell will be free after two moves and so on). Since <strong>N, M <= 1000</strong> we can do this using usual two-dimensional array. After that, model the movement of the snake head. If at <strong>k</strong>-th step the head visits the cell <strong>(r, c)</strong> then it collides with its body if and only if <strong>k < T[r][c]</strong>. Indeed, if <strong>k < T[r][c]</strong> then by definition of <strong>T[r][c]</strong> it means that the snake body still occupies this cell and collision happens otherwise all is fine. So if at some moment we obtain this inequality then we should output "BODY" and <strong>k-1</strong>, otherwise snake will collide with the wall. So the overall complexity is <strong>O(M N + L + max{M, N})</strong> per test case (we need to clean corresponding two-dimensional array before treatment of the snake movement hence we have <strong>M N</strong> term, <strong>L</strong> corresponds to the analyzing of the snake movement and <strong>max{M, N}</strong> corresponds to the movement of the head.)</p>
<h1>SETTER'S SOLUTION</h1>
<p>Can be found <a href="http://www.codechef.com/download/Solutions/COOK_08_solutions/Setter/SNAKY.cpp">here</a>.</p>
<h1>TESTER'S SOLUTION</h1>
<p>Can be found <a href="http://www.codechef.com/download/Solutions/COOK_08_solutions/Tester/snaky.cc">here</a>.</p>enMon, 25 Mar 2019 21:28:30 -0000