This page is still under construction.

Parts of this page are still being built. What you see may change.

Cows on Skates

Interview

Time limit1sMemory limit128 MB

Summary
Find a shortest orthogonal path from (1,1) to (R,C) through open grid cells, breaking ties by the lexicographically smallest sequence of cells.
Level

Medium5 of 10

Topics
BFS, Graph, Matrix, Shortest path
Solved
No attempts yet

Problem

After years of pleading, Farmer John finally gave in and bought roller skates for the entire herd of cows. Much of the farm is perfect for roller-skating, but a few squares have far too many rocks and cannot be crossed on skates.

The farm is represented as a grid with RR rows and CC columns (1≤R≤1131 \le R \le 113, 1≤C≤771 \le C \le 77). Around feeding time, Bessie finds herself at square (1,1)(1, 1) and wants to get back to the barn at square (R,C)(R, C). She may skate only between orthogonally adjacent squares (sharing an edge, never diagonally) that do not contain rocks. It is guaranteed that at least one such route from (1,1)(1, 1) to (R,C)(R, C) exists.

Here (r,c)(r, c) denotes the square in row rr and column cc, with rows numbered from 11 at the top and columns from 11 at the left.

Input

  • Line 1: two space-separated integers RR and CC.
  • Lines 2…R+12 \ldots R+1: each line contains CC characters with no spaces. Each character is either '.', meaning Bessie can skate on that square, or '*', meaning the square has too many rocks. The jj-th character of the ii-th of these lines describes square (i,j)(i, j). Squares (1,1)(1, 1) and (R,C)(R, C) are always '.'.

Output

Output the shortest path from (1,1)(1, 1) to (R,C)(R, C). A path is a sequence of squares that starts at (1,1)(1, 1) and ends at (R,C)(R, C), in which consecutive squares are orthogonally adjacent (sharing an edge) and no square is a rock. The shortest path is the one containing the fewest squares.

If several shortest paths exist, output the lexicographically smallest one. Compare the two paths as sequences of squares from the beginning; each square is treated as the pair (row,column)(\text{row}, \text{column}) and pairs are compared by row first, then by column.

Print the squares of the path one per line, each as two space-separated integers 'row col'. The first line must be '1 1' and the last line must be 'R C' (that is, RR and CC).

Examples6

  1. Example 1

    Input
    5 8
    ..*...**
    *.*.*.**
    *...*...
    *.*.*.*.
    ....*.*.
    
    Expected output
    1 1
    1 2
    2 2
    3 2
    3 3
    3 4
    2 4
    1 4
    1 5
    1 6
    2 6
    3 6
    3 7
    3 8
    4 8
    5 8
    
  2. Example 2

    Input
    1 1
    .
    
    Expected output
    1 1
    
  3. Example 3

    Input
    1 5
    .....
    
    Expected output
    1 1
    1 2
    1 3
    1 4
    1 5
    
  4. Example 4

    Input
    5 1
    .
    .
    .
    .
    .
    
    Expected output
    1 1
    2 1
    3 1
    4 1
    5 1
    
  5. Example 5

    Input
    2 2
    ..
    ..
    
    Expected output
    1 1
    1 2
    2 2
    
  6. Example 6

    Input
    3 3
    ...
    .*.
    ...
    
    Expected output
    1 1
    1 2
    1 3
    2 3
    3 3