Number of Papers
Time limit2sMemory limit256 MB
Given an N x N grid of -1, 0, 1 values, recursively split non-uniform blocks into 9 equal sub-squares and count how many final uniform blocks hold each value.
- Level
Medium5 of 10
- Topics
- Divide and conquer, Recursion, Matrix, Implementation
- Solved
- No attempts yet
Problem
You are given an N×N square paper represented as a matrix. Each cell contains one of the numbers -1, 0, and 1.
Cut the paper by the following rules.
- If every cell in the current paper contains the same number, use that paper as it is and do not cut it further.
- Otherwise, divide the current paper into 9 equal square pieces, and repeat the same process for each piece.
After all cutting is complete, find the number of papers made only of -1, the number of papers made only of 0, and the number of papers made only of 1.
Input
The first line contains N. N is at least 1, at most 3^7, and is a power of 3.
The next N lines contain the matrix representing the paper. Each line contains N integers separated by spaces, and each integer is one of -1, 0, and 1.
Output
On the first line, print the number of papers made only of -1.
On the second line, print the number of papers made only of 0.
On the third line, print the number of papers made only of 1.