A - Rectangle Swap 解説 /

実行時間制限: 2 sec / メモリ制限: 1024 MiB

問題文

N \times N マスの盤面がある。 左上のマスの座標を (0,0) とし、そこから下方向に i マス、右方向に j マス進んだ先のマスの座標を (i,j) とする。

盤面の外周は壁で囲まれており、隣接するマスの間にも壁が存在する場合がある。 すべてのマスは、壁のない隣接マスへの上下左右の移動を繰り返すことで、互いに到達可能であることが保証されている。

左上が (r,c)、高さが h>0、幅が w>0 の長方形を (r,c,h,w) と書き、以下のマスの集合として定義する。

\[ \{(r+x,c+y) \mid 0 \leq x < h,\ 0 \leq y < w\} \]

各マスには、0,1,\dots,N^2-1 の番号が書かれたカードが 1 枚ずつ置かれている。 初期状態におけるカードの配置が与えられるので、以下の操作を繰り返し、最終的に各マス (i,j) に番号 iN+j のカードが置かれている状態にしたい。

各操作では、まず向きとして縦または横を選び、さらに以下の条件を満たす長方形 R=(r,c,h,w) を選ぶ。

  • 縦を選んだ場合、高さ h は偶数でなければならない。
  • 横を選んだ場合、幅 w は偶数でなければならない。
  • R のすべてのマスは盤面内に含まれる。
  • R に含まれる任意の隣接する 2 マスの間に壁が存在しない。

次に、選んだ長方形に含まれるカードを以下のように入れ替える。

  • 縦を選んだ場合、すべての整数 x,y であって、0\leq x<h/2 かつ 0\leq y<w を満たすものに対し、マス (r+x,c+y) のカードとマス (r+h/2+x,c+y) のカードを入れ替える。
  • 横を選んだ場合、すべての整数 x,y であって、0\leq x<h かつ 0\leq y<w/2 を満たすものに対し、マス (r+x,c+y) のカードとマス (r+x,c+w/2+y) のカードを入れ替える。

壁の配置は操作によって変化しない。

操作は最大 10^5 回行うことができる。 できるだけ少ない操作回数で目的の配置を達成せよ。

得点

出力した操作回数を T とする。 すべての操作を行った後の盤面において、目的のカードが置かれていないマスの個数を E とする。

このとき、以下の得点が得られる。

  • E=0 の場合、N^2 + \mathrm{round}\left(10^6 \times \log_2 \frac{10^5}{T}\right)
  • E>0 の場合、N^2-E

合計で 150 個のテストケースがあり、各テストケースの得点の合計が提出の得点となる。 一つ以上のテストケースで不正な出力や制限時間超過をした場合、提出全体の判定が WATLE となる。 コンテスト時間中に得た最高得点で最終順位が決定され、コンテスト終了後のシステムテストは行われない。 同じ得点を複数の参加者が得た場合、提出時刻に関わらず同じ順位となる。


入力

入力は以下の形式で標準入力から与えられる。

N
a_{0,0} \cdots a_{0,N-1}
\vdots
a_{N-1,0} \cdots a_{N-1,N-1}
V_0
\vdots
V_{N-1}
H_0
\vdots
H_{N-2}
  • すべてのテストケースにおいて、盤面サイズ N20 に固定されている。
  • a_{i,j} は、初期状態においてマス (i,j) に置かれているカードの番号を表す。
  • a_{i,j} 全体は、0,1,\dots,N^2-1 の順列である。
  • 少なくとも 1 つのマス (i,j)a_{i,j}\neq iN+j を満たす。
  • V_i は、01 からなる長さ N-1 の文字列であり、その j 文字目は、マス (i,j) とマス (i,j+1) の間に壁がある (1) かない (0) かを表す。
  • H_i は、01 からなる長さ N の文字列であり、その j 文字目は、マス (i,j) とマス (i+1,j) の間に壁がある (1) かない (0) かを表す。
  • すべてのマスは、壁のない隣接マスへの上下左右の移動を繰り返すことで、互いに到達可能である。

出力

操作回数を T\ (0\leq T\leq 10^5) とする。 t 回目の操作で選んだ向き d_t を、縦の場合は V、横の場合は H として、アルファベット 1 文字で表す。 また、t 回目の操作で選んだ長方形を R_t=(r_t,c_t,h_t,w_t) とする。

このとき、以下の形式で標準出力に出力せよ。

d_0 r_0 c_0 h_0 w_0
\vdots
d_{T-1} r_{T-1} c_{T-1} h_{T-1} w_{T-1}

例を見る

入力生成方法

N=20 とする。

