显示原始代码
#include <bits/stdc++.h>
using namespace std;
int tiles[35];
vector<int> ans[35];
inline int id(string name) {
int x = name[0] - '1';
switch (name[1]) {
case 'm':
break;
case 's':
x += 9;
break;
case 'p':
x += 18;
break;
case 'z':
x += 27;
break;
}
return x;
}
inline string name(int id) {
string ans;
if (id >= 0 && id < 9) {
ans = "1m";
ans[0] += id;
} else if (id >= 9 && id < 18) {
ans = "1s";
ans[0] += id - 9;
} else if (id >= 18 && id < 27) {
ans = "1p";
ans[0] += id - 18;
} else if (id < 34) {
ans = "1z";
ans[0] += id - 27;
}
return ans;
}
bool shunzia[35] = { 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0,
0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0 };
inline bool shunzi(int left) {
if (left >= 0 && left < 35)
return shunzia[left];
return false;
}
bool search(int mianzi) {
if (mianzi == 4)
for (int i = 0; i < 34; ++i)
if (tiles[i] == 2)
return true;
else if (tiles[i] == 1)
return false;
for (int i = 0; i < 34; ++i) {
if (tiles[i] > 0 && tiles[i] < 2) {
bool zuhe = false;
for (int left = i - 2; left <= i; ++left) {
if (shunzi(left) && tiles[left] > 0 && tiles[left + 1] > 0 && tiles[left + 2] > 0) {
zuhe = true;
break;
}
}
if (!zuhe)
return false;
}
}
for (int i = 0; i < 34; ++i) {
if (tiles[i] > 0) {
if (tiles[i] >= 3) {
tiles[i] -= 3;
bool ans = search(mianzi + 1);
tiles[i] += 3;
if (ans)
return true;
}
int left = i - 2;
if (shunzi(left) && tiles[left] > 0 && tiles[left + 1] > 0 && tiles[left + 2] > 0) {
--tiles[left];
--tiles[left + 1];
--tiles[left + 2];
bool ans = search(mianzi + 1);
++tiles[left];
++tiles[left + 1];
++tiles[left + 2];
if (ans)
return true;
}
}
}
return false;
}
int main() {
#ifndef pasokon
ios::sync_with_stdio(false);
cin.tie(0);
#endif
int t;
cin >> t;
while (t--) {
string s;
cin >> s;
for (int i = 0; i < 28; i += 2) {
string sub;
sub += s[i];
sub += s[i + 1];
++tiles[id(sub)];
}
if (search(0))
cout << "Tsumo!\n";
else {
int cnt = 0;
for (int i = 0; i < 34; ++i) {
if (tiles[i]) {
--tiles[i];
for (int j = 0; j < 34; ++j) {
if (i != j) {
++tiles[j];
if (search(0))
ans[i].push_back(j);
--tiles[j];
}
}
++tiles[i];
}
if (ans[i].size() > 0)
++cnt;
}
cout << cnt << "\n";
for (int i = 0; i < 34; ++i) {
if (ans[i].size() > 0) {
cout << name(i) << " ";
for (int j = 0; j < ans[i].size(); ++j) cout << name(ans[i][j]) << " ";
cout << "\n";
}
ans[i].clear();
}
}
for (int i = 0; i < 34; ++i) tiles[i] = 0;
}
return 0;
}