Statistics

Problem Statement for "ChristmasSnake"

Problem Statement

It's Christmas and that means presents!

Well, this sucks. Your baby brother got a brand new PS5 while you got new socks and a subscription to the Snake Enthusiast Quarterly.

Watching your baby brother unwrap and plug in the PS5 hurts. To get rid of those thoughts, you pull out your trusty Nokia 3310 and launch a game of snake. And soon you are distracted by a snake puzzle.


The game world, given in the String[] game is an 8x8 bitmap. Somewhere in that world is a snake: a sequence of up to 26 distinct side-adjacent cells. The snake moves in steps. In each step, its head pixel moves into an adjacent empty cell, and simultaneously each other body part moves to the location that was previously occupied by its neigboring body part that is closer to the head (along the snake).

Example snake movement is shown below. The snake is given as the sequence of letters "abcdefgh", with 'a' being the head. (This exact format, generalized to an arbitrary length, will be used as the input format.) The head moved to the left.

........           ........
....hg..           .....h..
..ab.f..           .abc.g..
...cde..  becomes  ...def..
........           ........
........           ........
........           ........
........           ........

As a special case, the head is allowed to move into the cell currently occupied by the tail, as in the same movement the tail will leave that cell. (The head cannot move into another body cell, and it cannot attempt to leave the game world.)


We will call an empty cell clearly reachable if the head can reach that cell by a sequence of moves such that during the sequence the head only visits cells that are empty at this moment. (In other words, a clearly reachable cell would be reachable even if the snake grew after each move, which would mean that its tail remains in place.)

Your snake is currently in a fairly nice position:

  • it doesn't touch the walls of the game world.
  • A strict majority of empty cells in the game world are currently clearly reachable.

Find out whether there is a way to turn the snake around in at most 80 moves. If yes, return any one such sequence of moves. If not, return "impossible". Use 'N', 'S', 'E', 'W' to denote moves up, down, right, and left.

Definition

Class:
ChristmasSnake
Method:
turnAround
Parameters:
String[]
Returns:
String
Method signature:
String turnAround(String[] game)
(be sure your method is public)

Notes

  • There is no fruit anywhere in this problem. Hence, the snake never grows, it just moves.
  • A snake is turned around when it occupies the same sequence of squares but in the opposite direction. Note that it isn't sufficient to occupy the same set of squares, the exact order in which the snake passes through them matters.

Constraints

  • game will contain 8 elements.
  • Each element of game will contain 8 characters.
  • For some N (between 1 and 26, inclusive) game will contain each of the first N lowercase English letters exactly once.
  • All other characters in game will be periods ('.').
  • The letters in game will form a valid snake with the given properties.

