127-"accordian" Patience Data Structure

Source: Internet
Author: User

 

Question link:

Http://uva.onlinejudge.org/index.php? Option = com_onlinejudge & Itemid = 8 & page = show_problem & problem = 63

Description:

Give a deck of cards, start with 52 heaps, and then perform the matching and merging operation. If a group of top cards matches the third or top cards on the left, then, the card is merged to the matched pile. This continues until there is only one pile left or it cannot be done. Note: if there are two cards that can be merged, do the leftmost card first. If one card can be merged with the First and Third stacks on the left, first, perform operations with the third heap. Matching Rule: The color is the same or the value is the same.

Solution:

Note that after merging, you must start processing from the next pile after merging.

 

 

# Include <iostream> # include <cmath> # include <cstdio> # include <cstdlib> # include <string> # include <cstring> # include <algorithm> # include <vector> # include <map> # define EPS 1e-6 # define Inf (1 <20) # define PI ACOs (-1.0) using namespace STD; struct inf {char save [55] [5]; int top ;}inf [55]; bool judge (int, int B) {int topa = inf [A]. top, topb = inf [B]. top; If (INF [A]. save [topa] [0] = inf [B]. save [topb] [0]) return true; If (INF [A]. Save [topa] [1] = inf [B]. save [topb] [1]) return true; return false;} void remove (INT m) {for (INT I = m + 1; I <= 52; I ++) // Delete the current MTH {If (INF [I]. top = 0) // The final return; INF [I-1] = inf [I]; INF [I]. top = 0; // left blank} int main () {char temp [5]; int ans [55]; while (scanf ("% s", temp) & * temp! = '#') {Strcpy (INF [1]. save [1], temp); INF [1]. top = 1; for (INT I = 2; I <= 52; I ++) {scanf ("% s", INF [I]. save [1]); INF [I]. top = 1 ;}int Len = 52; For (INT I = 2; I <= 52;) {If (INF [I]. top = 0) break; if (I> = 4 & judge (I-3, I) {INF [I-3]. top ++; strcpy (INF [I-3]. save [INF [I-3]. top], INF [I]. save [INF [I]. top]); INF [I]. top --; If (INF [I]. top = 0) {remove (I); // put all the spaces behind Len --;} I-= 3; // equivalent to moving forward 3} else if (I> = 2 & judge (I-1, I) {INF [I-1]. top ++; strcpy (INF [I-1]. save [INF [I-1]. top], INF [I]. save [INF [I]. top]); INF [I]. top --; If (INF [I]. top = 0) {remove (I); Len --;} I-= 1;} else I + = 1;} printf ("% d", Len ); len> 1? Printf ("piles"): printf ("pile"); printf ("remaining:"); // note the single and multiple numbers for (INT I = 1; I <= Len; I ++) printf ("% d", INF [I]. top); putchar ('\ 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.