D - カジノ (Casino) 解説 /

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

Score: 100 points

Distributed Files

How to Submit to AtCoder

  • Include Azzurro.h and Bordeaux.h, and implement the functions specified in the statement.
  • Do not use standard input/output or file input/output.

Problem Statement

Azzurro and Bordeaux, a pair visiting a casino in Italy, decided to play a game proposed by the dealer Chiaro.

In this game, information is transmitted using an N \times N grid (N = 8)). The rows of the grid are numbered from 0 to N - 1 from top to bottom, and the columns of the grid are numbered from 0 to N - 1 from left to right. A cell with row number r and column number c is denoted by (r, c).

In this game, Azzurro and Bordeaux are isolated in separate rooms. They will play Q turns. The i-th turn (1 \leq i \leq Q) proceeds as follows.

  1. Azzurro receives from Chiaro an integer N, an integer L_i (1 \leq L_i \leq 51), a card on which a string S_i of length L_i consisting of 'A' and 'B' is written, and an N \times N grid whose cells are all colored white.
  2. Azzurro colors each of the N^2 cells either blue or red. He then hands the grid to Chiaro.
  3. Chiaro performs the following operations out of sight of both Azzurro and Bordeaux.
    1. He selects one path from (0, 0) to (N - 1, N - 1) by repeatedly moving only to an adjacent cell either down or to the right.
    2. For every cell on the path, if the cell is colored blue, he repaints it red; if it is colored red, he repaints it blue.
  4. Bordeaux receives from Chiaro a card with integers N and L_i written on it, along with the grid.
  5. Bordeaux writes a string of length L_i consisting of A and B on a sheet of paper. If the written string matches S_i, Azzurro and Bordeaux win.

Write programs which implement the strategies of Azzurro and Bordeaux to win this game. For the grading of this task, see Grading.

Implementation Details

You need to submit two files.

The first file is Azzurro.cpp. It should implement Azzurro's strategy. It should implement the following functions. The program should include Azzurro.h using the preprocessing directive #include.

  • std::vector<std::vector<int>> Azzurro(int N, int L, std::string S) This function is called Q times. The i-th call (1 \leq i \leq Q) corresponds to the procedures 1., 2.\ of the i-th turn of the game.
    • The parameter N is the integer N written on the card given to Azzurro in the procedure 1.\ of the i-th turn.
    • The parameter L is the integer L_i written on the card given to Azzurro in the procedure 1.\ of the i-th turn.
    • The parameter S is the string S_i written on the card given to Azzurro in the procedure 1.\ of the i-th turn.
    For each call to the function Azzurro, your program must return an N \times N two-dimensional array x, each of whose elements is either 0 or 1. If this condition is not satisfied, your program is judged as Wrong Answer [1].
    • If \texttt{x}[\texttt{r}][\texttt{c}] = 0 (0 \leq \texttt{r} \leq N - 1, 0 \leq \texttt{c} \leq N - 1), it indicates that the cell (\texttt{r}, \texttt{c}) is colored blue.
    • If \texttt{x}[\texttt{r}][\texttt{c}] = 1 (0 \leq \texttt{r} \leq N - 1, 0 \leq \texttt{c} \leq N - 1), it indicates that the cell (\texttt{r}, \texttt{c}) is colored red.

The second file is Bordeaux.cpp. It should implement Bordeaux's strategy. It should implement the following function. The program should include Bordeaux.h using the preprocessing directive #include.

  • std::string Bordeaux(int N, int L, std::vector<std::vector<int>> T) This function is called every time when Azzurro finishes painting the grid. This function is called Q times in total. The i-th call (1 \leq i \leq Q) corresponds to the procedures 4., 5.\ of the i-th turn of the game.
    • The parameter N is the integer N written on the card given to Bordeaux in the procedure 4.\ of the i-th turn.
    • The parameter L is the integer L_i written on the card given to Bordeaux in the procedure 4.\ of the i-th turn.
    • The parameter T is the N \times N two-dimensional array corresponding to the grid of cells given to Bordeaux in the procedure 4.\ of the i-th turn. The color of the cell (\texttt{r}, \texttt{c}) (0 \leq \texttt{r} \leq N - 10 \leq \texttt{c} \leq N - 1) is blue if \texttt{T[a][b]} = 0, and red if \texttt{T[a][b]} = 1.
    For each call to the function Bordeaux, your program must return a string s of length L_i consisting of 'A' and 'B'. If this condition is not satisfied, your program is judged as Wrong Answer [2].

