模拟游戏,规则如下
把卡牌一张接一张,从左到右依次摊开,不可以重叠,每当某张卡片和左边(左边第三张)卡片匹配,
它就能放到另外一张卡片上,匹配的规则是他们有一样的级别或者花色,
在每次移动完成以后,还需要再次检查这次移动后是否会造成其他可能的移动,
只有每一叠卡片最上面那张能够移动,如果一叠牌被移空,应该立即将右边各叠整体向左移动,
补上这个空隙,依次将牌发完,并尽可能的向左合并,如果最后只剩下一叠牌,则游戏胜利
多玩几次可能会出现这样的局面,
有俩个卡片可以移动,你应该采取的策略是移动最左边的卡片
如果一张卡片能够移动到左边第一个位置和第三个位置,将它移动到第三个位置上
输入到程序的数据指出了一副牌发出的顺序,输入包括俩行,
每一行包含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);
}
}