"Accordian" Patience

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 题意:给你一副牌,共有52张,摆成一行,每张牌由两个字符组成,第一个代表大小,第二个代表花色,每次操作是找到一堆牌最顶上的那张牌,如果他左边或者左边第3堆的最顶上的牌跟他一样
(大小或是花色一样),就把这张牌移过去(如果左边第3堆符合,则优先考虑,否则左边那堆),如果某一堆牌空了,则它后面的所有牌堆都往左移。如果某次操作有多组牌堆都符合,则优先考虑
最左边的,直到某次操作不能实现为止,最后打印有多少组牌堆剩余,注意这里有一个坑,如果只剩下一堆牌,pile后面没有s(QAQ),再按照从左到右的顺序剩余每组牌堆的牌数。 解析:很容易想到用链表去模拟,就按照他说的步骤一步步来,在查找左边或是左边第3堆时还要判断一下是否不存在,所以我把第一堆的le指向0,0的le再指向0,那么如果左边第3堆不存在时,你
查找的下标一定是0.还有52的ri指向一个越界的数,如53。 代码如下:

#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<set>
#include<map>
#include<queue>
#include<vector>
#include<iterator>
#include<utility>
#include<sstream>
#include<iostream>
#include<cmath>
#include<stack>
using namespace std;
const int INF=;
const double eps=0.00000001;
vector<string> card[];
struct node
{
int le,ri;
}nod[];
void init() // 建立链表
{
for(int i=;i<=;i++)
{
nod[i].le=i-;
nod[i].ri=i+;
}
nod[].le=;
nod[].ri=;
nod[].le=;
nod[].ri=;
}
int ans[];
bool Same(int a,int b) // 判断两张牌是否相同
{
string s1=card[a].back();
string s2=card[b].back();
if(s1[]==s2[]||s1[]==s2[]) return true;
return false;
}
void Link(int le,int ri)
{
nod[le].ri=ri;
nod[ri].le=le;
}
bool Match(int where)
{
int a=nod[where].le; // a是左边的
if(a==) return false; //左边都不存在,直接false
int b1=nod[a].le;
int b=nod[b1].le; // b是左边第三个
if(b!=&&Same(where,b)) //先考虑左边第三个
{
card[b].push_back(card[where].back()); // 添加到牌堆上
card[where].pop_back();
if(card[where].empty())
{
int L=nod[where].le,R=nod[where].ri; // 并判断该牌堆是否为空
Link(L,R); // 将左右连接起来
}
return true;
}
else if(Same(where,a)) // 再考虑左边的,跟上面操作一样
{
card[a].push_back(card[where].back());
card[where].pop_back();
if(card[where].empty())
{
int L=nod[where].le,R=nod[where].ri;
Link(L,R);
}
return true;
}
return false;
}
bool Find()
{
for(int st=nod[].ri;st<;) // 从剩余牌堆中找是否存在一个可行的牌堆
{
if(Match(st))
{
return true;
}
st=nod[st].ri;
}
return false;
}
void solve()
{
while(true)
{
if(!Find()) break; // 找可操作的牌堆,找不到就退出了
}
int go=;
for(int i=nod[].ri;i<;) // 从0指向的数开始保存
{
ans[go++]=card[i].size();
i=nod[i].ri;
}
if(go>) // 大于1输出时pile加s
{
printf("%d piles remaining:",go);
for(int i=;i<go;i++) printf(" %d",ans[i]);
printf("\n");
}
else printf("%d pile remaining: %d\n",,ans[]);
}
int main()
{
while(true)
{
string S;
cin>>S;
if(S=="#") break;
for(int i=;i<=;i++) card[i].clear(); // 清空动态数组
init();
card[].push_back(S);
for(int i=;i<=;i++) // 每堆添加一个数
{
cin>>S;
card[i].push_back(S);
}
solve();
}
return ;
}


 
 