Important Notices

  • Your program can implement other functions for internal use, or use global variables. Submitted files will be compiled with the grader, and become a single executable file. All global variables and internal functions should be declared in an unnamed namespace to avoid confliction with other files. When it is graded, it will be executed as two processes of Azzurro and Bordeaux. The process of Azzurro and the process of Bordeaux cannot share global variables.
  • Your program must not use the standard input and the standard output. Your program must not communicate with other files by any methods. However, your program may output debugging information to the standard error.

Compilation and Test Run

You can download an archive file from the contest webpage which contains the sample grader to test your program. The archive file also contains a sample source file of your program.

The sample grader is the file grader.cpp. In order to test your program, put grader.cppAzzurro.cppBordeaux.cppAzzurro.hBordeaux.h in the same directory, and run the following command to compile your programs.

g++ -std=gnu++20 -O2 -o grader grader.cpp Azzurro.cpp Bordeaux.cpp

Instead, you may run compile.sh contained in the archive file. In this case, run the following command to compile your programs.

./compile.sh

When the compilation succeeds, the executable file grader is generated.

Note that the actual grader is different from the sample grader. The sample grader will be executed as a single process, which will read input data from the standard input and write the results to the standard output.

In the actual judging grader, the path chosen by Chiaro is fixed in advance. That is, the path selected by Chiaro is determined before the functions Azzurro and Bordeaux in your submitted program are called.


Input for the Sample Grader

The sample grader reads the following data from the standard input.

Q N 
L_1 
S_1 
R_1 
L_2 
S_2 
R_2 
\vdots 
L_Q 
S_Q 
R_Q

Here, R_i (1 \leq i \leq Q) is a string of length 2(N - 1) consisting of exactly N - 1 occurrences of 'D' and N - 1 occurrences of 'R'. This string represents the path chosen by Chiaro in the i-th turn, which starts from (0, 0) and reaches (N - 1, N - 1) by repeatedly moving only to an adjacent cell either down or to the right. Specifically, starting from (0, 0), for each j = 1, 2, \cdots, 2(N - 1), if the j-th character of R_i is 'D', the move is to the adjacent cell below; if it is 'R', the move is to the adjacent cell to the right. Repeating this process results in reaching (N - 1, N - 1).

Output of the Sample Grader

The sample grader outputs the following information to the standard output (quotes for clarity).

  • If your program is judged as correct, it writes the value of L^{*} as "Accepted: 26". For the value of L^{*}, see Grading.
  • If your program is judged as any type of Wrong Answer, the sample grader writes its type as "Wrong Answer [1]".

If your program satisfies the conditions of several types of Wrong Answer, the sample grader reports only one of them. The sample grader may terminate the execution when one of the conditions for wrong answer is met.


Constraints

All the input data satisfy the following conditions.

  • 1 \leq Q \leq 30\,000.
  • N = 8.
  • 1 \leq L_i \leq 51 (1 \leq i \leq Q).
  • Q, L_i (1 \leq i \leq Q) are integers.
  • S_i (1 \leq i \leq Q) is a string of length L_i consisting of 'A' and 'B'.
  • R_i (1 \leq i \leq Q) is a string of length 2(N - 1) consisting of exactly N - 1 occurrences of 'D' and N - 1 occurrences of 'R'.

Grading

If your program is judged as any type of Wrong Answer [1] or Wrong Answer [2] (see Implementation Details), Time Limit Exceeded, Memory Limit Exceeded, or Runtime Error, in any testcase, your score is 0 points.

Otherwise, let L^{*} be the minimum of the following values for all test cases of this task. Your score is calculated as in the following table.

  • The maximum value of L such that Azzurro and Bordeaux win all the turns satisfying L_i \leq L. However, if they win all the turns in the test case, we set L = 51.

Sample Communication

Here is a sample input for the sample grader and corresponding function calls.

Sample Input 1

2 2
1
B
RD
3
ABB
DR

This sample input consists of Q \ (= 2) turns, and in each turn an N \times N grid (N = 2) is used. In this example, the first turn proceeds as follows.

  1. Azzurro colors (0, 1) and (1, 0) blue, and (0, 0) and (1, 1) red. He then hands the grid to Chiaro.
  2. Chiaro performs the following operations out of sight of Azzurro and Bordeaux.
    1. He selects the path (0, 0) \rightarrow (0, 1) \rightarrow (1, 1) as a path from (0, 0) to (N - 1, N - 1) by repeatedly moving only to an adjacent cell either down or to the right.
    2. For the three cells (0, 0), (0, 1), (1, 1) on this path, he changes their colors. As a result, the colors of (0, 0), (0, 1), (1, 1) become blue, red, and blue, respectively.
  3. Bordeaux can win this turn by writing "B" on the paper.

