#P7630. [COCI 2011/2012 #1] SKAKAC

[COCI 2011/2012 #1] SKAKAC

Description

Mirko and Slavko are playing a game.

Mirko places a knight piece on an N×NN \times N chessboard, covers Slavko's eyes, and then moves the knight for TT steps, one move per second. After that, Slavko must guess the knight's final position to win.

The chessboard in this game is special, because each square is forbidden for part of the time. More precisely, each square has a positive integer label. A square labeled with the number KK is passable only at times 0,K,K×2,K×3,...0, K, K \times 2, K \times 3, ... seconds, and at all other times this square is forbidden. Of course, the knight may enter a square only when that square is passable.

The game starts at time 00. Every second, Mirko must move the knight by one step (according to the rules of chess: the knight moves in an L-shape, like the horse in Chinese chess). Please help Slavko write a program to compute all squares where the knight may be located after TT seconds.

Input Format

The first line contains two positive integers N,TN, T.

The second line contains two positive integers X,YX, Y, representing the knight's initial coordinates.

The next NN lines each contain NN positive integers not exceeding 10910^9, describing the chessboard.

Output Format

The first line contains a non-negative integer MM, the number of squares where the knight may be located after TT seconds.

The next MM lines each contain one coordinate, indicating a square where the knight may be located after TT seconds. Output the coordinates sorted by increasing row number, and if the row numbers are the same, by increasing column number.

3 2
1 1
1 3 2
2 3 2
3 1 1
2
1 1
1 3
5 6
2 3
4 5 3 2 3
1 3 4 3 1
3 4 1 3 2
4 4 2 1 3
4 6 4 9 2
5
1 4
2 1
2 5
4 5
5 2
3 3
2 2
3 6 4
2 2 5
1 3 7
0

Hint

Sample 1 Explanation

The state of the chessboard is shown in the figure below. . represents a passable square, # represents a forbidden square, and K represents a square where the knight may be located.

Constraints

For 40%40\% of the testdata, T5×104T \le 5 \times 10^4.

For 100%100\% of the testdata, 3N303 \le N \le 30, 1T1061 \le T \le 10^6.

Notes

The score of this problem follows the original COCI settings, with a full score of 160160.

Translated from COCI2011-2012 CONTEST #1 T6 SKAKAC.

Translated by ChatGPT 5