A - Crops on Grid Editorial /

Time Limit: 2 sec / Memory Limit: 1024 MB

問題文

サイズが H \times W の長方形の土地があります。この土地はサイズ 1 \times 1 の正方形の区画に分割されており、区画と区画の境界は一部が水路となっています。また、土地の外周は柵で囲まれており、外周に一箇所だけ設置されている出入口からのみ出入りできます。出入口はいずれか一つの区画の一辺に含まれており、複数の区画にまたがることはありません。

あなたはこの土地を使って作物を育てたいと考えました。ある区画で作物を植えたり収穫したりするときは農機を使うので、出入口からその区画まで農機を移動させる必要があります。ただし、農機が通るための通路は設置されていません。

これから通路を整備するのは大変なため、あなたは作物を栽培中ではない区画だけを通って農機を移動させることにしました。すなわち、ある区画に作物を植えるときやある区画から作物を収穫するときは、出入口から目的の区画まで以下の条件のもとで到達できなければなりません。

  • 各区画からは隣接する4区画にのみ移動できる(斜めには移動できない)
  • ただし、境界が水路であるような二つの区画の間は直接移動できない
  • 栽培中の区画を通ってはならない

これから T ヶ月の間に栽培する作物の候補が K 種類あり、1, 2, \dots, K と番号が付けられています。作物 k を栽培するには、S_k ヶ月目までに植える必要があります。また、作物 kD_k ヶ月目に収穫する必要があります。作物 kS_k ヶ月目より早く植えることは可能ですが、その場合でも D_k ヶ月目より早く収穫することはできません。

どの作物をどの区画で栽培してもかまいませんが、一つの作物を複数の区画で栽培したり、複数の作物を一つの区画で同時に栽培したりすることはできません。

なるべく多くの区画を有効に利用できるような計画を立ててください。

土地情報の詳細

土地は北西の角の座標が (0, 0)、南西の角の座標が (H, 0)、北東の角の座標が (0, W)、南東の角の座標が (H, W) です。北から i+1 番目、西から j+1 番目の区画は点 (i, j) と点 (i + 1, j + 1) を対角線とする正方形であり、これを区画 (i, j) と呼ぶことにします(0 \le i \le H - 1, 0 \le j \le W - 1)。

出入口は区画 (i_0, 0) の西側の境界部分(言い換えると、点 (i_0, 0) と点 (i_0 + 1, 0) を結ぶ線分上)にあります。

辺を共有する2区画の境界のそれぞれについて、その部分に水路があるかどうかが指定されます。水路の情報のフォーマットは「入力」のセクションを参照してください。

計画の詳細

計画は、どの作物をどの区画で作り、何ヶ月目に植えるかの情報からなり、列 (k_1, i_1, j_1, s_1), \dots, (k_M, i_M, j_M, s_M) で表されます。この列の m 番目の要素は、作物 k_m を区画 (i_m, j_m)s_m ヶ月目に植えることを意味します。

計画が与えられたとき、t ヶ月目 (t = 1, 2, \dots, T) には以下のことを行います。

  1. t ヶ月目の始めに、s_m = t を満たすすべての m について、k_m(i_m, j_m) に植える。このとき、各 m について出入口から (i_m, j_m) まで水路や栽培中の区画を通ることなく移動できなければならない。
  2. t ヶ月目の終わりに、D_{k_m} = t を満たすすべての m について、k_m(i_m, j_m) から収穫する。このとき、各 m について出入口から (i_m, j_m) まで水路や栽培中の区画を通ることなく移動できなければならない。

なお、同じ月に複数の作物を植えるとき、植える順番によって上の条件が満たされるかどうかが変わることがあります。例えば、作物 k_1k_2 を同じ月に植えたく、かつ (i_1, j_1) を通らないと (i_2, j_2) に移動できない場合、先に k_1 を植えると k_2 が植えられなくなります。

このような場合は、各月ごとの植える順番を適切に決めることですべての作物を植えられるなら、実行可能な計画と判定されます。そのような順番が存在するかどうかはジャッジにより判定されるため、計画中で植える順番を指定する必要はありません。同じ月に複数の作物を収穫する場合も同様に、収穫する適切な順番が存在するかどうかはジャッジにより判定されるため、計画中で収穫の順番を指定する必要はありません。

計画は以下の条件を満たす必要があります。

  • 同じ作物は一つの区画でしか作ることができない
  • 一つの区画では同時に一つの作物しか作ることができない
  • 作物 k を植える場合は S_k ヶ月目までに植えなければならない
  • 作物を植えたり収穫したりする際、出入口と目的の区画の間を移動できなければならない

