码迷,mamicode.com
首页 > 其他好文 > 详细

uva 127 - "Accordian" Patience

时间:2014-08-10 18:11:40      阅读:312      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   os   io   strong   

 

 ``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

#include <iostream>
#include <stack>
#include <cstring>
#include <cstdio>
#include <string>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
#include <fstream>
#include <stack>
#include <list>

using namespace std;
/*
题目读了半天没动,只好百度下题意,结果还是理解错的,原本是叠加牌,被我直接覆盖了。
最后vector+数组模拟的栈居然过不了,无语,网上人家vector+stack结果却可以过,只好再写一次了。
活见鬼,整个过程从输入到处理必须要用char型;就连输入用char再转换成string都不行,算是涨姿势了。搞了好久
*/
#define ms(arr, val) memset(arr, val, sizeof(arr))
#define N 52
#define INF 0x3fffffff
#define vint vector<int>
#define sint set<int>
#define mint map<int, int>
#define lint list<int>
struct node
{
    int a, b;
    node(int a, int b):a(a), b(b){}
};
bool equal(node a, node b)
{
    if (a.a == b.a || a.b == b.b)
    {
        return true;
    }
    return false;
}
vector<stack<node>> vp;

void move(int i, int j)//由i移动到j
{
    //更新个数并移动操作
    vp[j].push(vp[i].top());
    vp[i].pop();
    if (vp[i].empty())
    {
        vp.erase(vp.begin() + i);//移除
    }
}
int main()
{
    int i, j;
    char s[3];
    while (scanf("%s", s), s[0] != #)
    {
        vp.clear();
        i = 1;
        while (true)//输入
        {
            stack<node> st;
            st.push(node(s[0], s[1]));
            vp.push_back(st);
            if (i >= N)
            {
                break;
            }
            i++;
            scanf("%s", s);
        }
        j = 1;
        for (i = 1; i < vp.size(); i = j)
        {
            while (j > 0)
            {
                if (j - 3 >= 0 && equal(vp[j].top(), vp[j - 3].top()))//先看左边第三个位置,开始少了个等号,找了半天
                {
                    move(j, j - 3);
                    j -= 3;
                    continue;
                }
                if (equal(vp[j].top(), vp[j - 1].top()))//再看左边第一个位置
                {
                    move(j, j - 1);
                    j--;
                    continue;
                }
                break;
            }
            j++;
            if (j < 1)
            {
                j = 1;
            }
        }
        cout<<vp.size();
        if (vp.size() == 1)
            cout << " pile remaining:";
        else
            cout << " piles remaining:";
        for (i = 0; i < vp.size(); i++)
        {
            cout <<   << vp[i].size();
        }
        cout << endl;
    }
    return 0;
}

 

uva 127 - "Accordian" Patience,布布扣,bubuko.com

uva 127 - "Accordian" Patience

标签:des   style   blog   http   color   os   io   strong   

原文地址:http://www.cnblogs.com/jecyhw/p/3902982.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!