TopcoderArchive
TCO21 Jan 2008
TCO08 Qual 3

EvenOnes

Problem statement, definition, constraints, and public examples.

Problem Statement

You are given a rectangular matrix with odd numbers of rows and columns. Each cell of the matrix contains either 0 or 1. In one move, you can select any one row or column of the matrix and replace all 0's with 1's and all 1's with 0's in that row/column. Your aim is to have an even number of 1's in each row and each column.

The elements of the String[] matrix correspond to the rows of the matrix. Return the minimal number of moves needed to achieve your aim, or -1 if it's impossible.

Definition

Class:
EvenOnes
Method:
minOperations
Parameters:
String[]
Returns:
int
Method signature:
int minOperations(String[] matrix)
(be sure your method is public)

Constraints

  • matrix will contain between 1 and 49 elements, inclusive.
  • Each element of matrix will contain between 1 and 49 characters, inclusive.
  • All elements in matrix will contain the same number of characters.
  • The number of elements in matrix will be odd.
  • The number of characters in each element of matrix will be odd.
  • Each character in each element of matrix will be '0' (zero) or '1' (one).

Examples

  1. {
     "111",
     "011",
     "001"
    }
    Returns: 2
    We can first apply the operation to the middle row of the matrix, and then to the middle column.
  2. {
      "111",
      "111",
      "111",
      "111",
      "111"
    }
    Returns: 3
    We must apply the operations either to all rows or to all columns. As the number of columns is less than the number of rows, we choose the second variant.
  3. {
      "00000",
      "00000",
      "00000"
    }
    Returns: 0
    The matrix initially contains an even number of ones in each row and column, so we don't apply any operations.
  4. {
      "10101",
      "01010",
      "10101",
      "01010",
      "10101"
    }
    Returns: 5
Back to all problems