なお、すべての作物を植える必要はありません。

得点

作物 k のスコア X_k を以下のように定めます。

  1. 作物 k が計画に含まれていなければ X_k = 0 とする
  2. 作物 k が計画に含まれているとき X_k = D_k - S_k + 1 とする

AC の判定がされたテストケースの得点は 10^6 \times {\sum_{k = 1}^K X_k \over HWT} となります(この値は暫定テストとシステムテストで使用されるすべてのテストケースに対して整数になります)。AC 以外の判定がされたテストケースの得点は 0 となります。

暫定テストでは、すべてのテストケースで AC の判定がされた場合は各テストケースの得点の合計が提出の得点となり、そうでない場合は提出の得点は 0 となります。

システムテストでは、各テストケースの得点の合計が提出の得点となります。

最終順位はコンテスト終了後に実施されるより多くの入力に対するシステムテストにおける得点で決定されます。システムテストは CE 以外の結果を得た一番最後の提出に対してのみ行われるため、最終的に提出する解答を間違えないよう注意してください。

テストケース数

  • 暫定テスト: 50個
  • システムテスト: 2000個、コンテスト終了後に seeds.txt (sha256=0c2ffe8037c0c232ed9775b89a892fc1f68709a1582c83fd6bb609244cefcf32) を公開

システムテストのテストケースには水路の生成に使用するパラメータ d1, 2, 3, 4 のものがそれぞれ同数ずつ含まれます(詳細は入力生成方法を参照してください)。

実行時間について

実行時間には多少のブレが生じます。また、システムテストでは同時に大量の実行を行うため、暫定テストに比べて数%程度実行時間が伸びる現象が確認されています。 そのため、実行時間制限ギリギリの提出がシステムテストで TLE となる可能性があります。プログラム内で時間を計測して処理を打ち切るか、実行時間に余裕を持たせるようお願いします。


入力

入力は以下のフォーマットで標準入力から与えられます。

T H W i_0
h_{0, 0}h_{0, 1}\cdots h_{0, W-1}
\vdots
h_{H-2, 0}h_{H-2, 1}\cdots h_{H-2, W-1}
v_{0, 0}v_{0, 1}\cdots v_{0, W-2}
\vdots
v_{H-1, 0}v_{H-1, 1}\cdots v_{H-1, W-2}
K
S_1 D_1
\vdots
S_K D_K

h_{i, 0} \cdots h_{i, W-1}0 または 1 のみからなる W 文字の文字列であり、区画 (i, j) の南側(座標 (i+1, j)(i+1, j+1) の間を結ぶ線分上)に水路があるときに h_{i,j} = 1、そうでないときに h_{i,j} = 0 です。

v_{i, 0} \cdots v_{i, W-2}0 または 1 のみからなる W-1 文字の文字列であり、区画 (i, j) の東側(座標 (i, j+1)(i+1, j+1) の間を結ぶ線分上)に水路があるときに v_{i,j} = 1、そうでないときに v_{i,j} = 0 です。

暫定テストおよびシステムテストに用いられる入力は「入力生成方法」のセクションに述べられている方法で生成され、以下の条件を満たします。

  • T = 100
  • H = W = 20
  • 0 \le i_0 \le H - 1
  • 0 < K \le HWT
  • 1 \le S_k < D_k \le T \quad (1 \le k \le K)
  • 出入口からどの区画へも、隣接する区画への移動を繰り返すことで水路を通ることなく到達可能である

出力

作る作物の数を M、作る作物を任意の順番で並べたものを k_1, \dots, k_M とし、k_m を区画 (i_m, j_m)s_m ヶ月目に植えるとします。

このとき、以下の形式で標準出力に出力してください。

M
k_1 i_1 j_1 s_1
\vdots
k_M i_M j_M s_M

出力は問題文中に記述されている計画の要件を満たす必要があります。満たさない場合は WA と判定されます。

入力生成方法

詳細

