## NOI '11 P6 - Bunny and Eggy's Game

View as PDF

Points: 25 (partial)
Time limit: 0.6s
Memory limit: 256M

Problem type
##### National Olympiad in Informatics, China, 2011

Recently, Bunny and Eggy have developed an interest in a new type of board game.

This game is played on an rows tall by columns wide board. Once the game starts, the board will contain a single square that is empty. All other squares will contain a single piece that's either black or white.

Each game will always have Bunny go first, followed by them alternating. The general steps are:

• Every time Bunny moves, she will select a white piece adjacent to the empty square and move it onto the space.
• Every time Eggy moves, he will select a black piece adjacent to the empty square and move it onto the space.

The first person that cannot follow the above loses the game. To simplify our description, we denote the action of "moving the piece from row , column to the empty space" as . The following are three examples of entire games.

Figure 1

Figure 2

Figure 3

Bunny has been losing a lot recently, and thus Eggy has been getting overly arrogant. Bunny wants to invite her good friend – you! – to help her. She has brought over a game which she has lost to Eggy, and wishes for you to point out all of the "mistakes" she made.

Note:

• Two squares are considered adjacent if and only if they share an edge.
• A move of Bunny's is a "mistake" if and only if Bunny had a guaranteed winning strategy before it was made, and Eggy had a guaranteed winning strategy after it was made.

#### Input Specification

The first line of input contains two positive integers and .
The following lines will describe the game board. The -th of these lines will contain characters, where each character can either be an uppercase X, an uppercase O, or a period .. Respectively, they represent a black piece on a square, a white piece on a square, and an empty square. The . character will appear exactly once.
The following line contains a single integer , indicating that Bunny and Eggy each made moves.
The following 2 lines will describe the process of a game. The th of these lines will represent Bunny's -th move, and the 2-th of these lines will represent Eggy's -th move. Each move will be described using two integers and , indicating that the piece in row , column will be moved onto the empty square.
The input guarantees that there will be exactly one empty square, all moves made by Bunny and Eggy in the process are valid, and that Eggy will be the winner at the end.

#### Output Specification

The first line of output should contain a single integer , representing the total number of times that Bunny has made a mistake.
The following lines should give the actual move numbers of the mistakes in ascending order. The -th of these lines should contain a single integer , indicating the -th mistake Bunny made is her -th move in the game.

#### Sample Input 1

1 6
XO.OXO
1
1 2
1 1

#### Sample Output 1

1
1

#### Explanation for Sample Output 1

This corresponds to the example in figure 1.

#### Sample Input 2

3 3
XOX
O.O
XOX
4
2 3
1 3
1 2
1 1
2 1
3 1
3 2
3 3

#### Sample Output 2

0

#### Explanation for Sample Output 2

This corresponds to the example in figure 3.

#### Sample Input 3

4 4
OOXX
OXXO
OO.O
XXXO
2
3 2
2 2
1 2
1 3

#### Sample Output 3

2
1
2

#### Constraints

The attributes of all the test cases are outlined below.

Test Case Range of Range of
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

Problem translated to English by Alex.