Description

 ``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 这个题目是一道栈模拟的题目,题目还可以,就是写的有点麻烦。 代码:
 #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <string>
#define inf 0x3fffffff
#define esp 1e-10
#define N 100005 using namespace std; struct node
{
int top;
char card[][];
}s[]; bool Input ()
{
scanf ("%s", s[].card[]);
if (s[].card[][] == '#')
return ;
s[].top = ;
for (int i = ; i < ; ++i)
{
s[i].top = ;
scanf ("%s", s[i].card[]);
}
return ;
} void Output ()
{
queue <int> q;
for (int i = ; i < ; ++i)
if (s[i].top != )
q.push(i);
int sum = q.size();
if (sum == )
printf ("1 pile remaining: %d\n", s[q.front()].top);
else
{
printf ("%d piles remaining:", sum);
int k;
while (!q.empty())
{
k = q.front();
q.pop();
printf (" %d", s[k].top);
}
printf ("\n");
}
} void qt ()
{
int p = ;
for (;;)
{
if (p == )
break;
if (s[p].top != )
{
int one = -, two = -;
int flag = , j = p-;
for (;;)
{
if (j < )
break;
if (s[j].top != )
{
flag++;
if (flag == )
{
one = j;
break;
}
if (flag == )
two = j;
}
j--;
}
if (one != -)
{
if (s[p].card[s[p].top-][] == s[one].card[s[one].top-][] ||
s[p].card[s[p].top-][] == s[one].card[s[one].top-][])
{
strcpy (s[one].card[s[one].top], s[p].card[s[p].top-]);
s[p].top--;
s[one].top++;
p = ;
continue;
}
}
if (two != -)
{
if (s[p].card[s[p].top-][] == s[two].card[s[two].top-][] ||
s[p].card[s[p].top-][] == s[two].card[s[two].top-][])
{
strcpy (s[two].card[s[two].top], s[p].card[s[p].top-]);
s[p].top--;
s[two].top++;
p = ;
continue;
}
}
}
++p;
}
} int main()
{
//freopen ("test.txt", "r", stdin);
while (Input ())
{
qt ();
Output ();
}
return ;
}
												

最新文章

  1. 【Oracle 集群】Linux下Oracle RAC集群搭建之Oracle DataBase安装(八)
  2. Iterm2 ssh tab title
  3. Lua源代码阅读分析问题列表(转)
  4. Lua 学习笔记(六)迭代器
  5. [转]响应式WEB设计学习(2)—视频能够做成响应式吗
  6. 1022. D进制的A+B (20)
  7. [git]解决rebase冲突
  8. 【助教】Java获取数据库数据展示
  9. Mac下的SVN客户端工具Cornerstone使用教程
  10. java中4中类修饰符访问范围
  11. hdu 3853 LOOPS(基础DP求期望)
  12. poj 3170
  13. java 读写锁
  14. mysql隐藏文件一定要删除彻底
  15. keil C51 指针总结
  16. ItextSharp代码示例
  17. ubunut在系统恢复模式下无法改动rootpassword的分析和解决
  18. IDEAL 热更新
  19. crontab下git命令无效
  20. layui(四)——table组件常见用法总结

热门文章

  1. 高仿微信app (含有发红包,聊天,消息等)用到 Rxjava+Retrofit+MVP+Glide技术
  2. RabbitMQ与Redis做队列比较
  3. poj2485
  4. 【BZOJ4894】天赋 有向图生成树计数
  5. Generally a good method to avoid this is to randomly shuffle the data prior to each epoch of training.
  6. ABAP内表数据做层次XML输出
  7. Android开发--多线程之Handler
  8. CSS3图片悬停放大动画
  9. keep-alive使用笔记
  10. 基于msm8909高通平台Android驱动开发之hello程序