Mock CCC '19 Contest 1 S2 - Pusheen's Puzzle Present

View as PDF

Submit solution

Points: 5 (partial)
Time limit: 1.0s
Memory limit: 256M

Problem type
Allowed languages
Ada, Assembly, Awk, Brain****, C, C#, C++, COBOL, CommonLisp, D, Dart, F#, Forth, Fortran, Go, Groovy, Haskell, Intercal, Java, JS, Kotlin, Lisp, Lua, Nim, ObjC, OCaml, Octave, Pascal, Perl, PHP, Pike, Prolog, Python, Racket, Ruby, Rust, Scala, Scheme, Sed, Swift, TCL, Text, Turing, VB, Zig

Pusheen has received a present from her friend! It turns out to be a puzzle similar to the 15-puzzle.

The puzzle is an N \times N grid where each integer from 1 to N^2 appears on the grid exactly once. In one move, Pusheen can take any K \times K subgrid and rotate it in place by 90 degrees, either clockwise or counter-clockwise. K can vary from move to move. The goal is for the integers to be in sorted order when read in row-major order - that is to say, each row is in sorted order, each column is in sorted order, and if the integers are read in order from the first row to the last row, going from left to right, the integers form the sequence 1 to N^2.

Pusheen is getting started on this puzzle, and has it set to the Easy difficulty. On Easy difficulty, Pusheen is able to solve the puzzle by rotating exactly one subgrid by 90 degrees. Help Pusheen determine the size of the subgrid she should rotate.


2 \le N \le 10^3

In tests worth 1 mark, N = 2.

In additional tests worth 2 marks, N \le 3.

In additional tests worth 11 marks, N \le 20.

Input Specification

The first line of input is a single positive integer, N.

The next N lines each contain N space-separated positive integers. These lines define the arrangement of numbers on the grid.

Output Specification

Output, on a single line, the size of the subgrid Pusheen will rotate. It is guaranteed that this integer will be at least 2, and will be unique.

Sample Input

1 2 3
4 8 5
7 9 6

Sample Output



  • 1
    This_CKaps  commented on Feb. 7, 2019, 3:14 p.m.

    Could someone give me some suggestions for test-cases to try-out? My program outputs 1, although I cannot find a situation where it would do so... I understand that the minimum size of sub-grid to rotate is 2.