题目
题意规则挺多的,但是读懂了还是清晰的,这里就不写了。
模拟题。我开了个双向链表,用struct 的 oop写的,没access control 看着挺乱但是写着还是挺爽的。130行...1Y。
代码:
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
#define MAXN 53 //52cards
struct Card
{
Card() {}
Card(char r, char s) { rank=r; suit = s; }
char rank;
char suit;
int isSame(Card c)
{
return rank==c.rank || suit==c.suit;
}
};
struct Pile //node
{
Pile() { top = 0; left = right = NULL; }
int top;
Card cards[MAXN];
Pile *left, *right;
int isSame(Pile* p)
{
return p->cards[p->top-1].isSame(cards[top-1]);
}
void add(Card c)
{
cards[top++] = c;
}
Card pop()
{
return cards[--top];
}
int isEmpty()
{
return top == 0;
}
};
struct List
{
List()
{
size = 0; head = tail = NULL;
}
int read() //初始化读入
{
size = 52;
head = tail = NULL;
char buf[3];
Pile* tp = NULL;
for(int i=0; i<52; i++)
{
scanf("%s", buf);
if(buf[0] == '#') return 0; //end of input
Pile* p= new Pile();
p->add(Card(buf[0], buf[1]));
if(!i) head = p;
else if(i==51) tail = p;
if(tp)
{
tp->right = p;
p->left = tp;
}
tp = p;
}
return 1;
}
Pile *head, *tail; //head->->tail
int size;
void del(Pile* p)
{
if(p->left)
{
p->left->right = p->right;
}
if(p->right)
{
p->right->left = p->left;
}
size--;
delete p;
}
void move(Pile* p, Pile* to) //p move to
{
to->add(p->pop());
if(p->isEmpty())
del(p);
}
int solve() //return 0 to finish
{
Pile* tp = head->right;
for(; tp; tp = tp->right)
{
if(tp->left && tp->left->left && tp->left->left->left && tp->left->left->left->isSame(tp)) //短路
{
move(tp, tp->left->left->left);
return 1;
}
if(tp->left && tp->left->isSame(tp))
{
move(tp, tp->left);
return 1;
}
}
return 0;
}
void print()
{
printf("%d pile", size);
printf(size>1? "s": "");
printf(" remaining:");
for(Pile* p=head; p; p=p->right) //head指针不会变
{
printf(" %d", p->top);
}
putchar('\n');
}
}row;
int main()
{
while(row.read())
{
while(row.solve()){}
row.print();
}
}