uva127--手风琴纸牌

解法:每当输入一张牌,就判断能不能按照题目所给规则移动到前面,先判断能否移到前面第三堆,不能的话,再判断能否移到前一堆

只要读懂了题意,,就是很简单的一道题目,,一开始读错了题意,改了很长时间,看来读题能力需要加强



# include <cstdio>
# include <iostream>
# include <algorithm>
# include <queue>
using namespace std;
typedef pair<char, char> Node;
Node pai;
deque<Node> dui[53];
void output(), solve(), dfs(int);
bool ok;
int cnt, t;

int main(void)
{
    cnt = 0, t = 0;
    char ch;
    while ( (ch = getchar()) != '#') {
        if (ch != ' ' && ch != '\n') {
            if (!t) pai.first = ch;
            else pai.second = ch;
            if (t) dui[cnt++].push_front(pai);
            t = !t; solve();
        }
        if (cnt == 52) output();
    }

    return 0;
}

void solve() {
    ok = false;
    for (int i = 0; i < cnt; ++i) {
        dfs(i); if (ok) i = 0;
        ok = false;
    }
}

void dfs(int cur) {
    if (dui[cur].empty()) return;
    int t1 = cur, t2 = cur, cnt1 = 0, cnt2 = 0;
    while (t1--) {
        if (!dui[t1].empty()) ++cnt1;
        if (cnt1 == 3) break;
    } if (cnt1 == 3) {
        deque<Node>& x = dui[cur], &y = dui[t1];
        bool okk = false;
        if (x.front().first == y.front().first || x.front().second == y.front().second) {
            ok = true; okk = true;
            y.push_front(x.front()); x.pop_front();
        }
        if (okk) { dfs(t1); return; }
    }
    while (t2--) {
        if (!dui[t2].empty()) ++cnt2;
        if (cnt2 == 1) break;
    } if (cnt2 == 1) {
        deque<Node>& x = dui[cur], &y = dui[t2];
        bool okk = false;
        if (x.front().first == y.front().first || x.front().second == y.front().second) {
            ok = true; okk = true;
            y.push_front(x.front()); x.pop_front();
        }
        if (okk) { dfs(t2); return; }
    }
}

void output() {
    cnt = 0;
    int sum = 0;
    for (int i = 0; i < 52; ++i) if (!dui[i].empty()) ++sum;
    printf("%d ", sum);
    if (sum == 1) printf("pile remaining:");
    else printf("piles remaining:");
    for (int i = 0; i < 52; ++i)
        if (!dui[i].empty()) printf(" %d", dui[i].size());
    printf("\n");
    for (int i = 0; i < 52; ++i)
        dui[i].clear();
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值