解法:每当输入一张牌,就判断能不能按照题目所给规则移动到前面,先判断能否移到前面第三堆,不能的话,再判断能否移到前一堆
只要读懂了题意,,就是很简单的一道题目,,一开始读错了题意,改了很长时间,看来读题能力需要加强
# 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();
}