eolymp
bolt
Try our new interface for solving problems
Məsələlər

Rotating Rings

Rotating Rings

\includegraphics{https://static.e-olymp.com/content/9a/9abce178a64e0e3c5855258ddf8db0df4a47f5f7.jpg} Any square grid can be viewed as one or more rings, one inside the other. For example, as shown in figure (a), a \textbf{5}×\textbf{5} grid is made of three rings, numbered \textbf{1}, \textbf{2} and \textbf{3} (from outside to inside.) A square grid of size \textbf{N} is said to be sorted, if it includes the values from \textbf{1} to \textbf{N^2} in a row-major order, as shown in figure (b) for \textbf{N = 4}. We would like to determine if a given square grid can be sorted by only rotating its rings. For example, the grid in figure (c) can be sorted by rotating the first ring two places counter-clockwise, and rotating the second ring one place in the clockwise direction. \InputFile Your program will be tested on one or more test cases. The first input line of a test case is an integer \textbf{N} which is the size of the grid. \textbf{N} input lines will follow, each line made of N integer values specifying the values in the grid in a row-major order. Note than \textbf{0} < \textbf{N} ≤ \textbf{1000} and grid values are natural numbers less than or equal to \textbf{1000000}. The end of the test cases is identified with a dummy test case with \textbf{N = 0}. \OutputFile For each test case, output the result on a single line using the following format: \textbf{k. result} Where \textbf{k} is the test case number (starting at \textbf{1}), and result is "\textbf{YES}" or "\textbf{NO}" (without the double quotes.)
Zaman məhdudiyyəti 1 saniyə
Yaddaşı istafadə məhdudiyyəti 64 MiB
Giriş verilənləri #1
4
9 5 1 2
13 7 11 3
14 6 10 4
15 16 12 8
0
Çıxış verilənləri #1
1. YES