Under construction

Still being built. Things may change

Jetpack

Interview

Time limit1sMemory limit64 MB

Summary
Find the lexicographically smallest schedule of screen holds that moves Barry right across N columns through a 10-row grid while avoiding obstacles.
Level

Medium6 of 10

Topics
Dynamic programming, Greedy, Array, Simulation
Solved
No attempts yet

Problem

Mirko got a new phone for his birthday. Like every kid these days he downloaded all the popular mobile games, one of which has the hero running with a jetpack on his back.

In that game the hero, Barry, runs across a field of 10 rows and NN columns of equal squares. Barry starts in the bottom left square and runs to the right at one square per second, and he has to avoid the obstacles in his way.

While Mirko holds the screen down, Barry turns on his jetpack and climbs one row per second. He keeps moving right at the same time, so he travels diagonally at 45 degrees. Once he reaches the top row he runs right along it until Mirko lets go. When Mirko releases the screen, Barry falls one row per second, and once he reaches the bottom row he runs right along that one.

Stated exactly: during second tt, counting from 0, Barry moves from column tt to column t+1t+1. If the screen is held during that second he also moves one row up, or stays in the top row if he is already there. If the screen is not held he moves one row down, or stays in the bottom row if he is already there. Every square Barry passes through must be free of obstacles.

Mirko started playing only recently and is still bad at it. Given the layout of the field, work out when he has to hold the screen so that Barry crosses all NN columns.

Input

The first line contains the integer NN, the width of the field (1≤N≤1051 \le N \le 10^5).

Each of the next 10 lines contains NN characters, either X or .. An X is an obstacle and a . is a square Barry may pass through. The first of those lines is the top row of the field and the last one is the bottom row. Barry starts in the first square of the bottom row, and that square has no obstacle. Every input admits at least one way to finish the game.

Output

Several schedules can work, so exactly one of them is required. Write a schedule as a string bb of length N−1N-1 where, counting from 0, character tt of bb is 1 if the screen is held during second tt and 0 if it is not. Among all strings that let Barry finish, output the lexicographically smallest one. In other words, at every second release the screen whenever finishing the game is still possible afterwards.

Print the number of presses PP on the first line. Each of the next PP lines describes one press with two integers tit_i and xix_i: Mirko presses the screen at second tit_i and holds it for xix_i seconds. These are the maximal blocks of consecutive 1 characters in bb listed in chronological order, so ti+xi≤ti+1t_i + x_i \le t_{i+1} and ti<Nt_i < N. When N=1N = 1, print a single line containing 0.

Examples2

  1. Example 1

    Input
    11
    .....XX...X
    ....XX...XX
    ...XX...XX.
    ...........
    ....XXX....
    ...........
    .....X.....
    ....XX...X.
    ...XX...XX.
    ...X...XX..
    
    Expected output
    2
    1 4
    7 2
    
  2. Example 2

    Input
    20
    X..................X
    .X................X.
    ..X..............X..
    ...X............X...
    ....X..........X....
    .....X........X.....
    ......X......X......
    .......X....X.......
    ........X..X........
    .........XX.........
    
    Expected output
    1
    8 10