模拟游戏,规则如下
把卡牌一张接一张,从左到右依次摊开,不可以重叠,每当某张卡片和左边(左边第三张)卡片匹配,
它就能放到另外一张卡片上,匹配的规则是他们有一样的级别或者花色,
在每次移动完成以后,还需要再次检查这次移动后是否会造成其他可能的移动,
只有每一叠卡片最上面那张能够移动,如果一叠牌被移空,应该立即将右边各叠整体向左移动,
补上这个空隙,依次将牌发完,并尽可能的向左合并,如果最后只剩下一叠牌,则游戏胜利

多玩几次可能会出现这样的局面,
有俩个卡片可以移动,你应该采取的策略是移动最左边的卡片
如果一张卡片能够移动到左边第一个位置和第三个位置,将它移动到第三个位置上

输入到程序的数据指出了一副牌发出的顺序,输入包括俩行,
每一行包含26个卡片,每个卡片用一个空格隔开,
输入文件的最后一行包含一个#作为它的第一个字符,
一张牌代表俩个字符单元,
第一个字符是面值(A=Ace,2-9,T=10,J=Jack,Q=Queen,K=King)
第二个字符是它的花色,(C=Clubs(梅花),D=Diamonds(钻石),H=Hearts(红心),S=Spades(黑桃))

每俩行(定义了一副52张牌)产生一个输出行,
每个输出行输出经过游戏后,剩下的叠数,每叠牌中的数目

AC代码如下,适用列表,一个Head数组

将tail指针指向数组位置绝对是噩梦

 #include <iostream>
#include<stdio.h>
using namespace std; struct Node
{
Node* next = NULL;
Node* pre = NULL;
//指向最后一个位置,不能是数组的位置
Node* tail = NULL;
char suit;
char face;
int total;
};
bool compareNode(const Node* node1, const Node* node2)
{ return (node1->face == node2->face) || (node1->suit == node2->suit);
}
/**
*
*get a head that node can link.
*if can not find,return NULL
*/
int findSuitableHead(Node* const heads, int total, Node* const node)
{
int index = -;
while (true)
{
bool fh = false;
if (total >= )
{
//可以找前面第三个
Node hNode = heads[total - ];
if (compareNode(hNode.tail, node))
{
total -= ;
index = total;
fh = true;
}
}
if (!fh && total >= )
{
//前三个位置不合适,前一个位置
Node hNode = heads[total - ];
if (compareNode(hNode.tail, node))
{
total -= ;
fh = true;
index = total;
}
}
if (!fh)
{
return index;
}
}
}
void reMegerNode(Node* const heads, int& total, int sIndex)
{
while (true)
{
bool hasResize = false;
for (int i = sIndex + ; i < total; i++)
{
int j = findSuitableHead(heads, i, (heads + i)->tail);
if (j != -)
{
//可以合并
Node* nHead = heads + j;
//这句话fb50
// Node mNode = heads[i];
Node* mNode = new Node;
*mNode = heads[i]; bool nm = heads[i].total == ? true : false;
if (nm)
{
//mNode is head
mNode->total = ;
mNode->tail = NULL;
nHead->tail->next = mNode;
mNode->pre = nHead->tail;
nHead->tail = mNode;
nHead->total++;
total--;
for (int k = i; k < total; k++)
heads[k] = heads[k + ]; Node n;
heads[total] = n;
}
else
{
//不是表头
heads[i].total--;
Node* tail = heads[i].tail;
Node* pre = tail->pre;
heads[i].tail = pre;
pre->next = NULL;
tail->pre = NULL;
nHead->tail->next = tail;
tail->pre = nHead->tail;
nHead->tail = tail;
nHead->total++;
}
sIndex = j == ? : j - ;
hasResize = true;
break;
}
}
if (!hasResize)
{
return;
}
} }
void mergeNode(Node* const heads, int& total, Node* node)
{
int sIndex = findSuitableHead(heads, total, node);
if (sIndex == -)
{
//new head
node->total = ;
heads[total] = *node;
heads[total].tail = node;
total++;
}
else
{
heads[sIndex].tail->next = node;
node->pre = heads[sIndex].tail;
heads[sIndex].tail = node;
heads[sIndex].total++;
reMegerNode(heads, total, sIndex);
}
}
void print(Node* const heads, int total)
{
if(total!=)
cout << total << " piles remaining:";
else
cout << total << " pile remaining:";
for (int i = ; i < total; i++)
{
cout << " " << heads[i].total;
}
cout << endl;
} int main()
{
//freopen("d:\\1.txt", "r", stdin);
char c1;
while (true)
{
Node* node = new Node;
cin >> c1;
if (c1 == '#')
{
return ;
}
node->face = c1;
cin >> node->suit;
int total = ;
Node heads[];
node->total = ;
heads[total++] = *node;
heads[total - ].tail = node;
for (int i = ; i < ; i++)
{
node = new Node;
cin >> node->face >> node->suit;
mergeNode(heads, total, node);
}
print(heads, total);
}
}