Examples

  1. {"........", "....hg..", "..ab.f..", "...cde..", "........", "........", "........", "........"}

    Returns: "WSSSSENNNNESEENNW"

    The sample snake from the problem statement. The first move of the sample solution corresponds to the move illustrated in the example above. After the next five moves the snake looks as follows: ........ ........ .fgh.... .e...... .d...... .c...... .ba..... ........

  2. {"........", "........", "..a.....", "........", "........", "........", "........", "........"}

    Returns: "WSNNSE"

    Easiest game ever. Doing nothing was also a valid solution, but we do not have to minimize the length of the solution.

  3. {"........", ".y.ghij.", ".x.fa.k.", ".w.eb.l.", ".v.dc.m.", ".u....n.", ".tsrqpo.", "........"};

    Returns: "ESSSWWWNNNNNEEEEESSSSSSSWWWWWWWNNNNNNNEESSSSSEEENNNWSSWNNNEEESSSSSWWWWWNNNNN"

  4. {"........", "..opqtu.", "..n.rsv.", "..m...w.", ".klab.x.", ".jgfc.y.", ".ihed.z.", "........"}

    Returns: "NEESSSSWWWWWNNNNNNNEEEEEEESSSSSSSWWNNNNWWSESSWNWSWNNENNNEESENESSSSS"

  5. {"........", ".abijkl.", ".dchyzm.", ".efgxwn.", "...tuvo.", "...srqp.", "........", "........"}

    Returns: "NEEEEEESSSSSSSWWWWWWWNNNNNNNESESWSEENNEEESSSSWWWNEENWNE"

  6. {"........", "........", "........", ".abcdef.", "........", "........", "........", "........"}

    Returns: "WNNNEEEEEEESSSSSSSWWWWWWWNNNNEEEEEE"

  7. {"........", ".tsrqpo.", ".a....n.", ".b....m.", ".c....l.", ".d....k.", ".efghij.", "........"}

    Returns: "WNNEEEEEEESSSSSSSWWWWWWWNNNNNESSSSEEEEENNNNNWWWWW"

  8. {"........", ".....qp.", "....sro.", ".abctmn.", "..edkl..", "..f.j...", "..ghi...", "........"}

    Returns: "WNNNEEEEEEESSSSSSSWWWWWWWNNNNEEESWSSEENNENENNWSWS"

  9. {"........", "........", "........", "........", "....ba..", "....cfg.", "....deh.", "........"}

    Returns: "EESSSWWWWWWWNNNNNNNEEEEEEESSSSWWWSSENES"

  10. {"........", ".ba.....", ".czwvu..", ".dyx.ts.", ".e.kl.r.", ".fijmnq.", ".gh..op.", "........"}

    Returns: "NEEEEESSSSSSSWWWWWWWNNNNNNNEESWSSSSSENENESESENNNWNWWSWN"

  11. {"........", ".nmlkhg.", ".op.jif.", ".rq...e.", ".sz...d.", ".tyx..c.", ".uvw.ab.", "........"}

    Returns: "SWWWWWNNNNNNNEEEEEEESSSSSSSWWNENNNNNWSWNWWWSESWSSSEENWN"

  12. {"........", ".....ef.", ".....da.", ".....cb.", "........", "........", "........", "........"}

    Returns: "ESSSSSWWWWWWWNNNNNNNEEEEEEESSWSWNNE"

  13. {"........", "..opcba.", "..nqd...", ".lmre...", ".ktsf...", ".jihg...", "........", "........"}

    Returns: "NESSSSSSSWWWWWWWNNNNNNNEEEEEESWWSSSSWWWNNENNESSSW"

  14. {"........", "..ed....", ".gfcb...", ".hi.a...", "..jk....", ".tulmn..", ".srqpo..", "........"}

    Returns: "EEESSSSWWWWWWWNNNNNNNEEEEEEESSSWWWNWNWSWSESESEESWWWWNE"

  15. {"........", "...efkj.", "...dghi.", "...cb...", "....a...", "........", "........", "........"}

    Returns: "EEESSSWWWWWWWNNNNNNNEEEEEEESSSSWWWNWNNESEENW"

  16. {"........", ".stab...", ".rudc...", ".qfe....", ".pgh....", ".o.ij...", ".nmlk...", "........"}

    Returns: "NEEEESSSSSSSWWWWWWWNNNNNNNEEESESWSWSESESWWWNNNNNES"

  17. {"........", ".ihgfab.", ".jk.edc.", "..lqrsv.", "..mp.tu.", "..no....", "........", "........"}

    Returns: "NEESSSSSSSWWWWWWWNNNNNNNEEEEESESWWNWWWSESSSENNEESEN"

  18. {"........", ".....lm.", ".....kn.", ".....jo.", ".a.efip.", ".bcdghq.", ".wvutsr.", "........"}

    Returns: "WNNNNEEEEEEESSSSSSSWWWWWWWNNNESEENESENNNNESSSSSWWWWW"

  19. {"........", ".ut..ab.", ".vs...c.", ".wr...d.", ".xq..fe.", ".ypmlgh.", ".zonkji.", "........"}

    Returns: "NEESSSSSSSWWWWWWWNNNNNNNEEEEESESSSWSESWWNWSWNNNNNWSSSSS"

  20. {"........", "..hgf...", "..i.e...", ".kj.dc..", ".lmnob..", ".uvqpa..", ".tsr....", "........"}

    Returns: "EESSWWWWWWWNNNNNNNEEEEEEESSSSSWWNNWNNWWSSWSEEESWSWWNE"

  21. {"........", ".mnop...", ".lsrq...", ".ktuvw..", ".jedcx..", ".ifaby..", ".hg..z..", "........"}

    Returns: "SSWWWNNNNNNNEEEEEEESSSSSSSWWWWNNENWWSSWNNNNNEEESWWSEEESSS"

  22. {"........", "...wvsr.", ".zyxutq.", "..lmnop.", ".jk..ba.", ".ihg.c..", "...fed..", "........"}

    Returns: "ESSSWWWWWWWNNNNNNNEEEEEEESSSSWWSSWWNWWNENEEEENNWSWNWSWW"

  23. {"........", ".fghijk.", ".edcbal.", "......m.", ".wxyz.n.", ".v....o.", ".utsrqp.", "........"}

    Returns: "SWWWWWNNNEEEEEEESSSSSSSWWWWWWWNNNNEEEEENWWWWNEEEEESSSSSWWWWWNNEEE"

  24. {"........", ".hijklm.", ".gfcban.", "..ed..o.", "......p.", ".xwvutq.", ".yz..sr.", "........"}

    Returns: "SSWWWWWNNNNEEEEEEESSSSSSSWWWWWWWNNNEEEEENNWWSWNWNEEEEESSSSSWNWWWWSE"

  25. {"........", ".tuvwxy.", ".s......", ".r.defg.", ".q.cbah.", ".p....i.", ".onmlkj.", "........" }

    Returns: "SWWWNNNEEEEESSSSSWWWWWWWNNNNNNNEEEEEEESSWWWWWSSSEEENWWNEEESSSWWWWWNNNNNEEEEE"

  26. {"........", "....z...", ".ji.yxw.", ".kh...v.", ".lg.abu.", ".mfedct.", ".nopqrs.", "........" }

    Returns: "WNNNNEEEESSSSSSSWWWWWWWNNNNNNNEEESSSSEESWWWNNNWSSSSEEEEENNNNWWN"

  27. {"........", "........", ".dcba...", ".efghij.", ".ponmlk.", ".qrstuv.", "...zyxw.", "........" }

    Returns: "NNEEESSSSSSSWWWWWWWNNNNNNNEEEESSWWWSEEEEESWWWWWSEEEEESWWW"

  28. {"........", "........", "..efghi.", "..d...j.", "..cba.k.", "......l.", ".rqponm.", "........" }

    Returns: "SWWWWNNNNNEEEEEEESSSSSSSWWWWWWWNNEEEENWWNNEEEESSSSWWWWW"

  29. {"........", "........", "..efgh..", "..da.i..", "..cb.j..", ".....k..", "........", "........" }

    Returns: "ESSSSWWWWNNNNNNNEEEEEEESSSSSSSWWWNNNNWSWNNEEESSS"

  30. {"........", ".lkjihg.", ".m....f.", ".n....e.", ".o....d.", ".p....c.", ".qrs.ab.", "........" }

    Returns: "SWWWWWNNNNNNNEEEEEEESSSSSSSWWNENNNNNWWWWWSSSSSEE"

  31. {"........", ".efgh...", ".da.i...", ".cb.j...", "........", "........", "........", "........" }

    Returns: "ESSWWWNNNNEEEEEEESSSSSSSWWWWWWWNNNEEENNWSWNNEEESS"


This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2024, TopCoder, Inc. All rights reserved.
This problem was used for: