Time Limit: 2 sec / Memory Limit: 1024 MiB
問題文
N\times N マスの盤面がある。 左上のマスの座標を (0,0) とし、そこから下方向に i マス、右方向に j マス進んだ先のマスの座標を (i,j) とする。 最初、すべてのマスは空である。 各マスには、駒を置きたい目標マスであるかどうかが指定されている。
あなたは、次の操作を N^2 回以下行うことができる。
盤面の上下左右いずれかの辺と、その辺に対応する行または列を選び、盤外から駒を 1 個入れる。 駒を最初に入れるマスを入口と呼ぶ。 各方向の入口と駒の進行方向は以下の通りである。
| 選ぶ辺 | 行または列 | 入口 | 進行方向 |
|---|---|---|---|
| 左 | 行 i | (i,0) | 右 |
| 右 | 行 i | (i,N-1) | 左 |
| 上 | 列 j | (0,j) | 下 |
| 下 | 列 j | (N-1,j) | 上 |
入口に既に駒がある操作は行えない。 駒は入口から進行方向へまっすぐ進み、次のマスに既に駒があるか、次のマスが盤外であるとき、現在のマスで停止して固定される。 進行方向に対して横に隣接するマスに駒があっても停止しない。 固定された駒を動かしたり、取り除いたりすることはできない。 各操作は、入れた駒が固定された後に次の操作へ進む。
目標マスに置かれた駒を増やし、それ以外のマスに置かれた駒を抑えて、得点ができるだけ高くなるような操作列を求めよ。
操作の例
説明のため、N=4 の盤面での操作例を示す。 実際の入力では N=30 である。 水色のマスは目標マス、緑の駒は目標マスに置かれた駒、赤の駒はそれ以外のマスに置かれた駒を表す。 図中の t は操作回数、駒の数字は入れた順番であり、紫の矢印と輪は直前の駒の進路と停止位置を表す。
初期状態と3回の操作後の盤面
- 左から行 1 へ駒を入れると、盤面の端の (1,3) で停止する。
- 上から列 2 へ駒を入れると、(3,2) で停止する。 途中で (1,3) の駒の横を通るが、停止しない。
- 下から列 3 へ駒を入れると、(1,3) の駒の手前の (2,3) で停止する。
この後、右から行 1 へ駒を入れる操作は、入口 (1,3) に既に駒があるため行えない。
得点
すべての操作を終えたとき、駒が置かれていない目標マスの個数を E、目標マス以外に置かれた駒の個数を B とする。 このとき、以下の得点が得られる。
\[ S=N^2-E-B+1 \]
上の例で 3 回の操作を終えた時点では、E=2、B=1 なので、得点は 4^2-2-1+1=14 となる。
合計で 100 個のテストケースがあり、各テストケースの得点の合計が提出の得点となる。 一つ以上のテストケースで不正な出力や制限時間超過をした場合、提出全体の判定が WA や TLE となり、提出の得点は 0 点となる。 コンテスト時間中に得た最高得点で最終順位が決定され、コンテスト終了後のシステムテストは行われない。 同じ得点を複数の参加者が得た場合、提出時刻に関わらず同じ順位となる。
入力
入力は以下の形式で標準入力から与えられる。
N
s_0
\vdots
s_{N-1}
各値は以下の制約を満たす。
- N=30
- s_i は長さ N の文字列であり、各文字は
oまたは.である。
s_i の j 文字目はマス (i,j) が目標マスであるかどうかを表し、o なら目標マス、. ならそれ以外のマスである。
ここで、文字列の文字の位置も 0 始まりとする。
出力
以下の形式で標準出力に出力せよ。
d_0 k_0
\vdots
d_{K-1} k_{K-1}
ここで、K は出力する操作の回数であり、N^2 以下でなければならない。 各 t=0,\ldots,K-1 について、d_t と k_t は t 番目の操作を表す整数であり、0\leq d_t\leq 3、0\leq k_t<N を満たさなければならない。 操作は出力した順に行われ、d_t の意味は以下の通りである。
| d_t | 操作 |
|---|---|
| 0 | 左から行 k_t へ駒を入れる。 |
| 1 | 右から行 k_t へ駒を入れる。 |
| 2 | 上から列 k_t へ駒を入れる。 |
| 3 | 下から列 k_t へ駒を入れる。 |
各操作を行う時点で、入口は空でなければならない。
入力生成方法
N=30 とする。 実数 p を区間 [0.35,0.65] から一様ランダムに選ぶ。 各マスを互いに独立に確率 p で目標マスとし、それ以外のマスは目標マスでないものとする。
ツール(入力ジェネレータ・ビジュアライザ)
- Web版: ローカル版より高性能でアニメーション表示と手動プレイが可能です。
- ローカル版: 使用するにはRust言語のコンパイル環境をご用意下さい。
コンパイルが通らない場合は
rustup updateをお試し下さい。- Windows用のコンパイル済みバイナリ: Rust言語の環境構築が面倒な方は代わりにこちらをご利用下さい。
コンテスト期間中に、チーム外とのビジュアライズ結果の共有や、解法・考察に関する言及は禁止されています。 ご注意下さい。
Problem Statement
There is an N\times N grid. Let (0,0) be the coordinates of the top-left cell, and let (i,j) be the coordinates of the cell located i cells downward and j cells to the right from there. Initially, all cells are empty. Each cell is designated as either a target cell, where you want to place a piece, or a non-target cell.
You may perform the following operation at most N^2 times.
Choose one of the four sides of the grid and a corresponding row or column, and insert one piece from outside the grid. The first cell the piece enters is called the entrance. The entrance and the direction of movement for each side are as follows.
| Side | Row or column | Entrance | Direction of movement |
|---|---|---|---|
| Left | Row i | (i,0) | Right |
| Right | Row i | (i,N-1) | Left |
| Top | Column j | (0,j) | Down |
| Bottom | Column j | (N-1,j) | Up |
An operation is not allowed if the entrance already contains a piece. The inserted piece moves straight from the entrance in the specified direction until the next cell already contains a piece or is outside the grid, at which point it stops in its current cell and remains fixed there. A piece in a cell adjacent to the side of its path does not cause it to stop. A fixed piece cannot be moved or removed. Each operation finishes before the next operation begins.
Find a sequence of operations that achieves as high a score as possible by placing more pieces in target cells while limiting the number of pieces in non-target cells.
Example Operations
The following example uses an N=4 grid for illustration. In the actual input, N=30. Light blue cells are target cells, green pieces are in target cells, and red pieces are in non-target cells. In the figure, t is the number of operations performed, the numbers on the pieces indicate their insertion order, and the purple arrow and ring show the path and stopping position of the most recently inserted piece.
The initial grid and the grid after each of three operations
- Insert a piece into row 1 from the left. It stops at (1,3), at the edge of the grid.
- Insert a piece into column 2 from the top. It stops at (3,2). It passes beside the piece at (1,3) without stopping.
- Insert a piece into column 3 from the bottom. It stops at (2,3), just before the piece at (1,3).
After these operations, you cannot insert a piece into row 1 from the right because the entrance (1,3) already contains a piece.
Scoring
After all operations, let E be the number of target cells without a piece, and let B be the number of pieces in non-target cells. You receive the following score.
\[ S=N^2-E-B+1 \]
After the three operations in the example above, E=2 and B=1, so the score is 4^2-2-1+1=14.
There are 100 test cases, and the score of a submission is the sum of the scores for all test cases. 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
Input is given from Standard Input in the following format.
N
s_0
\vdots
s_{N-1}
Each value satisfies the following constraints.
- N=30
- s_i is a string of length N consisting of
oand..
The j-th character of s_i indicates whether cell (i,j) is a target cell: o denotes a target cell, and . denotes a non-target cell.
Character positions in each string are also indexed from 0.
Output
Output to Standard Output in the following format.
d_0 k_0
\vdots
d_{K-1} k_{K-1}
Here, K is the number of operations you output and must be at most N^2. For each t=0,\ldots,K-1, d_t and k_t are integers specifying the t-th operation and must satisfy 0\leq d_t\leq 3 and 0\leq k_t<N. Operations are performed in the order they are output, and d_t has the following meaning.
| d_t | Operation |
|---|---|
| 0 | Insert a piece into row k_t from the left. |
| 1 | Insert a piece into row k_t from the right. |
| 2 | Insert a piece into column k_t from the top. |
| 3 | Insert a piece into column k_t from the bottom. |
The entrance must be empty when each operation is performed.
Input Generation
Set N=30. Choose a real number p uniformly at random from the interval [0.35,0.65]. Independently for each cell, designate it as a target cell with probability p and as a non-target cell otherwise.
Tools (Input generator and visualizer)
- Web version: This is more powerful than the local version providing animations and manual play.
- Local version: You need a compilation environment of Rust language.
If it does not compile, please try
rustup update.- Pre-compiled binary for Windows: If you are not familiar with the Rust language environment, please use this instead.
Please be aware that sharing visualization results or discussing solutions/ideas with anyone outside your team during the contest is prohibited.