Ultraviolet A 127

Source: Internet
Author: User

Question type: Stack Simulation

Problem solving experience: crash ....

Question: (from Baidu space on spark)

Your task is to simulate a card game called "accordian". Its rules are as follows:

A deck of playing cards has 52 cards. First, arrange one card from left to right (there must be no overlap, so there are a total of 52 cards, each heap ), when a card matches the card on the left or the third card on the left, move the card to the card. Here, the two cards "match" refer to the two cards with the same color (suit) or number of points (rank. After you make a move, check whether other moves can be performed. At any time, only the top card can be moved. If a card is moved to produce a space (that is, the heap card that is moved has only one card), you must move all the cards on the right to the left. So constantly looking for removable cards, until no card can be moved to the end of the game.

Some situations may occur when you select a card that can be moved. If two cards can be moved, you should move the leftmost card. When a card can be moved to one cell on the left or three cells on the left, you must move to the three cells on the left.

Input

Enter multiple groups of test materials. There are two groups of test materials, each with 26 cards. Each card is represented by 2 characters. The first character represents the number of points (A = ace, 2 ~ 9, t = 10, j = Jack, q = queen, K = King), the second character represents the card color (C = clubs, D = diamonds, H = hearts, S = spades)

If you encounter a column containing only #, the input ends. See sample input.

Output

Output The number of heap cards remaining at the end of the game for each group of test materials and the number of cards per heap. Note that if there is only one heap card and pile is not followed by S, see sample output.

TIPS:

1. Try to use as many functions as possible. Do not use the stack in STL. The old time-out ....

2. Top [I] refers to the number of elements in the stack. (This is a binary array stack. If one dollar is used, top is the number)

3. Use continue for determination ....

Solution:

Just follow the questions to simulate

Code:

# Include <stdio. h>
# Include <string. h>
Typedef struct card
{
Char rank;
Char suit;
} Card;

Int top [52]; // mark the top of the stack

Bool COM (card a, card B)
{
If (A. Rank = B. Rank | A. Suit = B. Suit) return 1;
Else return 0;
}
Int get (Int & X, Int & Y, int POS)
{
Int I, prep = 1, flag = 1, num = 0;
X =-1; y =-1;
For (I = pos-1; I> = 0; I --)
{
If (top [I]> 0) num ++;
If (num = 1 & prep) {Y = I; prep = 0 ;}
 
If (num = 3) {x = I; break ;}
// If (Num> 3) break;
}
If (x> = 0 | Y> = 0)
Return 1;
Return 0;
} // Determine whether a matching card can be found and return the position of the matching card

Int main ()
{
Char input [2];
While (1)
{
Int I;
Card pile [52] [52];
Char temp [2];
Memset (top, 0, sizeof (top); // remember to initialize, it is a good habit
Scanf ("% s", temp );
If (temp [0] = '#') return 0;
Pile [0] [top [0]. Rank = temp [0];
Pile [0] [top [0] ++]. Suit = temp [1];
For (I = 1; I <52; I ++)
{
Scanf ("% s", temp );
Pile [I] [top [I]. Rank = temp [0];
Pile [I] [top [I] ++]. Suit = temp [1];
}
For (I = 1; I <52 ;)
{
Int x =-1, y =-1;
If (top [I] = 0)
{I ++; continue ;}

If (! Get (x, y, I) {I ++; continue ;}
// Printf ("### % d/N", I );

// Match and reorganize
If (x> = 0 & COM (pile [I] [top [I]-1], pile [x] [top [x]-1])
{
Pile [x] [top [x]. Rank = pile [I] [top [I]-1]. rank;
Pile [x] [top [x]. Suit = pile [I] [top [I]-1]. suit;
Top [x] ++;
Top [I] --;
I = x; continue; // pointer backtracking, decisive continue
}

X = y; // you can copy the above content ,.... Nothing else
If (x> = 0 & COM (pile [I] [top [I]-1], pile [x] [top [x]-1])
{
Pile [x] [top [x]. Rank = pile [I] [top [I]-1]. rank;
Pile [x] [top [x]. Suit = pile [I] [top [I]-1]. suit;
Top [x] ++;
Top [I] --;
I = X;
Continue;
}
I ++;
}
Int Total = 0;
For (I = 0; I <52; I ++)
If (top [I]> 0) Total ++;
If (Total = 1) printf ("1 pile remaining: 52 ");
Else
{
Printf ("% d piles remaining:", total );
For (I = 0; I <52; I ++)
If (top [I]> 0) {printf ("% d", top [I]);}
}
Printf ("/N ");
}
Return 0;
}

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.