\mathrm{rand}(L, U)L 以上 U 未満の実数を一様ランダムに生成する関数を表す。また平均 \mu、分散 \sigma^2正規分布から実数値をランダムに生成する関数を \mathrm{normal}(\mu, \sigma^2) で表す。

  • T = 100
  • H = W = 20
  • 0 \le i_0 \le H - 1 となるように i_0 を一様ランダムに選ぶ
  • 土地(外周も含む)に含まれる格子点を頂点とし、距離 1 の頂点間すべてに辺を張ってできる無向グラフを G = (V, E) とする。つまり V, E は以下のように定義される。
    • V = \lbrace(i, j) \mid 0 \le i \le H, 0 \le j \le W\rbrace
    • E = \lbrace((i_1, j_1), (i_2, j_2)) \in V \times V \mid |i_1 - i_2| + |j_1 - j_2| = 1\rbrace
  • G を使って以下のように水路を生成する。
    • d = 1 + (\mathit{seed} \bmod 4)
    • 外周に含まれる点(すなわち、i = 0, i = H, j = 0, j = W のいずれかを満たす (i, j) \in V)すべてにマークを付ける
    • 以下の処理を繰り返す
      • V' をこの時点でマークの付いている点全体の集合、V'' = \lbrace (i', j') \in V \mid \forall (i, j) \in V', |i - i'| + |j - j'| > d \rbrace とする
      • V'' が空なら水路の生成を終了する。空でないなら V'' の要素を一様ランダムに一つ選んで (i, j) とする。
      • V' の点のうち、G において (i, j) から最も近い点を (i', j') とする。最も近い点が複数ある場合は、その中から一様ランダムに選ぶ。
      • (i, j)(i', j') の間の G における最短路を P とする。最短路が複数ある場合は、向きを変える回数が最も少ないもの(最大2通りある)の中から一様ランダムに選ぶ。
      • P に含まれるすべての辺を水路にする
      • P に含まれるすべての頂点にマークを付ける
  • L = \mathrm{round}(HWT \times \mathrm{rand(1.0, 2.0)}) とする。k = 1, 2, \dots に対して以下を繰り返すことで D_k, S_k を生成する。
    • L_k = \mathrm{round}(10^{\mathrm{normal}(1, 0.25^2)}) とする。ただし 2 \le L_k \le T を満たさなかったら L_k の生成をやり直す。
    • D_kL_k \le D_k \le T の範囲で一様ランダムに生成する
    • S_k = D_k - L_k + 1 とする
    • \sum_{i = 1}^k L_i \ge L なら生成を終了する

ツール

コンテスト終了までビジュアライズ結果の共有は禁止されています。ご注意下さい。


入力例 1

10 6 6 3
011010
000001
011000
000110
011000
00000
10001
10010
00110
10101
10000
20
2 10
1 10
4 10
1 10
2 9
4 5
2 8
4 10
4 9
1 9
1 3
1 6
1 7
3 6
8 10
1 7
3 8
2 8
1 10
2 10

出力例 1

12
1 0 0 2
2 0 1 1
3 1 0 4
4 0 2 1
5 3 1 2
6 2 0 4
7 4 0 2
10 3 2 1
15 2 0 8
18 5 0 2
19 0 3 1
20 4 1 2

このサンプルは入出力の例を示すための、採点に使われる入力データよりも小さいものです。

Problem Statement

There is a rectangular piece of land with size H \times W. This land is divided into square blocks of size 1 \times 1, and some of the boundaries between the blocks are separated by waterways. The border of the land is surrounded by fences except for a single entrance, which is the only way to enter or exit the land. The entrance is set on one side of a single block.

You plan to grow crops on this land. When planting or harvesting crops, you will need to move agricultural machinery from the entrance to the block you plan to work on. However, there are no fixed pathways set as the passage for the machinery.

As it would be a challenging task to establish the pathways now, you have decided to move the machinery through the blocks which are not currently being used for growing crops. In other words, when planting or harvesting crops in a particular block, it must be possible to reach to the block from the entrance while satisfying the following conditions:

  • You can only move to the four adjacent blocks from each block (moving diagonally is not allowed).
  • Yon cannot move from one block to another if they are separated by a waterway.
  • You may not pass through blocks which are currently being used for growing crops.

There are K kinds of crops available for planting, numbered 1, 2, \dots, K. You are to choose some of them to be planted and harvested in the following T months. To plant crop k, you will need to plant it no later than the S_k-th month, and harvest it at the D_k-th month. You can plant crop k before the S_k-th month, but even if you plant it earlier than required, you cannot harvest it before D_k-th month.

It is allowed to plant any kind of crop in any block, but one kind of crop cannot be planted in multiple blocks, and multiple kinds of crops cannot be planted in a single block.

Please make a planting plan which maximizes the utility of the land.

Details of the land

The land has coordinates with the northwest corner at (0, 0), the southwest corner at (H, 0), the northeast corner at (0, W), and the southeast corner at (H, W). The block at the i+1-th row from the north and the j+1-th column from the west is a square with diagonal points (i, j) and (i + 1, j + 1). This square is referred to as block (i, j) (0 \le i \le H - 1, 0 \le j \le W - 1).

