D. Solitaire

Codeforces
IDCF71D
Time1000ms
Memory256MB
Difficulty
brute forceimplementation
English · Original
Chinese · Translation
Formal · Original
Vasya has a pack of 54 cards (52 standard cards and 2 distinct jokers). That is all he has at the moment. Not to die from boredom, Vasya plays Solitaire with them. Vasya lays out _nm_ cards as a rectangle _n_ × _m_. If there are jokers among them, then Vasya should change them with some of the rest of 54 - _nm_ cards (which are not layed out) so that there were no jokers left. Vasya can pick the cards to replace the jokers arbitrarily. Remember, that each card presents in pack exactly once (i. e. **in a single copy**). Vasya tries to perform the replacements so that the solitaire was _solved_. Vasya thinks that the solitaire is solved if after the jokers are replaced, there exist two non-overlapping squares 3 × 3, inside each of which all the cards either have the same suit, or pairwise different ranks. Determine by the initial position whether the solitaire can be solved or not. If it can be solved, show the way in which it is possible. ## Input The first line contains integers _n_ and _m_ (3 ≤ _n_, _m_ ≤ 17, _n_ × _m_ ≤ 52). Next _n_ lines contain _m_ words each. Each word consists of two letters. The jokers are defined as "_J1_" and "_J2_" correspondingly. For the rest of the cards, the first letter stands for the rank and the second one — for the suit. The possible ranks are: "_2_", "_3_", "_4_", "_5_", "_6_", "_7_", "_8_", "_9_", "_T_", "_J_", "_Q_", "_K_" and "_A_". The possible suits are: "_C_", "_D_", "_H_" and "_S_". All the cards are different. ## Output If the Solitaire can be solved, print on the first line "_Solution exists._" without the quotes. On the second line print in what way the jokers can be replaced. Three variants are possible: * "_There are no jokers._", if there are no jokers in the input data. * "_Replace J_x_ with _y_._", if there is one joker. _x_ is its number, and _y_ is the card it should be replaced with. * "_Replace J1 with _x_ and J2 with _y_._", if both jokers are present in the input data. _x_ and _y_ here represent distinct cards with which one should replace the first and the second jokers correspondingly. On the third line print the coordinates of the upper left corner of the first square 3 × 3 in the format "_Put the first square to (_r_, _c_)._", where _r_ and _c_ are the row and the column correspondingly. In the same manner print on the fourth line the coordinates of the second square 3 × 3 in the format "_Put the second square to (_r_, _c_)._". If there are several solutions to that problem, print any of them. If there are no solutions, print of the single line "_No solution._" without the quotes. See the samples to understand the output format better. [samples] ## Note The pretests cover all the possible output formats.
Vasya 有一副包含 #cf_span[54] 张牌的牌组(#cf_span[52] 张标准牌和 #cf_span[2] 张不同的鬼牌)。此刻他只有这些牌。为了不因无聊而死,他用这些牌玩接龙游戏。 Vasya 将 #cf_span[nm] 张牌摆成一个 #cf_span[n × m] 的矩形。如果其中包含鬼牌,他必须用剩下的 #cf_span[54 - nm] 张牌(未被摆出的)替换它们,使得最终没有鬼牌剩下。Vasya 可以任意选择用于替换的牌。请注意,每张牌在牌组中仅出现一次(即 *单张*)。Vasya 试图进行替换,使得接龙游戏被“解决”。 Vasya 认为,当鬼牌被替换后,如果存在两个不重叠的 #cf_span[3 × 3] 正方形,且每个正方形内所有牌要么花色相同,要么点数两两不同,则接龙游戏被解决。 请根据初始布局判断接龙是否可以被解决。如果可以解决,请给出一种可行的方案。 第一行包含整数 #cf_span[n] 和 #cf_span[m](#cf_span[3 ≤ n, m ≤ 17],#cf_span[n × m ≤ 52])。接下来的 #cf_span[n] 行每行包含 #cf_span[m] 个单词。每个单词由两个字母组成。鬼牌被定义为 "_J1_" 和 "_J2_"。其余牌的第一个字母表示点数,第二个字母表示花色。可能的点数有:"_2_"、"_3_"、"_4_"、"_5_"、"_6_"、"_7_"、"_8_"、"_9_"、"_T_"、"_J_"、"_Q_"、"_K_" 和 "_A_"。可能的花色有:"_C_"、"_D_"、"_H_" 和 "_S_"。所有牌均互不相同。 如果接龙可以解决,请在第一行输出 "_Solution exists._"(不含引号)。第二行输出如何替换鬼牌,有三种可能: 第三行以格式 "_Put the first square to (#cf_span[r], #cf_span[c])._" 输出第一个 #cf_span[3 × 3] 正方形左上角的坐标,其中 #cf_span[r] 和 #cf_span[c] 分别为行号和列号。第四行以同样格式输出第二个 #cf_span[3 × 3] 正方形的坐标:"_Put the second square to (#cf_span[r], #cf_span[c])._"。 如果存在多种解法,输出任意一种即可。 如果无解,请在单行输出 "_No solution._"(不含引号)。 请参考样例以更好地理解输出格式。 预测试覆盖所有可能的输出格式。 ## Input 第一行包含整数 #cf_span[n] 和 #cf_span[m](#cf_span[3 ≤ n, m ≤ 17],#cf_span[n × m ≤ 52])。接下来的 #cf_span[n] 行每行包含 #cf_span[m] 个单词。每个单词由两个字母组成。鬼牌被定义为 "_J1_" 和 "_J2_"。其余牌的第一个字母表示点数,第二个字母表示花色。可能的点数有:"_2_"、"_3_"、"_4_"、"_5_"、"_6_"、"_7_"、"_8_"、"_9_"、"_T_"、"_J_"、"_Q_"、"_K_" 和 "_A_"。可能的花色有:"_C_"、"_D_"、"_H_" 和 "_S_"。所有牌均互不相同。 ## Output 如果接龙可以解决,请在第一行输出 "_Solution exists._"(不含引号)。第二行输出如何替换鬼牌,有三种可能: "_There are no jokers._",如果输入数据中没有鬼牌。 "_Replace J#cf_span[x] with #cf_span[y]._",如果存在一个鬼牌。其中 #cf_span[x] 是其编号,#cf_span[y] 是它应被替换为的牌。 "_Replace J1 with #cf_span[x] and J2 with #cf_span[y]._",如果输入数据中同时存在两个鬼牌。其中 #cf_span[x] 和 #cf_span[y] 分别表示用于替换第一个和第二个鬼牌的两张不同牌。 第三行以格式 "_Put the first square to (#cf_span[r], #cf_span[c])._" 输出第一个 #cf_span[3 × 3] 正方形左上角的坐标,其中 #cf_span[r] 和 #cf_span[c] 分别为行号和列号。第四行以同样格式输出第二个 #cf_span[3 × 3] 正方形的坐标:"_Put the second square to (#cf_span[r], #cf_span[c])._"。 如果存在多种解法,输出任意一种即可。 如果无解,请在单行输出 "_No solution._"(不含引号)。 请参考样例以更好地理解输出格式。 [samples] ## Note 预测试覆盖所有可能的输出格式。
**Definitions** Let $ n, m \in \mathbb{Z} $ with $ 3 \leq n, m \leq 17 $ and $ n \times m \leq 52 $. Let $ G \in (\mathcal{C} \cup \{ \texttt{J1}, \texttt{J2} \})^{n \times m} $ be the $ n \times m $ grid of cards, where $ \mathcal{C} $ is the set of 52 standard playing cards, each uniquely represented by a rank-suit pair. Let $ R = \{ \texttt{2}, \texttt{3}, \texttt{4}, \texttt{5}, \texttt{6}, \texttt{7}, \texttt{8}, \texttt{9}, \texttt{T}, \texttt{J}, \texttt{Q}, \texttt{K}, \texttt{A} \} $ be the set of ranks. Let $ S = \{ \texttt{C}, \texttt{D}, \texttt{H}, \texttt{S} \} $ be the set of suits. Let $ J = \{ \texttt{J1}, \texttt{J2} \} $ be the set of jokers. Let $ U = \mathcal{C} \setminus \{ c \in \mathcal{C} \mid c \text{ appears in } G \} $ be the set of unused cards (size $ 54 - n m $). **Constraints** 1. $ |J \cap G| \in \{0, 1, 2\} $ 2. Each card in $ \mathcal{C} $ appears at most once in $ G \cup U $. 3. Jokers may be replaced only with cards from $ U $, each used at most once. **Objective** Determine whether there exists an assignment $ f: J \cap G \to U $ such that in the modified grid $ G' $ (with jokers replaced by cards from $ U $), there exist two non-overlapping $ 3 \times 3 $ subgrids $ Q_1, Q_2 \subseteq G' $ satisfying: - For each $ i \in \{1, 2\} $, either: (a) All cards in $ Q_i $ have the same suit, or (b) All cards in $ Q_i $ have pairwise distinct ranks. If such an assignment and pair $ (Q_1, Q_2) $ exist, output one such solution. Otherwise, output that no solution exists.
Samples
Input #1
4 6
2S 3S 4S 7S 8S AS
5H 6H 7H 5S TC AC
8H 9H TH 7C 8C 9C
2D 2C 3C 4C 5C 6C
Output #1
No solution.
Input #2
4 6
2S 3S 4S 7S 8S AS
5H 6H 7H J1 TC AC
8H 9H TH 7C 8C 9C
2D 2C 3C 4C 5C 6C
Output #2
Solution exists.
Replace J1 with 2H.
Put the first square to (1, 1).
Put the second square to (2, 4).
Input #3
4 6
2S 3S 4S 7S 8S AS
5H 6H 7H QC TC AC
8H 9H TH 7C 8C 9C
2D 2C 3C 4C 5C 6C
Output #3
Solution exists.
There are no jokers.
Put the first square to (1, 1).
Put the second square to (2, 4).
API Response (JSON)
{
  "problem": {
    "name": "D. Solitaire",
    "description": {
      "content": "Vasya has a pack of 54 cards (52 standard cards and 2 distinct jokers). That is all he has at the moment. Not to die from boredom, Vasya plays Solitaire with them. Vasya lays out _nm_ cards as a rect",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 1000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF71D"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "Vasya has a pack of 54 cards (52 standard cards and 2 distinct jokers). That is all he has at the moment. Not to die from boredom, Vasya plays Solitaire with them.\n\nVasya lays out _nm_ cards as a rect...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "Vasya 有一副包含 #cf_span[54] 张牌的牌组(#cf_span[52] 张标准牌和 #cf_span[2] 张不同的鬼牌)。此刻他只有这些牌。为了不因无聊而死,他用这些牌玩接龙游戏。\n\nVasya 将 #cf_span[nm] 张牌摆成一个 #cf_span[n × m] 的矩形。如果其中包含鬼牌,他必须用剩下的 #cf_span[54 - nm] 张牌(未被摆出的)替换它们,使得...",
      "is_translate": true,
      "language": "Chinese"
    },
    {
      "statement_type": "Markdown",
      "content": "**Definitions**  \nLet $ n, m \\in \\mathbb{Z} $ with $ 3 \\leq n, m \\leq 17 $ and $ n \\times m \\leq 52 $.  \nLet $ G \\in (\\mathcal{C} \\cup \\{ \\texttt{J1}, \\texttt{J2} \\})^{n \\times m} $ be the $ n \\times ...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments