#include <stdlib.h>
#include <stdio.h>
struct page{
char ty;
char nu;
};
struct pnode{
struct page* p;
struct pnode* n;
};
struct pnode* packs[52];
struct page cards[52];
int l = 0;
/* copy page */
void coPa(struct page* pn, struct page* t){
/* printf("t:%c%c\n", pn->ty, pn->nu);*/
t->ty = pn->ty;
t->nu = pn->nu;
}
/* same page */
int samePa(struct page* a, struct page* b){
if(a->ty == b->ty || a->nu == b->nu)
return 1;
return 0;
}
/* move over to pos from original*/
void moveOver(struct pnode* pg, int pos, int ori){
if(packs[ori]->n->n != NULL)
packs[ori]->n = packs[ori]->n->n;
else if(packs[ori]->n->n == NULL){
int tot = ori;
while(tot < l){
packs[tot]->n = packs[tot+1]->n;
tot++;
}
l = l-1;
}
pg->n = packs[pos]->n;
packs[pos]->n = pg;
}
/* move page to pos*/
void move(struct page* pg, int pos){
struct pnode* ne = (struct pnode*)malloc(sizeof(struct pnode));
ne->n = packs[pos]->n;
ne->p = pg;
packs[pos]->n = ne;
}
/* judge whether move */
int judge(struct page* cp, int pos){
struct page thp, frp;
thp.nu = 'B';
frp.nu = 'B';
if(pos-3 >= 0)
coPa(packs[pos-3]->n->p, &thp);
if(pos-1>=0)
coPa(packs[pos-1]->n->p, &frp);
/* third cards exists and is the same*/
if(thp.nu != 'B' && samePa(&thp, cp)==1){
return 3;
}
if(frp.nu != 'B' && samePa(&frp, cp)==1){
return 1;
}
return 0;
}
/* traverse all the packs */
void traverse(){
int flag = -1;
int k = 1;
while( k < l){
struct pnode* pa = packs[k]->n;
int kres = judge(pa->p, k);
if(kres == 3 || kres == 1){
moveOver(pa, k-kres, k);
flag = 1;
break;
}
k++;
}
if(flag == 1) traverse();
}
void show(){
int i = 0;
int temp[52]={0};
while(i<l){
struct pnode* pg = packs[i];
while(pg->n != NULL){
struct page* ge = pg->n->p;
/*printf("%c%c ", ge->nu, ge->ty);*/
temp[i] += 1;
pg = pg->n;
}
i++;
}
if(i>1)
printf("%d piles remaining:", i);
else
printf("%d pile remaining:", i);
int j;
for(j=0; j<i; j++)
printf(" %d",temp[j]);
printf("\n");
}
void proc(){
int i = 1;
while(i<52){
int kres = judge(&cards[i], l);
if(kres == 3){
move(&cards[i], l-3);
traverse();
}
if(kres == 1){
move(&cards[i], l-1);
traverse();
}
if(kres == 0){
struct pnode* ne = (struct pnode*)malloc(sizeof(struct pnode));
ne->p = &cards[i];
ne->n = NULL;
packs[l]->n = ne;
l++;
}
/*show(); */
i++;
}
}
int main(void){
/* FILE* fp = fopen("data1.txt","r");*/
char num, tt;
int ct = 0;
int i;
for(i=0; i<52;i++){
packs[i] = (struct pnode*)malloc(sizeof(struct pnode));
packs[i]->n = NULL;
}
while(fscanf(stdin, "%c%c ", &num, &tt) && num != '#'){
cards[ct].nu = num;
cards[ct].ty = tt;
ct += 1;
if(ct == 52){
struct pnode* fp = (struct pnode*)malloc(sizeof(struct pnode));
fp->p = &cards[0];
fp->n = NULL;
packs[0]->n = fp;
l = 1;
proc();
show();
ct = 0;
l = 0;
}
}
/* system("pause");*/
return 0;
}
UVAOj 127 纸牌游戏
最新推荐文章于 2017-03-28 18:55:48 发布