There are now 52 cards starting from the top of the card, and the cards are paved from left to right, when the current card is the same as the first card on the left or the third card on the left, the card is moved to the first card on the left or the third card on the left (this is a heap ), if this card is moved again and the color or number of points on the first or third card on the left is the same, the card continues to move. When multiple cards can be moved, move the cards on the left first. When a card can be moved to the third and first cards on the left, move to the third card on the left. Find the number of heap cards and the number of heap cards after 52 cards are sent. (Output from left to right)
Idea: Build a heap, link it with a linked list, and perform recursive simulation. For details, refer to the Code (Note: A linked list with a header node)
Ps: opposite to the question in the description
# Include <stdio. h>
# Include <string. h>
# Include <stdlib. h>
Struct stack
{
Char c [5];
};
Typedef struct node
{
Char c [5]; // stack top Element
Struct stack s [56]; // stack of each stack
Int top;
Struct node * next;
} * List;
Int judge1 (list l)
{
Return (l-> next! = NULL & l-> next! = NULL & l-> next! = NULL & l-> next! = NULL );
}
Int judge2 (list l)
{
Return (l-> next! = NULL & l-> next! = NULL );
}
Void f (list l)
{
Int top1 = 0, top2 = 0;
List p;
// If (l = NULL) return;
If (l-> next = NULL) return;
If (l-> next = NULL) return; // you cannot move it to the right.
Else if (judge1 (l) // you can move three places to the right.
{
Top1 = l-> next-> top;
While (top1! = 0 & judge1 (l) & (l-> next-> c [0] = l-> next-> c [0] | l-> next -> next-> c [1] = l-> next-> c [1])
{// Move three places to the right
Top2 = l-> next-> top;
Strcpy (l-> next-> s [top2 ++]. c, l-> next-> c); // stack entry
Strcpy (l-> next-> c, l-> next-> c); // you can specify the top element.
L-> next-> top ++; // stack plus 1
Top1 --;
L-> next-> top --;
If (top1)
Strcpy (l-> next-> c, l-> next-> s [top1-1]. c );
If (! Top1) // if it is null, move it to the right, merge
{
P = l-> next;
L-> next = l-> next;
Top1 = l-> next-> top; // after merging, you can move it to the right.
Free (p );
} // Check whether the data is merged and merge the data in time.
F (l-> next); // perform subsequent judgment. The cards on the right are judged first and recursive.
F (l-> next );
F (l-> next );
}
}
If (judge2 (l) // compare with the one on the right
{
Top1 = l-> next-> top;
While (top1! = 0 & judge2 (l) & (l-> next-> c [0] = l-> next-> c [0] | l-> next-> c [1] = l-> next-> c [1])
{
Top2 = l-> next-> top;
Strcpy (l-> next-> s [top2 ++]. c, l-> next-> c );
Strcpy (l-> next-> c, l-> next-> c );
L-> next-> top ++;
Top1 --;
L-> next-> top --;
If (top1)
Strcpy (l-> next-> c, l-> next-> s [top1-1]. c );
Else
{
P = l-> next;
L-> next = l-> next;
Free (p );
}
F (l-> next );
F (l); // The three judgments that can be moved to the right start to miss this kind of case. When there is a merge on the right, be sure to note that, in this case, you need to judge the third on the right.
}
}
}
Int main ()
{
Int I = 0, j = 0, len = 0, top = 0, cnt = 0, ans [60];
Char str1 [1, 200], str2 [200];
List l = (list) malloc (sizeof (struct node), p;
L-> next = NULL;
While (gets (str1 ))
{
L-> next = NULL;
Cnt = 0;
If (str1 [0] = '#') break;
Len = strlen (str1 );
Str1 [len] = '';
Gets (str1 + len + 1 );
Len = strlen (str1 );
For (I = 0; I <len; I + = 3)
{
P = (list) malloc (sizeof (struct node ));
P-> c [0] = str1 [I];
P-> c [1] = str1 [I + 1];
P-> c [3] = '\ 0 ';
Strcpy (p-> s [0]. c, p-> c );
P-> top = 1;
P-> next = l-> next;
L-> next = p;
F (l );
}
For (p = l-> next; p = p-> next)
{
Top = p-> top;
Ans [cnt ++] = top;
}
If (cnt = 1)
Printf ("% d pile remaining:", cnt );
Else
Printf ("% d piles remaining:", cnt );
For (I = cnt-1; I>-1; I --)
Printf ("% d", ans [I]);
Printf ("\ n ");
}
Return 0;
}