Uva127-Solitaire Game

Source: Internet
Author: User

The first question of the basic data structure in little white Book

Please poke http://luckycat.kshs.kh.edu.tw/

Problem Solving Ideas:

Use stacks to represent the movement of cards. The initial time to build 52 stacks, one element per stack.

Re-open the number group to store the top pointer. If the stack is empty, do not ignore the card when it moves.

Correctly find the location of the card placement, out of the stack, into the stack.

Note that each time you place it, you start by searching the next movable card from the beginning (leftmost) until no card can move.

Each time you read a card, you can see if you're moving. Until 52 cards have been put out.

Code:

#include <stdio.h>Const intMax_len = the+1;intTop[max_len];intlen, x;structCard {Charnum, deg;}; Card Stack[max_len][max_len];intFindNext (intIndexinttot)//find where the card should be placed {inti =index;  while(Tot && i>=0) {        if(Top[i]! =-1) {tot--; i--; } Elsei--; }     while(Top[i] = =-1&& i>=0) I--; returni;}BOOLMatch (Card card1, card card2)//Determine if the card can be put up {if(Card2.num = = Card1.num | | card2.deg = = card1.deg)return true; return false;}voidConquer ()//move the card continuously until no card can move {BOOLFlag =false, first =true;  while(Flag | |First ) {        BOOLChange =false;  for(intI=1; i< x; i++) {            if(top[i]<0)Continue; intj = FindNext (i,3); if(j>=0&&Match (Stack[j][top[j]], stack[i][top[i])) { Change=true; stack[j][++TOP[J]] =Stack[i][top[i]]; Top[i]--; if(Top[i] = =-1) len--;  Break; }            Else{J= FindNext (I,1); if(j>=0&&Match (Stack[j][top[j]], stack[i][top[i])) { Change=true; stack[j][++TOP[J]] =Stack[i][top[i]]; Top[i]--; if(Top[i] = =-1) len--;  Break; }            }        }        if(!change) flag =false;ElseFlag =true; if(first) First =false; }}intMain () {CharC;c =GetChar ();  while(c! ='#') {len=0;  for(intI=0; i<max_len; i++) {stack[i][0].num = stack[i][0].deg =0; Top[i] =0;} Stack[len][top[len]].num=C; Stack[len][top[len]].deg=GetChar (); X=0;  while(X < the&& C! ='#') {            if((C=getchar ()) = =' '|| c = ='\ n') {x++;                Conquer (); Len++; }            Else if(Stack[x][top[x]].num = =0) Stack[x][top[x]].num =C; ElseStack[x][top[x]].deg =C;        } conquer (); printf (Len==1?"%d pile remaining:":"%d piles remaining:", Len);  for(intI=0; i<x; i++)if(Top[i]! =-1) printf ("%d", top[i]+1); printf ("\ n"); C=GetChar (); }    return 0;}

Uva127-Solitaire Game

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.