Card Game
题面翻译
题目描述
两名玩家正在玩在线纸牌游戏。游戏使用一副
32
32
32 张牌进行。每张牌都有花色和数字。有四种花色:梅花、方块、红心和黑桃。用字符 C
、D
、H
和 S
分别表示它们。共有 8 种数字,按递增顺序为 2
、3
、4
、5
、6
、7
、8
、9
。
每张牌由两个字母表示:其等级和花色。例如,红心 8 可以表示为 8H
。
在游戏开始时,会选择一种花色作为王牌花色。
在每一轮中,玩家的操作如下:第一个玩家在桌子上放一张牌,第二个玩家必须用自己的一张牌打败这张牌。之后,两张牌都被移动到弃牌堆中。
一张牌可以打败另一张牌,如果两张牌都具有相同的花色,并且第一张牌的等级比第二张牌高。例如,方块 8 可以打败方块 4。此外,王牌可以打败任何非王牌牌,无论牌的等级如何,例如,如果王牌花色是梅花 (C
),那么梅花 3 可以打败方块 9。请注意,王牌只能被等级更高的王牌打败。
游戏中进行了 n n n 轮,因此弃牌堆现在包含 2 n 2n 2n 张牌。你想要重建游戏中进行的轮次,但是弃牌堆中的牌已经洗牌。找到可能在游戏中玩过的 n n n 轮的任何可能顺序。
输入格式
第一行包含整数 t t t( 1 ≤ t ≤ 100 1\le t\le100 1≤t≤100),表示测试数据数量。接下来是 t t t 个测试数据。
每个测试数据的第一行包含整数 n n n( 1 ≤ n ≤ 16 1\le n\le16 1≤n≤16)。
每个测试数据的第二行包含一个字符,即王牌花色。它是 CDHS
中的一个。
每个测试数据的第三行包含
2
n
2n
2n 张牌的描述。每张牌由一个两个字符的字符串描述,第一个字符是牌的等级,是 23456789
中的一个,第二个字符是牌的花色,是 CDHS
中的一个。所有牌都是不同的。
输出格式
对于每个测试用例,输出答案:
打印
n
n
n 行。在每一行中,以与输入相同的格式打印两张牌的描述:第一张牌是第一个玩家打出的牌,然后是第二个玩家用来打败它的牌。
如果没有解决方案,则打印一行 IMPOSSIBLE
。
如果有多个解决方案,则打印其中任何一个。
题目描述
Two players are playing an online card game. The game is played using a 32-card deck. Each card has a suit and a rank. There are four suits: clubs, diamonds, hearts, and spades. We will encode them with characters ‘C’, ‘D’, ‘H’, and ‘S’, respectively. And there are 8 ranks, in increasing order: ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’, ‘9’.
Each card is denoted by two letters: its rank and its suit. For example, the 8 of Hearts is denoted as 8H.
At the beginning of the game, one suit is chosen as the trump suit.
In each round, players make moves like this: the first player places one of his cards on the table, and the second player must beat this card with one of their cards. After that, both cards are moved to the discard pile.
A card can beat another card if both cards have the same suit and the first card has a higher rank than the second. For example, 8S can beat 4S. Additionally, a trump card can beat any non-trump card, regardless of the rank of the cards, for example, if the trump suit is clubs (‘C’), then 3C can beat 9D. Note that trump cards can be beaten only by the trump cards of higher rank.
There were $ n $ rounds played in the game, so the discard pile now contains $ 2n $ cards. You want to reconstruct the rounds played in the game, but the cards in the discard pile are shuffled. Find any possible sequence of $ n $ rounds that might have been played in the game.
输入格式
The first line contains integer $ t $ ( $ 1 \le t \le 100 $ ) — the number of test cases. Then $ t $ test cases follow.
The first line of a test case contains the integer number $ n $ ( $ 1\le n\le 16 $ ).
The second line of a test case contains one character, the trump suit. It is one of “CDHS”.
The third line of a test case contains the description of $ 2n $ cards. Each card is described by a two-character string, the first character is the rank of the card, which is one of “23456789”, and the second one is the suit of the card, which is one of “CDHS”. All cards are different.
输出格式
For each test case print the answer to it:
- Print $ n $ lines. In each line, print the description of two cards, in the same format as in the input: the first card that was played by the first player, and then the card that was used by the second player to beat it.
- If there is no solution, print a single line “IMPOSSIBLE”.
If there are multiple solutions, print any of them.
样例 #1
样例输入 #1
8
3
S
3C 9S 4C 6D 3S 7S
2
C
3S 5D 9S 6H
1
H
6C 5D
1
S
7S 3S
1
H
9S 9H
1
S
9S 9H
1
C
9D 8H
2
C
9C 9S 6H 8C
样例输出 #1
3C 4C
6D 9S
3S 7S
IMPOSSIBLE
IMPOSSIBLE
3S 7S
9S 9H
9H 9S
IMPOSSIBLE
6H 9C
9S 8C
思路分析
本题的关键就在于排序,一开始本想着用vector存放王牌和非王牌的两种情况,然后随取随用,后来发现这种想法不好处理,因为每一个都是数字加字母,排序的话可能会出现错误导致一些问题,所以我们根据H.S.C.D四种不同的情况进行分类排序处理即可。即用map存放前置字母,然后把当前出的牌放在后面。
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<map>
#include<vector>
#define x first
#define y second
using namespace std;
int t, n;
char a;
string s;
int main() {
cin >> t;
while (t--){
bool st = 1;
map<char, vector<string>>mp;//map相当于编号
cin >> n >> a;
int k = 0;
for (int i = 0; i < 2 * n; i++) {
cin >> s;
mp[s[1]].push_back(s);
}
vector<char>cc = { 'C','D','H','S' };
for (char c : cc) {
sort(mp[c].begin(), mp[c].end());//分类排序
}
for (char c : cc) {
if (c != a && mp[c].size() % 2 != 0) k++;//是否需要王牌协助
}
if ( k> mp[a].size() || mp[a].size() % 2 != k % 2) {
//王牌数量太小了或者王牌帮了忙导致王牌多余
cout << "IMPOSSIBLE" << endl;/直接不可能
continue;
}
for (char c : cc)
{
if (c == a) continue;//先不处理王牌
if (mp[c].size() % 2 == 1)//需要王牌协助处理的情况
{
cout << mp[c].back() << " " << mp[a].back() << endl;
mp[c].pop_back(), mp[a].pop_back();//用完了拿出来
}
}
for (char c :cc) {
for (int i = 0; i < mp[c].size(); i += 2) {
//剩下由于拍好了序两两分配即可
cout << mp[c][i] << " " << mp[c][i + 1] << endl;
}
}
}
return 0;
}