一道让我崩溃的题目。。题目非常好理解但是我自己写的时候比较混乱。。最后是模仿着网上的代码写掉的
代码里用到<vector> ,第一次知道 用一个vector 的变量 a 用 a.erase() 后仍然可以使整个队列连续。比数组代码上方便。
题目:
``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
代码:
1 #include <iostream> 2 #include <string.h> 3 #include <cstdio> 4 #include <vector> 5 using namespace std; 6 7 const int maxn = 60; 8 9 struct node 10 { 11 char num; 12 char color; 13 }; 14 15 16 vector< vector<node> > List; 17 vector <node> card; 18 19 20 bool match(node a,node b) 21 { 22 if(a.color==b.color ||a.num == b.num ) return true; 23 else return false; 24 } 25 26 27 bool check() 28 { 29 bool flag =false; 30 int now,delta; 31 int s = List.size(); 32 for(int i=s-1;i>0;i--) 33 { 34 if(i-3>=0 && match(List[i].back(),List[i-3].back())) 35 { 36 now = i; 37 delta = 3; 38 flag = true; 39 } 40 else if(i-1>=0 && match(List[i].back(),List[i-1].back())) 41 { 42 now = i; 43 delta = 1; 44 flag=true; 45 } 46 } 47 if(flag) 48 { 49 List[now-delta].push_back(List[now].back()); 50 List[now].pop_back(); 51 if(List[now].empty())List.erase((List.begin()+now)); 52 return true; 53 } 54 return false; 55 56 } 57 58 int main() 59 { 60 //freopen("in.txt","r",stdin); 61 node p; 62 63 while(cin>>p.num && p.num != '#') 64 { 65 cin>>p.color; 66 card.clear(); 67 68 69 card.push_back(p); 70 List.clear(); 71 List.push_back(card); 72 73 for(int i=2;i<=52;i++) 74 { 75 cin>>p.num>>p.color; 76 card.pop_back(); 77 card.push_back(p); 78 List.push_back(card); 79 } 80 while(check()); 81 82 if(List.size()==1)cout<<List.size()<<" pile remaining:"<<" 52"<<endl; 83 else 84 { 85 cout<<List.size()<<" piles remaining:"; 86 for(int i=0;i<List.size();i++) 87 { 88 cout<<" "<<List[i].size(); 89 } 90 cout<<endl; 91 } 92 93 } 94 95 96 return 0; 97 }