TopcoderArchive
Archive
Algorithm problem

SnakeMove

Problem statement, definition, constraints, and public examples.

Problem Statement

Snake is a videogame in which the player controls a snake. The snake is simply a sequence of adjacent pixels.
You are given a screenshot of the game in the String[] board. Each character of board represents one pixel on the screen. The character '.' denotes an empty pixel. The characters '0', '1', ... denote the body of the snake: '0' is the head, '1' is the pixel next to the head, and so on until the tail. (The length of the snake will not exceed 10 pixels.)
As the snake moves, its length remains the same. During each move, all pixels that form the snake move simultaneously. The head of the snake moves one pixel in the direction it is currently pointing - i.e., away from the pixel '1'. Each other pixel shifts to occupy the location of the previous pixel of the snake. For example, the new location of pixel '1' is the old location of pixel '0'. The pixel that was previously occupied by the tail will usually become empty (but see Example 2).
You lose the game if the head of the snake tries to leave the game screen. You also lose the game if the snake tries to eat itself - i.e., if the head tries to enter a pixel that still has to be occupied by another part of the snake.
Given the current state of the board, determine whether you lose the game when the snake makes the next move. If you do lose the game, return {"YOU LOST"}. (I.e., return a String[] with a single element that is the string "YOU LOST" without the quotes.) If the game continues, return the state of the game after one move of the snake, in the same format as the input.

Definition

Class:
SnakeMove
Method:
move
Parameters:
String[]
Returns:
String[]
Method signature:
String[] move(String[] board)
(be sure your method is public)

Constraints

  • board will contain between 1 and 11 elements, inclusive.
  • Each element of board will contain between 1 and 11 characters, inclusive.
  • All elements of board will contain the same number of characters.
  • Each character in board will be '.' or a digit '0'..'9'.
  • The digits in board will form a valid snake: digits '0' and '1' will appear exactly once, the rest of the digits will appear at most once, consecutive digits will be adjacent to each other.

Examples

  1. {"10.",
     "2..",
     "34."}
    Returns: {"210", "3..", "4.." }
    The head of the snake will move right, the rest of the body will follow.
  2. {"21",
     ".0"}
    Returns: {"YOU LOST" }
    The head of the snake tries to move down but runs into the wall, so movement is impossible.
  3. {"765",
     "0.4",
     "123"}
    Returns: {"076", "1.5", "234" }
    The head of the snake will move up, to a cell currently occupied by the tail which will slither out of the way.
  4. {"8765",
     ".0.4",
     ".123"}
    Returns: {"YOU LOST" }
    The head of the snake tries to move up but runs into the part of the body which will not move out of the way in time.
  5. {".012.",
     "..43.",
     "..5.9",
     "..678"}
    Returns: {"0123.", "..54.", "..6..", "..789" }
Back to all problems