Two Triangles
Time limit2sMemory limit1024 MB
Given two triangular arrays of 0s and 1s with size N, rotate or reflect A to find the fewest positions where it differs from B.
- Level
Easy3 of 10
- Topics
- Brute force, Implementation, Matrix
- Solved
- No attempts yet
Problem
The first line has number, the second line has numbers, and so on, up to line with numbers. Triangles and are arranged as shown in the figure below. Each position holds or .

You can rotate the triangle clockwise or counterclockwise, or reflect it left to right.
For example, rotating triangle gives the triangles below.

Reflecting gives the triangle below.

The difference between two triangles is the number of positions where their values differ when the two triangles are overlapped.
For example, when and are overlapped, the values differ at the leftmost position of row 2 and at the leftmost and rightmost positions of row 3. So the difference between and is . However, if is rotated counterclockwise (the right triangle in the second figure) and overlapped with , only the second position from the left in row 3 differs. So that difference is .
You are given triangles and . You can rotate and reflect as many times as you want, or not at all. There is no limit on the number of rotations or reflections.
Rotate or reflect to make its difference from as small as possible. Find that minimum difference.
Input
The first line contains the size of and .
Lines 2 through give the values at each position of . Line () contains the integers in row of , separated by spaces, from left to right.
Lines through give the values at each position of . Line () contains the integers in row of , separated by spaces, from left to right.
Output
Print the minimum difference from among all triangles you can get by rotating and reflecting .
Constraints
- All given numbers are integers.
- Each value in and is or .