Thursday, May 1, 2014

Sereja and Mirroring : Codeforces Round #243 (Div. 2) Problem B

B. Sereja and Mirroring
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Let's assume that we are given a matrix b of size x × y, let's determine the operation of mirroring matrix b. The mirroring of matrix b is a2x × y matrix c which has the following properties:
  • the upper half of matrix c (rows with numbers from 1 to x) exactly matches b;
  • the lower half of matrix c (rows with numbers from x + 1 to 2x) is symmetric to the upper one; the symmetry line is the line that separates two halves (the line that goes in the middle, between rows x and x + 1).
Sereja has an n × m matrix a. He wants to find such matrix b, that it can be transformed into matrix a, if we'll perform on it several(possibly zero) mirrorings. What minimum number of rows can such matrix contain?
Input
The first line contains two integers, n and m (1 ≤ n, m ≤ 100). Each of the next n lines contains m integers — the elements of matrix a. The i-th line contains integers ai1, ai2, ..., aim (0 ≤ aij ≤ 1) — the i-th row of the matrix a.

Output
In the single line, print the answer to the problem — the minimum number of rows of matrix b.

Sample test(s)
input
4 3
0 0 1
1 1 0
1 1 0
0 0 1
output
2
input
3 3
0 0 0
0 0 0
0 0 0
output
3
input
8 1
0
1
1
0
0
1
1
0
output
2
Note
In the first test sample the answer is a 2 × 3 matrix b:
001
110
If we perform a mirroring operation with this matrix, we get the matrix a that is given in the input:
001
110
110
001

→ Source
import java.util.Scanner;

public class SerejaAndMirroring {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int rows = scan.nextInt();
        int columns = scan.nextInt();

        if (rows % 2 == 1) {
            System.out.println(rows);
            System.exit(0);
        }
        if (rows == 1) {
            System.out.println(rows);
            System.exit(0);
        }
        int result = 0;

        int[][] mat = new int[rows + 1][columns + 1];
        for (int i = 1; i <= rows; ++i) {
            for (int j = 1; j <= columns; ++j) {
                mat[i][j] = scan.nextInt();
            }
        }
        int m = 0;

        while (rows % 2 == 0) {
            for (int i = 1; i <= rows / 2 && m != 1; ++i) {
                for (int j = 1; j <= columns; ++j) {
                    if (mat[i][j] != mat[rows - i + 1][j]) {
                        m = 1;
                        break;
                    }
                }
            }
            if (m == 1) {
                break;
            } else {
                rows /= 2;
            }
        }
            System.out.println(rows);
    }
}

No comments:

Post a Comment