Under construction

Still being built. Things may change

Two Doors

Interview

Time limit1sMemory limit1024 MB

Summary
Given n doors and m switches that flip doors, find a set of switches that makes the passage shift time by each i from -n to n years, or report that none exists.
Level

Medium4 of 10

Topics
Brute force, Bit manipulation
Solved
No attempts yet

Problem

Dodo set out to find Ea, who was swept away by a storm across space and time. Dodo knows only that Ea is between −n-n years and nn years in the future from now. While searching for a time machine, Dodo learned about a strange door from the evil Black Wizard.

The door has a front side and a back side. The front side is always 1 year ahead of the back side. In other words, going through the door from front to back takes you 1 year into the past, and going through from back to front takes you 1 year into the future.

The devilish Black Wizard would not simply give Dodo the door. Instead, he offered a passage made of nn doors joined together. After long study, Dodo installed mm switches, each of which turns a specific combination of doors 180 degrees. Now let us use these switches to rescue Ea!

Input

The first line contains nn (1≤n≤1001 \le n \le 100) and mm (1≤m≤201 \le m \le 20).

The second line contains a string of length nn made only of 0 and 1. The jj-th character gives the initial state of the jj-th door. 0 means the front side faces Dodo, and 1 means the back side faces Dodo.

From the third line to line m+2m+2, there are mm strings of length nn made only of 0 and 1. The jj-th character of the (i+2)(i+2)-th line tells whether switch ii turns door jj 180 degrees. 0 means it does not turn the door, and 1 means it does.

Output

Print (2n+1)(2n+1) lines in total. For every −n≤i≤n-n \le i \le n, print the answer for trying to go ii years into the future on line (i+n+1)(i+n+1).

On each line, if a solution exists, print the number kk (0≤k≤m0 \le k \le m) of switches to press, followed by the switch numbers separated by spaces. If no solution exists, print -1.

If there are several combinations of switches to press, print any one of them.

Hint

In the figure, red means the front side of a door faces you and blue means the back side faces you. The upper passage in the figure is the initial state. Passing through this passage takes you −3-3 years into the future, that is, 3 years into the past. Pressing switch 2 changes the passage to the lower one. Passing through that passage takes you 1 year into the future.

alt text

Examples1

  1. Example 1

    Input
    3 2
    000
    010
    101
    
    Expected output
    0
    -1
    1 1
    -1
    1 2
    -1
    2 1 2