The second turn proceeds as follows.

  1. Azzurro colors all cells blue. He then hands the grid to Chiaro.
  2. Chiaro performs the following operations out of sight of Azzurro and Bordeaux.
    1. He selects the path (0, 0) \rightarrow (1, 0) \rightarrow (1, 1) as a path from (0, 0) to (N - 1, N - 1) by repeatedly moving only to an adjacent cell either down or to the right.
    2. For the three cells (0, 0), (1, 0), (1, 1) on this path, he changes their colors. As a result, the colors of (0, 0), (1, 0), (1, 1) all become red.
  3. Bordeaux can win this turn by writing "ABB" on the paper.

Note that this sample input does not satisfy the constraints of the problem. The file sample-01-in.txt, which can be downloaded from the contest site, corresponds to Sample Input 1. The file sample-02-in.txt, which can be downloaded from the contest site, is a sample input that satisfies the constraints.

配点: 100

配布ファイル

AtCoder での提出方法

  • Azzurro.h, Bordeaux.h を include し,問題文で指定された関数を実装してください.
  • 標準入出力やファイルへの入出力を使用しないでください.

問題文

イタリアのカジノを訪れた Azzurro と Bordeaux の 2 人組は,ディーラーの Chiaro に提案されたゲームを遊ぶことにした.

このゲームでは,NN 列 (N = 8) のマス目を介して情報を伝える.マス目の各行には上から順に 0 から N - 1 までの行番号が,各列には左から順に 0 から N - 1 までの列番号が付けられている.行番号が r であり,列番号が c であるマスを (r, c) と表記する.

このゲームでは,Azzurro と Bordeaux が別々の部屋に隔離された状態で Q 回のターンが行われる.i 回目 (1 \leqq i \leqq Q) のターンは次のように進行する.

  1. Azzurro は Chiaro から,整数 N, L_i (1 \leqq L_i \leqq 51) および 'A' と 'B' からなる L_i 文字の文字列 S_i が書かれたカードと,すべてのマスが白色で塗られた NN 列のマス目を受け取る.
  2. Azzurro は,N^2 個のマスについて,各マスを青色か赤色で塗る.その後,Chiaro にマス目を渡す.
  3. Chiaro は,以下の操作を Azzurro と Bordeaux から見えない場所で行う.
    1. 下または右に隣接するマスへの移動のみを繰り返して (0, 0) から (N - 1, N - 1) まで到達する経路を 1 つ選ぶ.
    2. 経路上にあるすべてのマスについて,そのマスが青色で塗られているならば赤色で塗り直し,赤色で塗られているならば青色で塗り直す.
  4. Bordeaux は Chiaro から,整数 N, L_i が書かれたカードとマス目を受け取る.
  5. Bordeaux は 'A' と 'B' からなる L_i 文字の文字列を紙に書く.書いた文字列が S_i と一致していれば,Azzurro と Bordeaux の勝利となる.

Azzurro と Bordeaux がこのゲームで勝利するための戦略を実装せよ.なお,この課題の採点方法については,採点基準の項を参照すること.

実装の詳細

あなたは 2 つのファイルを提出しなければならない.

1 つ目のファイルは Azzurro.cpp という名前である.このファイルは Azzurro の戦略を実装したファイルであり,以下の関数を実装していなければならない.また,#include プリプロセッサ指令によって Azzurro.h を読み込むこと.

  • std::vector<std::vector<int>> Azzurro(int N, int L, std::string S) この関数は合計 Q 回呼び出される.i 回目 (1 \leqq i \leqq Q) の呼び出しは,ゲームにおける i 回目のターンの手順 1.,手順 2. に相当する.
    • 引数 Ni 回目のターンの手順 1. で Azzurro が受け取るカードに書かれた整数 N である.
    • 引数 Li 回目のターンの手順 1. で Azzurro が受け取るカードに書かれた整数 L_i である.
    • 引数 Si 回目のターンの手順 1. で Azzurro が受け取るカードに書かれた文字列 S_i である.
    関数 Azzurro1 回の呼び出しについて,各要素が 0 または 1 である N \times N2 次元配列 \texttt{x} を返さなければならない.これが満たされない場合,不正解 [1] と判定される.
    • \texttt{x}[\texttt{r}][\texttt{c}] = 0 (0 \leqq \texttt{r} \leqq N - 10 \leqq \texttt{c} \leqq N - 1) のとき,マス (\texttt{r}, \texttt{c}) を青色で塗ることを表す.
    • \texttt{x}[\texttt{r}][\texttt{c}] = 1 (0 \leqq \texttt{r} \leqq N - 10 \leqq \texttt{c} \leqq N - 1) のとき,マス (\texttt{r}, \texttt{c}) を赤色で塗ることを表す.

