22163_Flipthecoins

2022-5-16 18:21| 发布者: Hocassian| 查看: 22| 评论: 0|原作者: 肇庆学院ACM合集

摘要:
C:\Users\Administrator\Downloads\2019-10-12-10-14-5-89506164826300-Problem List-采集的数据-后羿采集器.html

Pro.ID

22163

Title

Flip the coins

Title链接

http://10.20.2.8/oj/exercise/problem?problem_id=22163

AC

0

Submit

0

Ratio

-

时间&空间限制

  • Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Others)
  • 描述

    Have you ever played the game like this? There are coins placed on an N*N matrix. Initially some coins are facing upwards, and some are facing downwards. Each time, you can choose one of the following rules to take a move.

    Select one row of the matrix and flip all coins in that row, which means replace all upward coins with downward coins and all downward coins with upward coins.

    Select one column of the matrix and flip all coins in that column.

    Select one diagonal of the matrix and flip all coins in that diagonal. Flip coins in diagonals is shown as Figure 1. Notice that there are 2*N diagonal in total. You can recognize them when taking the following picture.

    Figure 1 Flip coins in diagonals

    Your task is to use minimal movement to achieve an even number of downward-faced coins in each row and each column.

    输入

    The first line of input there is one integer T ( T ≤ 100 ), giving the number of test cases in the input. Each test case starts with a line containing a positive integer N ( N ≤ 20 ), representing the size of the matrix. Next N line each line contains N integer, giving the status of coins in that row. Value 0 means the coin is facing upwards, while value 1 means the coin is facing downwards.

    输出

    Description

    Have you ever played the game like this? There are coins placed on an N*N matrix. Initially some coins are facing upwards, and some are facing downwards. Each time, you can choose one of the following rules to take a move.

    Select one row of the matrix and flip all coins in that row, which means replace all upward coins with downward coins and all downward coins with upward coins.

    Select one column of the matrix and flip all coins in that column.

    Select one diagonal of the matrix and flip all coins in that diagonal. Flip coins in diagonals is shown as Figure 1. Notice that there are 2*N diagonal in total. You can recognize them when taking the following picture.

    Figure 1 Flip coins in diagonals

    Your task is to use minimal movement to achieve an even number of downward-faced coins in each row and each column.

    Input

    The first line of input there is one integer T ( T ≤ 100 ), giving the number of test cases in the input. Each test case starts with a line containing a positive integer N ( N ≤ 20 ), representing the size of the matrix. Next N line each line contains N integer, giving the status of coins in that row. Value 0 means the coin is facing upwards, while value 1 means the coin is facing downwards.

    Output

    For each case, output the answer of minimal movement to achieve the goal. If it is impossible to find a solution, output -1 instead.

    Sample Input

    1
    3
    0 1 1
    0 1 0
    0 0 1

    Sample Output

    2

    Source

    样例输入

    1
    3
    0 1 1
    0 1 0
    0 0 1

    样例输出

    2

    作者


    路过

    雷人

    握手

    鲜花

    鸡蛋

    最新评论

    返回顶部