QOJ.ac

QOJ

Time Limit: 10 s - 20 s Memory Limit: 1024 MB Total points: 13

#5953. 棋盘矩阵

Statistics

Mija 有时喜欢玩矩阵游戏。她试图用最少的步数将一个矩阵变换为另一个矩阵。对 Mija 来说,一次移动是指交换矩阵中的任意两行或任意两列。

今天,Mija 有一个非常特殊的矩阵 $M$。$M$ 是一个 $2N \times 2N$ 的矩阵,其中每个元素要么是 0,要么是 1。Mija 决定尝试将 $M$ 变换为一个“棋盘矩阵”,即矩阵中每一行和每一列的元素都在 0 和 1 之间交替出现。你能帮 Mija 找到将 $M$ 变换为棋盘矩阵所需的最少移动次数吗?

输入格式

输入的第一行包含测试用例的数量 $T$。接下来是 $T$ 个测试用例。每个测试用例的第一行包含一个整数 $N$。接下来的 $2N$ 行,每行包含 $2N$ 个字符,表示矩阵 $M$ 的行;每个字符都是 0 或 1。

输出格式

对于每个测试用例,输出一行 "Case #x: y",其中 $x$ 是测试用例编号(从 1 开始),$y$ 是将 $M$ 变换为棋盘矩阵所需的最少行交换和列交换次数。如果无法将 $M$ 变换为棋盘矩阵,则 $y$ 应为 "IMPOSSIBLE"。

数据范围

$1 \le T \le 100$。

小数据集

$1 \le N \le 10$。

大数据集

$1 \le N \le 10^3$。

样例

样例输入 1

3
1
01
10
2
1001
0110
0110
1001
1
00
00

样例输出 1

Case #1: 0
Case #2: 2
Case #3: IMPOSSIBLE

说明

在第一个样例中,$M$ 已经是一个棋盘矩阵。

在第二个样例中,Mija 可以通过交换第 1 列和第 2 列,然后交换第 1 行和第 2 行,将 $M$ 变换为棋盘矩阵。

在第三个样例中,Mija 永远无法将 $M$ 变换为棋盘矩阵;因为它没有足够的 1。

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.