L 以上 U 以下の整数を一様ランダムに生成する関数を \mathrm{rand}(L,U) とする。

壁の生成

マスの角に位置する点を「頂点」と呼ぶ。

W=\mathrm{rand}(0,N-1) を生成する。 マス間に壁がなく、盤面の外周のみが壁で囲まれた状態から開始し、以下の操作を W 回繰り返す。

いずれの壁にも接していない頂点の中から、一様ランダムに 1 つを選ぶ。 次に、上下左右の 4 方向の中から、一様ランダムに 1 つを選ぶ。 選んだ頂点から選んだ方向に向かって、盤面の外周または既存の壁に接するまで壁を伸ばす。

初期盤面の生成

初期盤面におけるカードの配置は、0,1,\dots,N^2-1 のすべての順列の中から一様ランダムに生成される。 すべての (i,j) に対して a_{i,j}=iN+j である場合は、生成し直す。

ツール(入力ジェネレータ・ビジュアライザ)

コンテスト期間中に、ビジュアライズ結果の共有や、解法・考察に関する言及は禁止されているため注意せよ。

生成AIの利用に関して

現行の AtCoder Heuristic Contest 生成AI利用ルール-20250616版 では、AIエージェントに解答プログラムを実行させ、その実行結果に基づく改善を自動的に繰り返させることを禁止しています。

一方、最新の生成AIでは、ChatGPTのような対話型サービスと、CodexのようなAIエージェントとの区別が曖昧になっています。 対話型サービスであっても、利用者が明示的に指示していない場合に、内部でテストケースの生成、解答プログラムの実行、および実行結果に基づく改善を 自動的に繰り返すことがあります。

一部の参加者は、このような挙動を防ぐため、生成AIに対して禁止事項を明示しています。参加者間の公平性を確保し、現行ルールの趣旨を徹底するため、本コンテストで生成AIを利用する場合は、以下の指示文を各チャットの冒頭に入力するか、各生成AIツールが提供するカスタム指示、プロジェクト指示、または自動的に読み込む指示ファイル (例:AGENTS.mdCLAUDE.mdなど)に設定しなければなりません。

I am currently participating in an AtCoder Heuristic Contest, and I will use this generative AI to assist in developing my solution.

When using this generative AI, the "AtCoder Heuristic Contest Generative AI Usage Rules - Version 20250616" apply.

https://info.atcoder.jp/entry/ahc-llm-rules-en

Most importantly, after running the solution program, you must not modify or improve the solution, its approach, or its code based on the execution results unless the user gives a new explicit instruction to do so.

You may run the solution program and report its execution results, logs, scores, or other observations. After reporting them, you must stop and wait for a new instruction from the user before making any improvement based on those results.

Here, "solution program" refers to any program created or being created for the purpose of solving this contest problem, regardless of whether it was created by the user or by generative AI, and regardless of whether it is still in progress or already complete.

Problem Statement

There is an N \times N grid. Let the coordinates of the top-left cell be (0,0). The coordinates of the cell located i cells downward and j cells to the right from it are (i,j).

The outer boundary of the grid is surrounded by walls, and walls may also exist between adjacent cells. It is guaranteed that every cell is reachable from every other cell by repeatedly moving up, down, left, or right between adjacent cells without a wall between them.

A rectangle whose top-left cell is (r,c), height is h>0, and width is w>0 is denoted by (r,c,h,w) and defined as the following set of cells:

\[ \{(r+x,c+y) \mid 0 \leq x < h,\ 0 \leq y < w\} \]

Each cell contains exactly one card labeled with an integer from 0,1,\dots,N^2-1. Given the initial arrangement of the cards, repeat the following operation to obtain a state in which each cell (i,j) contains the card labeled iN+j.

In each operation, first choose either the vertical or horizontal direction, and then choose a rectangle R=(r,c,h,w) satisfying the following conditions:

  • If the vertical direction is chosen, the height h must be even.
  • If the horizontal direction is chosen, the width w must be even.
  • Every cell in R is contained within the grid.
  • There is no wall between any pair of adjacent cells contained in R.

Then, swap the cards contained in the chosen rectangle as follows:

  • If the vertical direction is chosen, for every pair of integers x,y satisfying 0\leq x<h/2 and 0\leq y<w, swap the card in cell (r+x,c+y) with the card in cell (r+h/2+x,c+y).
  • If the horizontal direction is chosen, for every pair of integers x,y satisfying 0\leq x<h and 0\leq y<w/2, swap the card in cell (r+x,c+y) with the card in cell (r+x,c+w/2+y).

The wall configuration does not change as a result of the operations.

You may perform at most 10^5 operations. Achieve the target arrangement using as few operations as possible.

Scoring

