知识点:模拟
这个题是第6章的,但是应该是练习链表的,但是主要还是个模拟,就用向量来写就行了,一开始肚子饿放下没写,过了一会儿再写14分钟过了,题目的意思也不难理解,就是模拟就行了
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
while (cin >> s && s != "#") {
vector<string> a[52];
a[0].push_back(s);
for (int i = 1; i < 52; i++) {
cin >> s; a[i].push_back(s);
}
vector<int> b;
for (int i = 0; i < 52; i++) b.push_back(i);
while (1) {
int is_move = 0;
for (int i = 1; i < (int) b.size(); i++) {
int l1 = (int) a[b[i]].size();
int l2 = (int) a[b[i - 1]].size();
string s1 = a[b[i]][l1 - 1];
string s2 = a[b[i - 1]][l2 - 1];
if (i >= 3) {
int l3 = (int) a[b[i - 3]].size();
string s3 = a[b[i - 3]][l3 - 1];
if (s1[0] == s3[0] || s1[1] == s3[1]) {
a[b[i - 3]].push_back(s1);
a[b[i]].erase(a[b[i]].begin() + l1 - 1);
if (!(int) a[b[i]].size()) b.erase(b.begin() + i);
is_move = 1; break;
}
}
if (s1[0] == s2[0] || s1[1] == s2[1]) {
a[b[i - 1]].push_back(s1);
a[b[i]].erase(a[b[i]].begin() + l1 - 1);
if (!(int) a[b[i]].size()) b.erase(b.begin() + i);
is_move = 1; break;
}
}
if (!is_move) break;
}
cout << (int) b.size();
if ((int) b.size() == 1) cout << " pile remaining:";
else cout << " piles remaining:";
for (int i = 0; i < (int) b.size(); i++) {
cout << " " << (int) a[b[i]].size();
}
cout << endl;
}
return 0;
}