Uva127 card games (Linked List)

Source: Internet
Author: User
 

I have been very decadent recently. I haven't been playing a problem for a long time. After reading my previous blog, I feel that all the goals I wrote at that time have been ruined. I don't know if I am awake now. It's not too late, all in all, I hope you can stick to it. I also hope this is a good start. Attack acmer !!!!!

Your mission is to create a licensing board named "Accordian". His statement is as follows:


A pair of poker cards have 52 million cards. First, we should arrange one card, and one card from left to right (there cannot be heavy cards, so there are a total of 52 million cards, each Heap has one card ), when a card has a "Match" with the other card on the left or the third card on the left, move the card to the card. Here the two Match cards refer to the suit or rank of these two cards. After you make a motion, you need to check whether other actions can be performed. At any time, only the top card can be moved. If a space (that is, the heap card that is moved has only one card) is generated because of the one-character mobile device card ), you must move all the cards on the right to the left. If you do not want to renew your account, you can find a mobile card until no mobile card is available.

When you select a card that can be moved, some players may be released. If you have two cards that can be moved, you should move the cards on the far left. When a card can be moved to the left side of a grid, or three to the left side, you must move to the left side of the three.

Input

Includes multiple groups of metadata. There are two groups of materials, each with 26 million cards. Each card is represented by 2 characters. The first character represents the point number of the card (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 # In a condition, it indicates the end of the condition. Please refer to Sample Input.

Output

The remaining heap cards and the number of cards for each heap are displayed at the end of each batch of metadata. Note that if there are only one heap of cards and pile does not include s, please test Sample Output.

Sample input
Qd ad 8 H 5S 3 H 5 h tc 4D jwks 6 H 8 s js ac as 8D 2 h qs ts 3 s ah 4 h th td 3C 6 S
8C 7D 4C 4S 7 S 9 H 7C 5D 2 s kd 2D qh jd 6D 9D JC 2C KH 3D QC 6C 9 s kc 7 H 9C 5C
AC 2C 3C 4C 5C 6C 7C 8C 9C tc jc qc kc ad 2D 3D 4D 5D 6D 7D 8D TD 9D JD QD KD
AH 2 H 3 H 4 H 5 H 6 H 7 H 8 H 9 h kh 6 s qh th as 2 S 3 S 4S JH 7 S 8 S 9 S TS JS QS KS
#

Sample Output
6 piles remaining: 40 8 1 1 1 1
1 pile remaining: 52

This is a typical simulation question. At that time, I felt that the algorithm was very simple, but it seemed that I could not write it down, because I had no brains or thought about it for a long time, I still referred to the ideas of the experts, but the only difference is that I learned something about stl. In addition, if you want to write data, you can find a data structure. Instead of using this structure, you can use it again. This is not good.

 
# Include <iostream> # include <string> # include <cstring> # include <cstdio> # include <cmath> # include <vector> using namespace std; const int N = 55; struct node {char face; char suit ;}; vector <node> List; // In short, vector is a dynamic array that can store any type, data can be added and compressed. Vector is a container. Here, the vector is a container of the node struct. Bool match (node a, node B) {return. face = B. face |. suit = B. suit;} bool test () {bool flag = false; int k, t; int s = List. size (); for (int I = s-1; I> 0; I --) {if (I-3> = 0 & match (List [I]. back (), List [I-3]. back () {k = I; t = 3; flag = true;} // according to the question, first move else if (I-1> = 0 & match (List [I]. back (), List [I-1]. back () {k = I; t = 1; flag = true;} // move to the left} if (flag) {List [k-t]. push_back (List [k]. back (); List [k]. pop_back (); I F (List [k]. empty () {List. erase (List. begin () + k); // after the group is moved, if the group is empty, move the group on the right to the left.} Return true;} return false;} // get the last position of the card. int main () {node p; vector <node> l; while (cin> p. face & p. face! = '#') {Cin> p. suit; l. clear (); l. push_back (p); List. clear (); List. push_back (l); for (int I = 2; I <= 52; I ++) {cin> p. face> p. suit; l. pop_back (); l. push_back (p); List. push_back (l);} while (test (); string c = List. size ()> 1? "S": ""; cout <List. size () <"pile" <c <"remaining:"; for (int I = 0; I <List. size (); I ++) {cout <''<List [I]. size () ;}cout <endl;} 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.