2014
01-28

# UVa-127-Accordian Patience[模拟]

Time limit: 3.000 seconds

## Problem问题

You are to simulate the playing of games of “Accordian” patience, the rules for which are as follows:

Deal cards one by one in a row from left to right, not overlapping. Whenever the card matches its immediate neighbour on the left, or matches the third card to the left, it may be moved onto that card. Cards match if they are of the same suit or same rank. After making a move, look to see if it has made additional moves possible. Only the top card of each pile may be moved at any given time. Gaps between piles should be closed up as soon as they appear by moving all piles on the right of the gap one position to the left. Deal out the whole pack, combining cards towards the left whenever possible. The game is won if the pack is reduced to a single pile.

Situations can arise where more than one play is possible. Where two cards may be moved, you should adopt the strategy of always moving the leftmost card possible. Where a card may be moved either one position to the left or three positions to the left, move it three positions.

## Input输入

Input data to the program specifies the order in which cards are dealt from the pack. The input contains pairs of lines, each line containing 26 cards separated by single space characters. The final line of the input file contains a # as its first character. Cards are represented as a two character code. The first character is the face-value (A=Ace, 2-9, T=10, J=Jack, Q=Queen, K=King) and the second character is the suit (C=Clubs, D=Diamonds, H=Hearts, S=Spades).

## Output输出

One line of output must be produced for each pair of lines (that between them describe a pack of 52 cards) in the input. Each line of output shows the number of cards in each of the piles remaining after playing “Accordian patience” with the pack of cards as described by the corresponding pairs of input lines.

## Sample Input输入示例

QD AD 8H 5S 3H 5H TC 4D JH KS 6H 8S JS AC AS 8D 2H QS TS 3S AH 4H TH TD 3C 6S
8C 7D 4C 4S 7S 9H 7C 5D 2S KD 2D QH JD 6D 9D JC 2C KH 3D QC 6C 9S KC 7H 9C 5C
AC 2C 3C 4C 5C 6C 7C 8C 9C TC JC QC KC AD 2D 3D 4D 5D 6D 7D 8D TD 9D JD QD KD
AH 2H 3H 4H 5H 6H 7H 8H 9H KH 6S QH TH AS 2S 3S 4S 5S JH 7S 8S 9S TS JS QS KS
#

## Sample Output输出示例

6 piles remaining: 40 8 1 1 1 1
1 pile remaining: 52

(译注：第一个数字为剩下的叠数，当叠数为1时，后面输出“pile remaining: ”，否则输出“piles remaining: ”。接下来从左至右输出各叠牌的数量）

## Solution解答

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
//CARD结构体，存储一张牌，Face[0]表示花色，Face[1]表示点数
struct CARD {char Face[2];} Src;
//比较两张牌是否匹配。花色或点数相同即匹配
inline bool Match(const CARD &c1, const CARD &c2) {
return (c1.Face[0] == c2.Face[0] || c1.Face[1] == c2.Face[1]);
}
int main(void) { //主函数
typedef vector<CARD> PILE;
char Pack[256]; //用于存储输入的一副牌，下面循环读取输入的数据
for (string Line; getline(cin, Line) && Line[0] != '#'; cout << endl) {
strcpy(Pack, Line.c_str()); //读取并处理输入的数据
while (getline(cin, Line) && Line.empty()); //读取第2行
strcat(Pack, Line.c_str());
*remove(&Pack[0], &Pack[strlen(Pack)], ' ') = '\0'; //删除行中的空格
vector<PILE> Piles;
for (int i = 0; i < 52; ++i) { //循环发出每一张牌
PILE Stack(1, ((CARD*)&Pack)[i]);
Piles.push_back(Stack); //将新发的牌放在最后一叠
//j表示当前牌的位置，k表示j左边与之匹配的牌的位置
for (size_t j = Piles.size() - 1, k; j < Piles.size(); ++j) {
//以下循环向左查找可以移到的最左边的位置
for (k = j, Src = Piles[j].back(); k > 0; --k) {
if (k >= 3) { //先判定左边是否存在第3张
if (Match(Src, Piles[k - 3].back())) {
//如果与左边第3张匹配，则将k指向这张牌
k -= 2;
continue;
} //虚拟移到k指向的位置，继续向左查找
}
if (!Match(Src, Piles[k - 1].back())) {
break; //如果左边第3张和第1张都失配，跳出循环
}
}
if (k != j) { //k与原位置j不相等表示可以移动
Piles[k].push_back(Piles[j].back()); //移动牌
Piles[j].pop_back();
if (Piles[j].empty()) { //如果牌叠被移空则删除之
Piles.erase(Piles.begin() + j);
}
j = k; //将移动牌查找的起点定为k
}
}
} //以下按要求的格式输出结果。注意到Pile有单复数的区分
int nSize = Piles.size();
cout << nSize << " pile" << (nSize > 1 ? "s " : " ") << "remaining:";
for (int i = 0; i < nSize; cout << ' ' << Piles[i++].size());
}
return 0;
}

1. 我还有个问题想请教一下，就是感觉对于新手来说，递归理解起来有些困难，不知有没有什么好的方法或者什么好的建议？

2. 第二块代码if(it != mp.end())应改为if(it != mp.end() && (i+1)!=(it->second +1))；因为第二种解法如果数组有重复元素 就不正确