In a matrix of characters, a cursor is a movable position between two adjacent characters, before the first character or after the last character in a line, like that "I"-shaped indicator which moves while you type.
In this challenge, a cursor position is considered valid if at least one of its two sides touches a space. The cursor travels through valid positions in a matrix, by moving up, down, left or right by one position each time. It could also move left or right though the line separator, to the end of the previous line, or the beginning of the next line respectively.
Given a character matrix as input, and a cursor initially at the top left corner, your task is to determine whether it can reach the bottom line.
- You can assume that the input consists only of
- You may require there must or must not be a trailing newline in the input, if you want to.
- Your code should output either a truthy value (if the condition is true), or a falsy value (if the condition is false).
░ is the space character):
░.....░ ░...... .░.░.░. .░░░░░░ ......░
The cursor can reach the bottom in this case. This process of moving the cursor will work: down, right(touches spaces on the left), down(touches spaces on the right), down, right(touches spaces on both sides) 6 times, and down(touching spaces on the left).
This will also work(
░ still represent spaces):
░░ .░ .░
The cursor starts at the up-left corner. After moving right two times, it can move down twice, which touches the bottom of the lines.
This example will not work(
░ represent spaces again):
The cursor cannot move down, as there is no sufficient whitespace to be touched. There are only newline characters on the right of the input, and there are no newline characters on the left(otherwise a line would have spanned two lines.)
The cursor can also move up and left, in order to solve a maze-like input as in this one:
░.... ░░░░. ...░. .░░░. ░.... ░░░░░
In this case, the cursor has to move left in order to get through spaces and get to the bottom line.
Here is another important test case: the cursor can move left or right to another line.
░░░░ ...░ ░...
In this case, the cursor can reach the last line, as the cursor can move left or right in order to get to the next line. Moving the cursor down is blocked on the right position of row 2, column 4; however, the cursor can still get to the last line if it moves right (touching spaces).
This is a fairly interesting test input:
░░..░ ░...░ ....░
In this case, the cursor can reach the last line, by moving down and left to the end of the first line, then down twice to reach the end of the bottom line.