The entrance is set at the west side of block (i_0, 0) (that is, on the segment between coordinates (i_0, 0) and (i_0 + 1, 0)).

For the blocks which shares sides, whether there is a waterway between them is determined by the given input. Please refer to the "Input" section for the details of the input format for the waterway.

Details of the planting plan

A planting plan describes which crop are grown on which blocks, and when they are planted. It is represented by a sequence (k_1, i_1, j_1, s_1), \dots, (k_M, i_M, j_M, s_M). The m-th element of this sequence signifies that crop k_m is planted in block (i_m, j_m) on the s_m-th month.

Based on a given planting plan, the following actions are taken on the t-th month (t = 1, 2, \dots, T).

  1. At the beginning of the t-th month, for all m where s_m = t, crop k_m is planted in block (i_m, j_m). During this process, (i_m, j_m) must be reachable from the entrance without crossing waterways or passing through blocks where crops are currently growing in.
  2. At the end of the t-th month, for all m where D_{k_m} = t, crop k_m is harvested from block (i_m, j_m). During this process, (i_m, j_m) must be reachable from the entrance without crossing waterways or passing through blocks where crops are currently growing in.

When planting multiple crops in a same month, the reachability condition above may depend on the order in which crops are planted. For example, if crops k_1 and k_2 are to be planted in a same month, and (i_2, j_2) cannot be reached without passing through (i_1, j_1), then planting crop k_1 before k_2 blocks the way for planting k_2.

In this case, a planting plan is considered valid if there exists an order such that planting them by the order does not lead to the situation above. Such an order will be calculated by the judging program, so it does not have to be determined in the output. Similarly, when harvesting multiple crops in a same month, the order of the crops will be calculated by the judging program and does not have to be determined in the output.

The planting plan must satisfy the following conditions.

  • The same crop can only be planted in the same block.
  • A block can only contains at most one kind of crop at the same time.
  • When planting crop k, it must be planted before S_k-th month.
  • When planting or harvesting crop, the target crop must be reachable from the entrance.

Note that, it is not required to plant all the K crops.

Scoring

The score of crop k, X_k, is determined as the following rules.

  1. If crop k is not planted, then X_k = 0.
  2. If crop k is planted, then X_k = D_k - S_k + 1.

For each test case that received an AC verdict, the score of the test case will be 10^6 \times {\sum_{k = 1}^K X_k \over HWT} (this number will be an integer in all test cases used in provisional test and system test). For each test case that received a not AC verdict, the score of the test case will be 0.

In provisional test, if a submission receives AC in all test cases, the score of the submission will be the sum of the score of all test cases. Otherwise, the score of the submission will be 0.

In system test, the score of the submission will be the sum of the score of all test cases.

The final standings will be determined by the system test which is conducted after the contest. The system test will be performed only for the last submission which received a result other than CE. Be careful not to make a mistake in the final submission.

Number of test cases

  • Provisional test: 50
  • System test: 2000; We will publish seeds.txt (sha256=0c2ffe8037c0c232ed9775b89a892fc1f68709a1582c83fd6bb609244cefcf32) after the contest is over.

The test cases in the system test contains the same amount of test cases with d = 1, 2, 3, 4, where d is a parameter used in generating the waterways (for details, please refer to the input generation).

About execution time

Execution time may vary slightly from run to run. In addition, since system tests simultaneously perform a large number of executions, it has been observed that execution time increases by several percent compared to provisional tests. For these reasons, submissions that are very close to the time limit may result in TLE in the system test. Please measure the execution time in your program to terminate the process, or have enough margin in the execution time.


Input

Input is given from Standard Input in the following format:

T H W i_0
h_{0, 0}h_{0, 1}\cdots h_{0, W-1}
\vdots
h_{H-2, 0}h_{H-2, 1}\cdots h_{H-2, W-1}
v_{0, 0}v_{0, 1}\cdots v_{0, W-2}
\vdots
v_{H-1, 0}v_{H-1, 1}\cdots v_{H-1, W-2}
K
S_1 D_1
\vdots
S_K D_K

h_{i, 0} \cdots h_{i, W-1} is a string of length W consisting of only 0 and 1. For block (i, j), if there is a waterway on the south side (along the segment between coordinates (i + 1, j) and (i + 1, j + 1)), then h_{i, j} = 1. Otherwise, if there is no waterway, h_{i, j} = 0.

