UVA - 127 "Accordian" Patience

来源:互联网 发布:mac双系统安装教程win7 编辑:程序博客网 时间:2024/05/01 17:02
ou 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 6S8C 7D 4C 4S 7S 9H 7C 5D 2S KD 2D QH JD 6D 9D JC 2C KH 3D QC 6C 9S KC 7H 9C 5CAC 2C 3C 4C 5C 6C 7C 8C 9C TC JC QC KC AD 2D 3D 4D 5D 6D 7D 8D TD 9D JD QD KDAH 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 11 pile remaining: 52题意:按从左至右的顺序发牌,并摆成一行,发牌不要相互重叠。游戏中一旦出现任何一张牌与它左边的第一张或第三张“匹配”,即花色或点数相同,则须立即将其移动到那张牌上面。如果牌被移动后又出现了上述情况,则需再次向左移动每叠牌只能移动最上面的一张。如果一叠牌被移空,应该立即将右边各叠整体向左移动,补上这个空隙。依次将整副牌都发完,并不断的向左合并。如果全部移动结束后整副牌都放成了一叠,则游戏胜利。玩上几次你就会遇到一些状况。如果同时有两张牌都可以移动,你应该采取的策略是移动最左边的牌(当然必须是可以移动的)。当一张牌既可以移动到左边第一张,又可以移动到左边第三张时,应移动到左边第三张上面。
#include <iostream>#include <algorithm>#include <string>#include <cstdio>#include <cstdlib>#include <cstring>using namespace std;#define M 60struct card{char s[5];}p[M];card ans[M][M];int top[M];char str[5];int fixed (int i, card key){int num = 0, j;for (j = i - 1; j > 0; j--){if (top[j] > 0){num++;if (num == 3){if (key.s[0] == ans[j][top[j]-1].s[0] || key.s[1] == ans[j][top[j]-1].s[1])return j;}}}num = 0;for (j = i - 1; j > 0; j--){if (top[j] > 0){num++;if (num == 1){if (key.s[0] == ans[j][top[j]-1].s[0] || key.s[1] == ans[j][top[j]-1].s[1])return j;}}}return -1;}int main(){int i;while (scanf ("%s", p[1].s)){if (strcmp (p[1].s, "#") == 0) break;memset (top, 0, sizeof(top));ans[1][top[1]++] = p[1];for (i = 2; i <= 52; i++){scanf ("%s", p[i].s);ans[i][top[i]++] = p[i];}for (i = 2; i <= 52; i++){if (top[i] <= 0) continue;int pos = i, pre;card key = ans[i][top[i]-1];while (pos != -1){pre = pos;pos = fixed (pos, key);}if (pre != i){top[i]--;ans[pre][top[pre]++] = key;i = pre;}}int num = 0;for (i = 1; i <= 52; i++)if (top[i] > 0) num++;if (num > 1)printf ("%d piles remaining:", num);else printf ("%d pile remaining:", num);for (i = 1; i <= 52; i++)if (top[i] > 0) printf (" %d", top[i]);printf ("\n");}return 0;}





0 0
原创粉丝点击