QOJ.ac

QOJ

时间限制: 8.0 s 内存限制: 1024 MB 总分: 100 难度: [显示] 可 Hack ✓

#18001. 白夜

统计

Little Cyan Fish は、$n$ 行 $m$ 列の行列 $A$ を持っている。行列の各要素は Cyan(シアン)または White(白)である。Cyan を表すには文字 C を、White を表すには文字 W を使用する。便宜上、Little Cyan Fish は行列の $i$ 行 $j$ 列目($1 \le i \le n, 1 \le j \le m$)の要素を $A_{i,j}$ と表記する。

Little Cyan Fish は以下の操作を何度でも行うことができる:

  • 垂直方向または水平方向に隣接するセルのペア $A_{i,j}$ と $A_{k,l}$ を選ぶ。すなわち、 $|i - k| + |j - l| = 1$ を満たすものを選ぶ。
  • $A_{i,j}$ と $A_{k,l}$ を入れ替える。

Little Cyan Fish は行列 $A$ を別の与えられた行列 $B$ に変換したいと考えている。もちろん、行列 $A$ に含まれる Cyan の個数は最終的な行列 $B$ に含まれる Cyan の個数と等しいことが保証されているため、Little Cyan Fish の要求を満たす操作手順は必ず存在する。

Little Cyan Fish が要求を達成するために必要な最小の操作回数を計算する手助けをしてほしい。

入力

入力は複数のテストケースからなる。入力の最初の行には、テストケースの数を示す整数 $T$ ($1 \le T$) が含まれる。

各テストケースについて、最初の行には行列の行数と列数を表す2つの整数 $n$ と $m$ ($1 \le n \le 10^5, 1 \le m \le 6$) が含まれる。

続く $n$ 行には、行列 $A$ の各行を表す長さ $m$ の文字列(C または W のみを含む)が与えられる。

続く $n$ 行には、行列 $B$ の各行を表す長さ $m$ の文字列(C または W のみを含む)が与えられる。行列 $A$ と行列 $B$ に含まれる C の個数は同じである(当然、W の個数も同じである)。

すべてのテストケースにおける $n$ の総和は $10^5$ を超えないことが保証されている。

出力

各テストケースについて、Little Cyan Fish が行列 $A$ を行列 $B$ に変換するために必要な最小の操作回数を整数で1行に出力せよ。

入出力例

入力 1

2
2 2
CW
WC
WC
CW
5 3
WWC
WCW
CWC
CCC
CCC
CCC
CCC
CCC
CWW
WWW

出力 1

2
16

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.