2 つ目のファイルは Bordeaux.cpp という名前である.このファイルは Bordeaux の戦略を実装したファイルであり,以下の関数を実装していなければならない.また,#include プリプロセッサ指令によって Bordeaux.h を読み込むこと.

  • std::string Bordeaux(int N, int L, std::vector<std::vector<int>> T) この関数は Azzurro がマス目を塗り終わるたびに 1 回,合計で Q 回呼び出される.i 回目 (1 \leqq i \leqq Q) の呼び出しは,ゲームにおける i 回目のターンの手順 4.,手順 5. に相当する.
    • 引数 N は,i 回目のターンの手順 4. で Bordeaux が受け取るカードに書かれた整数 N である.
    • 引数 L は,i 回目のターンの手順 4. で Bordeaux が受け取るカードに書かれた整数 L_i である.
    • 引数 T は,i 回目のターンの手順 4. で Bordeaux が受け取るマス目の各マスの色を表す N \times N2 次元配列である.マス (\texttt{r}, \texttt{c}) (0 \leqq \texttt{r} \leqq N - 10 \leqq \texttt{c} \leqq N - 1) の色は,\texttt{T[r][c]} = 0 であれば青色,\texttt{T[r][c]} = 1 であれば赤色である.
    関数 Bordeaux1 回の呼び出しについて,'A' と 'B' からなる L_i 文字の文字列 s を返さなければならない.これが満たされない場合,不正解 [2] と判定される.

重要な注意

  • 内部での使用のために他の関数を実装したり,グローバル変数を宣言するのは自由である. ただし,提出された 2 つのプログラムは,採点プログラムとまとめてリンクされて 1 つの実行ファイルになるので, 各ファイル内のすべてのグローバル変数と内部関数を無名名前空間内で宣言して,他のファイルとの干渉を避ける必要がある. 採点時には,このプログラムは Azzurro 側,Bordeaux 側として 2 個のプロセスとして実行されるので, Azzurro 側と Bordeaux 側でプログラム中のグローバル変数を共有することはできない.
  • あなたの提出したプログラムは,標準入力・標準出力,あるいは他のファイルといかなる方法でもやりとりしてはならない. ただし,標準エラー出力にデバッグ情報等を出力することは許される.

コンパイル・実行の方法

作成したプログラムをテストするための,採点プログラムのサンプルが,コンテストサイトからダウンロードできるアーカイブの中に含まれている.このアーカイブには,提出しなければならないファイルのサンプルも含まれている.

採点プログラムのサンプルは 1 つのファイルからなる.そのファイルは grader.cpp である.作成したプログラムをテストするには,grader.cppAzzurro.cppBordeaux.cppAzzurro.hBordeaux.h を同じディレクトリに置き,次のようにコマンドを実行する.

g++ -std=gnu++20 -O2 -o grader grader.cpp Azzurro.cpp Bordeaux.cpp

なお,アーカイブの中に含まれている compile.sh というファイルを代わりに実行してもよい.その場合,次のようにコマンドを実行する.

./compile.sh

コンパイルが成功すれば,grader という実行ファイルが生成される.

実際の採点プログラムは,採点プログラムのサンプルとは異なることに注意すること.採点プログラムのサンプルは単一のプロセスとして起動する.このプログラムは,標準入力から入力を読み込み,標準出力に結果を出力する.

なお,実際の採点プログラムにおいて,Chiaro の選ぶ経路はあらかじめ定まっている.すなわち,あなたの提出したプログラムにおける関数 Azzurro や関数 Bordeaux が呼び出される前に,Chiaro の選ぶ経路は確定している.

採点プログラムのサンプルの入力

採点プログラムのサンプルは標準入力から以下の形式で入力を読み込む.

Q N 
L_1 
S_1 
R_1 
L_2 
S_2 
R_2 
\vdots 
L_Q 
S_Q 
R_Q

