``Accordian‘‘ Patience |
You are to simulate the playing of games of ``Accordian‘‘ patience, the rules for which are as follows:
Deal cards one by one in a row from left to right, not overlapping. Whenever the card matches its immediate neighbour on the left, or matches the third card to the left, it may be moved
onto that card. Cards match if they are of the same suit or same rank. After making a move, look to see if it has made additional moves possible. Only the top card of each pile may be moved at any given time. Gaps between piles should be closed up as soon
as they appear by moving all piles on the right of the gap one position to the left. Deal out the whole pack, combining cards towards the left whenever possible. The game is won if the pack is reduced to a single pile.
Situations can arise where more than one play is possible. Where two cards may be moved, you should adopt the strategy of always moving the leftmost card possible. Where a card may be moved either one position to
the left or three positions to the left, move it three positions.
Input
Input data to the program specifies the order in which cards are dealt from the pack. The input contains pairs of lines, each line containing 26 cards separated by single space characters. The final line of the
input file contains a # as its first character. Cards are represented as a two character code. The first character is the face-value (A=Ace, 2-9, T=10, J=Jack, Q=Queen, K=King) and the second character is the suit (C=Clubs, D=Diamonds, H=Hearts, S=Spades).
Output
One line of output must be produced for each pair of lines (that between them describe a pack of 52 cards) in the input. Each line of output shows the number of cards in each of the piles remaining after playing
``Accordian patience‘‘ with the pack of cards as described by the corresponding pairs of input lines.
Sample Input
QD AD 8H 5S 3H 5H TC 4D JH KS 6H 8S JS AC AS 8D 2H QS TS 3S AH 4H TH TD 3C 6S 8C 7D 4C 4S 7S 9H 7C 5D 2S KD 2D QH JD 6D 9D JC 2C KH 3D QC 6C 9S KC 7H 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 2H 3H 4H 5H 6H 7H 8H 9H KH 6S QH TH AS 2S 3S 4S 5S JH 7S 8S 9S TS JS QS KS #
Sample Output
6 piles remaining: 40 8 1 1 1 1 1 pile remaining: 52
解决方式:
首先题意要细致理解,然后模拟就可以,只是,因为处理大量的字符,必须用scanf来输入,并且scanf不能对string类型进行输入,所以不能用string,要用字符数组,假设用string,就必须把字符赋给string,这样非常耗时,因为这个,这里tle了几次。然后是模拟,用vector,要2000多毫秒,用list可能比較少点。
代码:
#include<iostream> #include<cstdio> #include<string> #include<vector> #include<stack> using namespace std; struct node { char x,y; }; bool cmp(node a,node b) { if(a.x==b.x) return true; if(a.y==b.y) return true; return false; } int main(){ while(1) { vector<stack<node > >card;//定义vector(容器),包括的元素类型为stack<node>; char temp[3]; scanf("%c",&temp[0]); if(temp[0]==‘#‘) break; scanf("%c",&temp[1]); stack<node > p; node c; c.x=temp[0];c.y=temp[1]; p.push(c); card.push_back(p);//把栈存入card容器。 for(int i=1;i<52;i++) { getchar(); scanf("%c%c",&temp[0],&temp[1]); node c; c.x=temp[0];c.y=temp[1]; stack<node > t; t.push(c); card.push_back(t); } /* int len=card.size(); for(int i=0;i<len;i++) { cout<<card[i].top()<<endl; } getchar();*/ int len=card.size();//求容器的大小 for(int i=0;i<len;i++) { int j=i; if(j-3>=0){ if(cmp(card[j-3].top(),card[j].top())){ //容器也可像数组一样通过下标来使用。 card[j-3].push(card[j].top()); card[j].pop(); if(card[j].empty()) { card.erase(card.begin()+j);//删除某个位置的栈,參数必须为指向该位置的指针 len=card.size(); } i=-1; continue; } } if(j-1>=0){ if(cmp(card[j-1].top(),card[j].top())){ card[j-1].push(card[j].top()); card[j].pop(); if(card[j].empty()) { card.erase(card.begin()+j); len=card.size(); } i=-1; continue; } } } int l=card.size(); if(l>1) { printf("%d piles remaining:",l); for(int i=0;i<l;i++) printf(" %d",card[i].size()); } else { printf("%d pile remaining:",l); printf(" %d",card[0].size()); } printf("\n"); getchar(); } return 0; }
``Accordian'' Patience