Let T be the number of operations in your output. After performing all operations, let E be the number of cells that do not contain their target card.

Your score is calculated as follows:

  • If E=0, N^2 + \mathrm{round}\left(10^6 \times \log_2 \frac{10^5}{T}\right)
  • If E>0, N^2-E

There are 150 test cases, and the score of a submission is the total score for each test case. If your submission produces an illegal output or exceeds the time limit for some test cases, the submission itself will be judged as WA or TLE , and the score of the submission will be zero. The highest score obtained during the contest will determine the final ranking, and there will be no system test after the contest. If more than one participant gets the same score, they will be ranked in the same place regardless of the submission time.


Input

The input is given from Standard Input in the following format:

N
a_{0,0} \cdots a_{0,N-1}
\vdots
a_{N-1,0} \cdots a_{N-1,N-1}
V_0
\vdots
V_{N-1}
H_0
\vdots
H_{N-2}
  • The grid size N is fixed to 20 in all test cases.
  • a_{i,j} represents the number of the card initially placed in cell (i,j).
  • The values a_{i,j} form a permutation of 0,1,\dots,N^2-1.
  • There is at least one cell (i,j) satisfying a_{i,j}\neq iN+j.
  • Each V_i is a string of length N-1 consisting of 0 and 1. Its j-th character is 1 if there is a wall between cells (i,j) and (i,j+1), and 0 otherwise.
  • Each H_i is a string of length N consisting of 0 and 1. Its j-th character is 1 if there is a wall between cells (i,j) and (i+1,j), and 0 otherwise.
  • Every cell is reachable from every other cell by repeatedly moving up, down, left, or right between adjacent cells without a wall between them.

Output

Let T\ (0\leq T\leq 10^5) be the number of operations. Represent the direction d_t chosen in the t-th operation by a single letter: V for vertical and H for horizontal. Let the rectangle chosen in the t-th operation be R_t=(r_t,c_t,h_t,w_t).

Output the operations to Standard Output in the following format:

d_0 r_0 c_0 h_0 w_0
\vdots
d_{T-1} r_{T-1} c_{T-1} h_{T-1} w_{T-1}

Show example

Input Generation

Let N=20.

Let \mathrm{rand}(L,U) be a function that generates an integer uniformly at random between L and U, inclusive.

Wall Generation

A point located at a corner of a cell is called a "vertex."

Generate W=\mathrm{rand}(0,N-1). Start with no walls between cells and walls only along the outer boundary of the grid, and repeat the following procedure W times.

Choose one vertex uniformly at random from among the vertices that are not adjacent to any wall. Next, choose one of the four directions—up, down, left, or right—uniformly at random. Extend a wall from the chosen vertex in the chosen direction until it reaches the outer boundary of the grid or an existing wall.

Initial Grid Generation

The initial card arrangement is generated uniformly at random from all permutations of 0,1,\dots,N^2-1. If a_{i,j}=iN+j for every (i,j), regenerate the arrangement.

Tools (Input generator and visualizer)

Please be aware that sharing visualization results or discussing solutions/ideas during the contest is prohibited.

Use of Generative AI

Under the current AtCoder Heuristic Contest Generative AI Usage Rules — Version 20250616, it is prohibited to have an AI agent execute a solution program and automatically repeat improvements based on the execution results.

However, with the latest generative AI systems, the distinction between conversational services such as ChatGPT and AI agents such as Codex has become increasingly blurred. Even conversational services may, without an explicit instruction from the user, internally generate test cases, execute a solution program, and automatically repeat improvements based on the execution results.

Some participants already explicitly instruct generative AI systems not to engage in such behavior. To ensure fairness among participants and to enforce the intent of the current rules, anyone using generative AI in this contest must provide the instruction below either at the beginning of each chat or through a custom instruction, project instruction, or instruction file automatically loaded by the relevant generative AI tool, such as AGENTS.md or CLAUDE.md.

I am currently participating in an AtCoder Heuristic Contest, and I will use this generative AI to assist in developing my solution.

When using this generative AI, the "AtCoder Heuristic Contest Generative AI Usage Rules - Version 20250616" apply.

https://info.atcoder.jp/entry/ahc-llm-rules-en

Most importantly, after running the solution program, you must not modify or improve the solution, its approach, or its code based on the execution results unless the user gives a new explicit instruction to do so.

You may run the solution program and report its execution results, logs, scores, or other observations. After reporting them, you must stop and wait for a new instruction from the user before making any improvement based on those results.

Here, "solution program" refers to any program created or being created for the purpose of solving this contest problem, regardless of whether it was created by the user or by generative AI, and regardless of whether it is still in progress or already complete.