Image | 프로그래밍의 벗 PivotOJ
PivotOJ

Image

시간 제한: 2000ms메모리 제한: 1024MB출처: NOI 2000BOJ 9920
이 문제는 본문 이미지 일부가 표시되지 않습니다. 텍스트만으로 풀이가 어려울 수 있습니다.

문제

This task concerns the output length of the quadtree image compression scheme.  Only L×LL \times L images consisting of L2L^2 pixels are considered.  An image pixel is either a 00-pixel or a 11-pixel.

The quadtree image compression scheme is as follows:

  1. If the image consists of both 00-pixels and 11-pixels, encode a 11 to indicate that the image will be partitioned into 44 sub-images as described in Step (2).  Otherwise, encode the entire image as 0000 or 0101 to indicate that the image consists of only 00-pixels or only 11-pixels respectively.

  2. An image II is partitioned into 44 equal size sub-images AA, BB, CC, DD as shown:

    [이미지 1]

    Step (1) is then performed on each of the four sub-images in the order of AA, BB, CC, DD.

Let (I)(I) be the encoding of the image II.  The following examples show the encoding process to compress an image.

Example 1.  This example shows the encoding process of a 4×44 \times 4 image.

[1111110101000011]=1[0100][1111][0011][1101]=11[0][0][0][1]011[1][0][1][0]1[0][1][1][1]=110000000101101000100100010101\begin{align*} \begin{bmatrix} 1&1&1&1\\1&1&0&1 \\ 0&1&0&0 \\ 0&0&1&1\end{bmatrix} & = 1 \begin{bmatrix}0&1\\0&0\end{bmatrix} \begin{bmatrix}1&1\\1&1\end{bmatrix} \begin{bmatrix}0&0\\1&1\end{bmatrix} \begin{bmatrix}1&1\\0&1\end{bmatrix} \\ & = 1 \, 1\begin{bmatrix}0\end{bmatrix}\begin{bmatrix}0\end{bmatrix}\begin{bmatrix}0\end{bmatrix}\begin{bmatrix}1\end{bmatrix} \, 01 \, 1\begin{bmatrix}1\end{bmatrix}\begin{bmatrix}0\end{bmatrix}\begin{bmatrix}1\end{bmatrix}\begin{bmatrix}0\end{bmatrix} \,1\begin{bmatrix}0\end{bmatrix}\begin{bmatrix}1\end{bmatrix}\begin{bmatrix}1\end{bmatrix}\begin{bmatrix}1\end{bmatrix} \\ & = 1\,1\,00\,00\,00\,01\,01\,1\,01\,00\,01\,00\,1\,00\,01\,01\,01\end{align*}

Since there are 3030 bits (00’s and 11’s) in the encoding, the length of the compressed image is 3030.

Example 2.  This example shows the encoding process of an 8×88 \times 8 image.

[0000111100001111000011110000111111111111111111111111111111111111]=1[1111111111111111][0000000000000000][1111111111111111][1111111111111111]=101000101\begin{align*} \begin{bmatrix} 0&0&0&0&1&1&1&1\\0&0&0&0&1&1&1&1\\0&0&0&0&1&1&1&1\\0&0&0&0&1&1&1&1\\1&1&1&1&1&1&1&1\\1&1&1&1&1&1&1&1\\1&1&1&1&1&1&1&1\\1&1&1&1&1&1&1&1\end{bmatrix} & = 1 \begin{bmatrix}1&1&1&1\\1&1&1&1\\1&1&1&1\\1&1&1&1\end{bmatrix} \begin{bmatrix}0&0&0&0\\0&0&0&0\\0&0&0&0\\0&0&0&0\end{bmatrix} \begin{bmatrix}1&1&1&1\\1&1&1&1\\1&1&1&1\\1&1&1&1\end{bmatrix} \begin{bmatrix}1&1&1&1\\1&1&1&1\\1&1&1&1\\1&1&1&1\end{bmatrix} \\ & = 1\,01\,00\,01\,01\end{align*}

Thus the length of the compressed image is 99 (bits).

  1. Read an L×LL \times L image (1L641 \le L \le 64 and LL is a power of 22) from the input.
  2. Compute the length (the number of bits) of the compressed image encoded by the quadtree compression scheme.
  3. Write the length to the output.

입력

For an L×LL \times L square image, the input file contains L+1L + 1 lines.  The first line consists of the single integer LL.  Each line of the subsequent LL lines consists of LL bits (a bit is either a 00 or a 11) with a blank between two adjacent bits.

출력

The output file consists of one integer which is the length (the number of bits) in the compressed image.

예제

예제 1

입력
4
1 1 1 1
1 1 0 1
0 1 0 0
0 0 1 1
출력
30

예제 2

입력
8
0 0 0 0 1 1 1 1
0 0 0 0 1 1 1 1
0 0 0 0 1 1 1 1
0 0 0 0 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
출력
9
코드를 제출하려면 로그인하세요.