ここで,R_i (1 \leqq i \leqq Q) は,'D' と 'R' を N - 1 文字ずつ含む 2(N - 1) 文字の文字列である.この文字列は Chiaro が i 回目のターンで選ぶ,下または右に隣接するマスへの移動のみを繰り返して (0, 0) から (N - 1, N - 1) まで到達する経路を表す.その経路は,(0, 0) からスタートして,j = 1, 2, \cdots , 2(N - 1) の順に,R_ij 文字目が 'D' であれば下に隣接するマスに,'R' であれば右に隣接するマスに移動する,という操作を繰り返すことで最終的に (N - 1, N - 1) に到達する経路である.

採点プログラムのサンプルの出力

採点プログラムのサンプルは標準出力へ以下の情報を出力する(引用符は実際には出力されない).

  • 正解の場合,L^{*} の値が "Accepted: 26" のように出力される.L^{*} の値については採点基準の項を参照せよ.
  • 不正解の場合,不正解の種類が "Wrong Answer [1]" のように出力される.

実行するプログラムが複数の不正解の条件を満たした場合,表示される不正解の種類はそれらのうち 1 つのみである.採点プログラムのサンプルは,不正解の条件を満たした場合,途中で実行を打ち切ることがある.


制約

すべての入力データは以下の条件を満たす.

  • 1 \leqq Q \leqq 30\,000
  • N = 8
  • 1 \leqq L_i \leqq 51 (1 \leqq i \leqq Q).
  • Q, L_i (1 \leqq i \leqq Q) は整数である.
  • S_i (1 \leqq i \leqq Q) は 'A' と 'B' からなる L_i 文字の文字列である.
  • R_i (1 \leqq i \leqq Q) は 'D' と 'R' を N - 1 文字ずつ含む 2(N - 1) 文字の文字列である.

採点基準

この課題のテストケースの中で,1 つでも不正解 [1] または不正解 [2](実装の詳細を参照)と判定されたものや,実行時エラー(実行時間制限超過,メモリ制限違反,異常終了など)と判定されたものがあった場合,他のテストケースでどのターンに勝利したかにかかわらず無条件で 0 点となる.

そうでない場合,この課題のすべてのテストケースに対する以下の値の最小値を L^{*} とするとき,下表のように得点が与えられる.

  • L_i \leqq L を満たすすべてのターンについて勝利したような最大の整数 L.ただし,テストケース内のすべてのターンに勝利した場合は L = 51 とする.

やりとりの例

採点プログラムのサンプルが読み込む入力の例と,それに対応する関数の呼び出しの例を以下に示す.

入力例 1

2 2
1
B
RD
3
ABB
DR

この入力例は Q \ (= 2) 回のターンからなり,2 回のターンでは NN 列 (N = 2) のマス目を使用する.この例では,1 回目のターンは次のように進行する.

  1. Azzurro は (0, 1)(1, 0) を青色に,(0, 0)(1, 1) を赤色に塗る.その後,Chiaro にマス目を渡す.
  2. Chiaro は,以下の操作を Azzurro と Bordeaux から見えない場所で行う.
    1. 下または右に隣接するマスへの移動のみを繰り返して (0, 0) から (N - 1, N - 1) まで到達する経路として,(0, 0) \rightarrow (0, 1) \rightarrow (1, 1) を選ぶ.
    2. この経路上にある 3 つのマス (0, 0), (0, 1), (1, 1) について,そのマスに塗られた色を変更する.これにより,(0, 0), (0, 1), (1, 1) の色はそれぞれ青色,赤色,青色に変更される.
  3. Bordeaux は "B" と紙に書くことで,このターンでは勝利できる.

また,2 回目のターンは次のように進行する.

  1. Azzurro はすべてのマスを青色に塗る.その後,Chiaro にマス目を渡す.
  2. Chiaro は,以下の操作を Azzurro と Bordeaux から見えない場所で行う.
    1. 下または右に隣接するマスへの移動のみを繰り返して (0, 0) から (N - 1, N - 1) まで到達する経路として,(0, 0) \rightarrow (1, 0) \rightarrow (1, 1) を選ぶ.
    2. この経路上にある 3 つのマス (0, 0), (1, 0), (1, 1) について,そのマスに塗られた色を変更する.これにより,(0, 0), (1, 0), (1, 1) の色はすべて赤色に変更される.
  3. Bordeaux は "ABB" と紙に書くことで,このターンでは勝利できる.

この入力例は制約を満たさないことに注意すること.コンテストサイトからダウンロードできるファイルのうち,sample-01-in.txt は入力例 1 に対応する.コンテストサイトからダウンロードできるファイルのうち,sample-02-in.txtは制約を満たす.