5
There is a mouse in a room, in the top-left corner. It can move up, down, left, or right, and the mouse cannot go through any cell in the room twice. Given this, return all the possible paths in which the mouse will visit every square.
Input: Two integers m
and n
, which represent the m x n
room. m<9, n<9.
Output: The m x n
room, with ^<>v
noting the direction the path goes. Each line of the room will be seperated by a newline, and each room will be seperated by one newline. The last room the mouse enters will be noted by a dot.
v > v .
v ^ v ^
> ^ > ^
next room goes here...
Test Cases:
Input: 2 3
, output:
v > v
> ^ .
> > v
. < <
v . <
> > ^
Shortest code wins.
Is there any restriction about the very last step? Since the mouse already entered each cell, any of the four directions would be ok. – Howard – 2012-07-01T15:20:36.883
@Howard: Whoops! The mouse can only go through cells once. – beary605 – 2012-07-01T16:01:55.900
1So that means that the last step must go "outside" of the grid? Because the very last example in your test cases does not fulfill this rule. (should be
v^<
,>>^
then) – Howard – 2012-07-01T16:13:49.237@Howard: Ok, I see what you are getting at now. The last step is unrestricted. – beary605 – 2012-07-01T16:22:10.703
1I edited your question to improve the example output formatting. While doing so, I also moved the dot in the last example output room, since it appeared to be misplaced. (The mouse cannot end its path in the upper left corner, since that's where it started.) Please correct me if I made any mistakes. – Ilmari Karonen – 2012-07-02T15:42:00.537
@IlmariKaronen: That's a much better output. Thanks! – beary605 – 2012-07-02T15:43:37.713