UVA127- "Accordian" Patience(模拟链表)的更多相关文章

  1. [刷题]算法竞赛入门经典(第2版) 6-9/UVa127 - "Accordian" Patience

    题意:52张牌排一行,一旦出现任何一张牌与它左边的第一张或第三张"匹配",即花色或点数相同,则须立即将其移动到那张牌上面,将其覆盖.能执行以上移动的只有压在最上面的牌.直到最后没有 ...

  2. ACM学习历程——UVA127 "Accordian" Patience(栈, 链表)

    Description  ``Accordian'' Patience  You are to simulate the playing of games of ``Accordian'' patie ...

  3. UVA-127 "Accordian" Patience (模拟)

    题目大意:一种纸牌游戏,将52张扑克牌排成一列,每次操作可将一张扑克牌移到它的前一张或前面第三张上当牌的点数或花色匹配时.每次都移动最靠左的扑克牌,并且能移动三格就移动三格.求最终扑克牌状态. 题目分 ...

  4. UVA-127 "Accordian" Patience(模拟)

    题目: 把52张牌从左到右排好,每张牌自成一个牌堆.当某张牌与它左边那张牌或者左边第三张牌匹配时(花色或者点数相同)时,就把这张牌移到那张牌上面. 移动之后还要查看是否可以进行其他移动.只有位于牌堆顶 ...

  5. UVa127,"Accordian" Patience

    注意1堆的时候,pile后面没有s!!!!因为这个WA了一次,否则就1A了 犯了一个很幼稚很幼稚的错误,申请ans[]后玩了吧ans置0,结果调了好长好长时间,本来是敲完就能过的T T啊啊啊啊啊啊,一 ...

  6. ACM学习历程——UVA 127 "Accordian" Patience(栈;模拟)

    Description  ``Accordian'' Patience  You are to simulate the playing of games of ``Accordian'' patie ...

  7. Uva 127 poj 1214 `Accordian'' Patience 纸牌游戏 模拟

    Input Input data to the program specifies the order in which cards are dealt from the pack. The inpu ...

  8. hdu5009 Paint Pearls (DP+模拟链表)

    http://acm.hdu.edu.cn/showproblem.php?pid=5009 2014网络赛 西安 比较难的题 Paint Pearls Time Limit: 4000/2000 M ...

  9. UVa12657 - Boxes in a Line(数组模拟链表)

    题目大意 你有一行盒子,从左到右依次编号为1, 2, 3,…, n.你可以执行四种指令: 1 X Y表示把盒子X移动到盒子Y左边(如果X已经在Y的左边则忽略此指令).2 X Y表示把盒子X移动到盒子Y ...

随机推荐

  1. Android(java)学习笔记225:Activity 4 种启动模式

    1. 任务栈(task stack): 任务栈 是用来记录用户操作的行为,维护一个用户体验. 一个应用程序一般都是由多个activity组成的. 任务栈(task stack)记录存放用户开启的act ...

  2. POJ 1011 sticks 搜索

    Sticks Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 125918   Accepted: 29372 Descrip ...

  3. ASP.NET-FineUI开发实践-13(一)

    开原版好像是没有gird树,有一个扩展列下的模拟树列,就是不能展开,专业版有,开原版我弄了弄,思路是有,就是不是很好实现.这篇博客直接写出了(一)说明一个肯定是写不完的. FineUI重在封装,改这个 ...

  4. redisbook笔记——redis内存映射数据结构

    虽然内部数据结构非常强大,但是创建一系列完整的数据结构本身也是一件相当耗费内存的工作,当一个对象包含的元素数量并不多,或者元素本身的体积并不大时,使用代价高昂的内部数据结构并不是最好的办法. 为了解决 ...

  5. linux list all users.

    cat /etc/passwd sample of list users in linux. ref: Linux Command: List All Users In The System

  6. 写一个基于NSURLSession的网络下载库

    前段时间AFNetworking 更新到3.0 ,彻底废弃NSURLConnection的API转由NSURLSession来实现,代码并没有改动很大,AF封装的很好了,读完源码感觉收获很大. 下载不 ...

  7. addEventListener之handleEvent

    addEventListener() 方法是将指定的事件监听器注册到目标对象上,当该对象触发指定的事件时,指定的回调函数就会被执行.语法: element.addEventListener(type, ...

  8. 【USACO 2.1.2】法雷序列

    [问题描述]     对任意给定的一个自然数 n(n<=160), 将分母小于等于 n 的不可约的真分数按上升的次序排序 , 并且在第一个分数前加上 0/1, 而在最后一个分数后加上 1/1, ...

  9. ES5严格模式

    http://www.cnblogs.com/snandy/p/3428171.html 介绍了由ECMA262规范定义的Javascript标准,旨在改善错误检查功能并且标识不会延续到未来js版本的 ...

  10. jQuery 使用 jQuery UI 部件工厂编写带状态的插件(翻译)

    首先,我们要创建一个progress bar,它只允许我们简单的设置进度值.正如我们接下来将要看到的,我们需要通过调用 jQuery.widget 及其两个参数来实现这一操作,这两个参数分别是:将要创 ...