CF71D.Solitaire

提高+/省选-

通过率:0%

时间限制:1.50s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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.

瓦西娅有一副54张的扑克牌(52张标准牌和2张互不相同的王牌)。目前他只有这些牌。为了不无聊至死,瓦西娅用它们玩单人纸牌游戏(Solitaire)。

瓦西娅将 $ nm $ 张牌以 $ n \times m $ 的矩形阵列方式铺开。若其中包含王牌,则瓦西娅需用剩余未铺出的 $ 54 - nm $ 张牌中的某些牌替换这些王牌,使得最终阵列中不再含有王牌。瓦西娅可任意选择用于替换王牌的牌。注意:每种牌在整副牌中仅出现一次(即每张牌均为唯一)。

瓦西娅试图通过替换使该单人纸牌游戏“被解出”。

瓦西娅认为单人纸牌游戏被解出,是指:在王牌被替换完毕后,存在两个互不重叠的 $ 3 \times 3 $ 子方阵,且在每个子方阵中,所有牌要么花色完全相同,要么点数两两互不相同。

请根据初始铺牌情况,判断该单人纸牌游戏是否可被解出。若可解出,请给出一种可行的替换方案。

输入格式

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.

第一行包含两个整数 nn 和 mm(满足 3≤n,m≤173 \leq n, m \leq 17,且 n×m≤52n \times m \leq 52)。接下来的 nn 行,每行包含 mm 个单词。每个单词由两个字母组成。大小王分别定义为 "J1" 和 "J2"。其余牌中,第一个字母表示点数(rank),第二个字母表示花色(suit)。可能的点数有:"2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K" 和 "A";可能的花色有:"C", "D", "H" 和 "S"。所有牌互不相同。

输出格式

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.

如果纸牌游戏有解,在第一行输出“Solution exists.”(不带引号)。在第二行输出鬼牌应如何替换。存在以下三种情况:

  • “There are no jokers.”,若输入数据中没有鬼牌;
  • “Replace J_x_ with y.”,若输入数据中仅有一个鬼牌;其中 x 是该鬼牌的编号,y 是它应被替换成的牌;
  • “Replace J1 with x and J2 with y.”,若输入数据中包含两个鬼牌;此处 x 和 y 为两个互不相同的牌,分别用于替换第一个和第二个鬼牌。

在第三行按格式“Put the first square to (r, c).”输出第一个 3×33 \times 3 方块的左上角坐标,其中 r 和 c 分别表示行号与列号。同理,在第四行按格式“Put the second square to (r, c).”输出第二个 3×33 \times 3 方块的左上角坐标。

若存在多个解,输出任意一个即可。

若无解,则在单独一行输出“No solution.”(不带引号)。

请参考样例以更清楚地理解输出格式。

输入输出样例

  • 输入#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

    输出#1

    No solution.
  • 输入#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

    输出#2

    Solution exists.
    Replace J1 with 2H.
    Put the first square to (1, 1).
    Put the second square to (2, 4).
  • 输入#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

    输出#3

    Solution exists.
    There are no jokers.
    Put the first square to (1, 1).
    Put the second square to (2, 4).

说明/提示

The pretests cover all the possible output formats.

预测试涵盖了所有可能的输出格式。

输入解题思路,AI测评打分。不知道怎么写?

首页