v_{i, 0} \cdots v_{i, W-2} is a string of length W - 1 consisting of only 0 and 1. For block (i, j), if there is a waterway on the east side (along the segment between coordinates (i, j + 1) and (i + 1, j + 1)), then v_{i, j} = 1. Otherwise, if there is no waterway, v_{i, j} = 0.

The inputs of provisional test and system test are generated by the method described below in "Input Generation". The inputs satisfy the following regulations.

  • T = 100
  • H = W = 20
  • 0 \le i_0 \le H - 1
  • 0 < K \le HWT
  • 1 \le S_k < D_k \le T \quad (1 \le k \le K)
  • For any particular block, it is possible to reach it from the entrance by moving between adjacent blocks without crossing the waterway.

Output

Let the number of crops to plant be M, k_1, \dots, k_M be an arbitrary order of the crops to be planted, and crop k_m is planned to be planted in block (i_m, j_m) in the s_m-th month.

In this case, output to standard output (stdout) in the following format.

M
k_1 i_1 j_1 s_1
\vdots
k_M i_M j_M s_M

The output must meet the requirements described in the problem statement. Otherwise, the test case will be judged as WA.

Input Generation

Details

Let \mathrm{rand}(L, U) be a function that generates a uniform random floating-point number at least L and less than U. Let \mathrm{normal}(\mu, \sigma^2) be a function that generates a random value from the standard normal distribution with \mu as its mean and \sigma^2 as its variance.

  • T = 100
  • H = W = 20
  • Generate i_0 randomly from {0, 1, \dots, H - 1}.
  • Let G = (V, E) be the undirected graph with the lattice points on the land (including border) as its vertices, and segments of length 1 connecting two lattice points as its edges. Formally, V and E are defined as the following:
    • V = \lbrace(i, j) \mid 0 \le i \le H, 0 \le j \le W\rbrace
    • E = \lbrace((i_1, j_1), (i_2, j_2)) \in V \times V \mid |i_1 - i_2| + |j_1 - j_2| = 1\rbrace
  • Generate the waterways in the following way.
    • d = 1 + (\mathit{seed} \bmod 4)
    • Mark all vertices which locates on the border (that is, all (i, j) \in V where i = 0, i = H, j = 0, or j = W is satisfied).
    • Repeat the following processes.
      • Let V' be the set of vertices that is marked, and V'' = \lbrace (i', j') \in V \mid \forall (i, j) \in V', |i - i'| + |j - j'| > d \rbrace.
      • If V'' is empty, the process of generating waterways is ended. Otherwise, let (i, j) be a element selected randomly from V''.
      • Let (i', j') be the vertex in V' which is nearest to (i, j). If there are multiple candidates of (i', j'), let (i', j') be one of them randomly.
      • Let P be the shortest path in G connecting (i, j) and (i', j'). If there are multiple candidates of P, let P be one of the paths having the least turning points. If there are still multiple (at most 2) candidates of P, let P be one of them randomly.
      • Set all edges in P to waterways.
      • Mark all vertices in P.
  • Let L = \mathrm{round}(HWT \times \mathrm{rand(1.0, 2.0)}). For k = 1, 2, \dots, repeat the following processes to generate D_k and S_k.
    • Let L_k = \mathrm{round}(10^{\mathrm{normal}(1, 0.25^2)}). If 2 \le L_k \le T isn't satisfied, regenerate L_k.
    • Generate D_k randomly from {L_k, L_k + 1, \dots, T}.
    • Let S_k = D_k - L_k + 1.
    • If \sum_{i = 1}^k L_i \ge L, then the processes are ended.

Tools

  • Web version: This is more powerful than the local version when displaying animations.
  • Local version: You need a compilation environment of Rust language.
  • Sample code (C++): This sample code searches for a block to plant each crop, and choose the first one found. Only the first \min(K, 10) crops are considered.

Sharing visualization results is not allowed until the end of the contest.


Sample Input 1

10 6 6 3
011010
000001
011000
000110
011000
00000
10001
10010
00110
10101
10000
20
2 10
1 10
4 10
1 10
2 9
4 5
2 8
4 10
4 9
1 9
1 3
1 6
1 7
3 6
8 10
1 7
3 8
2 8
1 10
2 10

Sample Output 1

12
1 0 0 2
2 0 1 1
3 1 0 4
4 0 2 1
5 3 1 2
6 2 0 4
7 4 0 2
10 3 2 1
15 2 0 8
18 5 0 2
19 0 3 1
20 4 1 2

This sample is only to show a concrete example of input/output, and is smaller than the actual test cases used for scoring.