UVA127的更多相关文章

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

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

  2. UVa127,"Accordian" Patience

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

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

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

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

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

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

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

  6. 入门经典——基础数据结构专题(List)

    UVA127 链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_pr ...

  7. UVa 127 纸牌游戏(栈)

    https://vjudge.net/problem/UVA-127 题意: 按从左至右的顺序发牌,并摆成一行,发牌不要相互重叠.游戏中一旦出现任何一张牌与它左边的第一张或第三张“匹配”,即花色或点数 ...

随机推荐

  1. 利用git向github中推送文件

    /*游戏或者运动才能让我短暂的忘记心痛,现如今感觉学习比游戏和运动还重要——曾少锋*/ 如果对git不够熟悉的学者,可以参考:http://www.cnblogs.com/zengsf/p/75062 ...

  2. MyEclipse junit测试问题initializationError

    问题的情况如上. 问题的解决方法居然是:选中函数的整行,而不是只选中函数名,如下图选中运行junit测试. TestMySQL.testDriverMannager1Unrooted Testsini ...

  3. HDU 1176:免费馅饼(DP,自认为很详细的解释)

    免费馅饼 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submi ...

  4. 我的第一个Mybatis程序

    第一个Mybatis程序 在JDBC小结中(可以参阅本人JDBC系列文章),介绍到了ORM,其中Mybatis就是一个不错的ORM框架 MyBatis由iBatis演化而来 iBATIS一词来源于“i ...

  5. 解决安装vmware-tools出现的“The path "" is not a valid path to the 3.2.0-4-amd64 kernel headers”问题

    在用虚拟机安装使用64位Crunchbang(一种Debian GNU/Linux 的linux)的过程中出现很多小问题.其中vmware-tools安装就是第一个问题. 在使用终端安装vmware- ...

  6. windows下,java环境变量的设置,设置点击startup.bat启动tomcat

    1.首先.安装好java jdk以后环境变量设置: CLASSPATH:.;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\tools.jar JAVA_HOME:C:\ ...

  7. Spring核心思想:“控制反转”,也叫“依赖注入” 的理解

    @Service对应的是业务层Bean,例如: @Service("userService") public class UserServiceImpl implements Us ...

  8. O(n)线性空间的迷宫生成算法

    之前所有的迷宫生成算法,空间都是O(mn),时间同样是O(mn),时间上已经不可能更优化, 于是,我就从空间优化上着手,研究一个仅用O(n)空间的生成算法. 我初步的想法是,每次生成一行,生成后立即输 ...

  9. nginx 官方docker镜像使用教程

    最近在看nignx,在本地虚拟机使用docker nginx镜像搭建了nginx+php环境 整理的教程如下: 拉取nginx镜像docker pull nginx 创建一个容器,并挂载本地目录doc ...

  10. 【python】split 和 join函数

    一.关于split 和 join 方法 1只针对字符串进行处理.split:拆分字符串.join连接字符串2.string.join(sep): 以string作为分割符,将sep中所有的元素(字符串 ...