#P1391. 方阵安排
方阵安排
Description
Class A wants a good result in the school marching competition, and they hope their class’s marching formation is a perfect square formation. They consider the formation perfect if, for every person, the number of boys among the four adjacent neighbors (up, down, left, right) is even, considering only neighbors within the grid.
You are given Class A’s current formation. You need to change as few girls as possible into boys to make the formation perfect.
Input Format
The first line contains a positive integer , meaning the formation is of size .
Lines to each contain numbers, each either or . In the -th line, the -th number represents the gender of the person at row , column , where means girl and means boy.
Output Format
Output a single number: the minimum number of girls that need to be changed into boys. If there is no solution, output .
3
0 0 0
1 0 0
0 0 0
3
Hint
Explanation for Sample 1
Change the formation to:
0 1 0
1 0 1
0 1 0
Constraints
For of the testdata, .
For of the testdata, .
Translated by ChatGPT 5
京公网安备 11011102002149号