Hi guys, I have been trying different test cases for many times... My code passed all available test cases, and I have been checking the code again and again... but I still don't have any idea on how my program could be wrong. Would anyone please take a look on my code?
The comment should be clear enough.
Code: Select all
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <map>
using namespace std;
#define x first
#define y second
typedef pair<int,int> P;
void bug(int x){
for(int i=0;i<13;i++)
putchar((x&(1<<i))?'1':'0');
puts(""); }
map<int,int> mp;
P p[13];
int f2(char *p){ return (*p-'A') * 26 + p[1]-'A'; }
const int M = 1<<13;
char tile[] = "TSW";
char fan[7][6] = {"DONG", "NAN", "XI", "BEI", "ZHONG", "FA", "BAI"};
int toPai(P p){
putchar(' ');
if(p.x <= 2){ printf("%d%c", p.y, tile[p.x]); }
else { printf("%s", fan[p.y]); }
}
int main(){
// "style" mapping
{
for(int r=0; r<3; r++)
mp[tile[r]] = r;
for(int r=0; r<7; r++)
mp[f2(fan[r])] = r;
}
int Zz = 0;
char s[14][7];
while(scanf("%s",s[0]), s[0][0]!='0'){
for(int r=1; r<13; r++)
scanf("%s", s[r]);
for(int r=0; r<13; r++)
{
if(isdigit(s[r][0])){
p[r].x = mp[s[r][1]];
p[r].y = s[r][0] - '0';
}else{
p[r].x = 3;
p[r].y = mp[f2(s[r])];
}
}
sort(p, p+13);
vector<int> a3, a6, a9, a2;
// a2, a3 (pong)
for(int a=0; a<13; a++)
for(int b=a+1; b<13; b++){
if(p[b]!=p[a]) continue;
int t = (1<<a) | (1<<b);
a2.push_back(t);
for(int c=b+1; c<13; c++){
if(p[c]!=p[b]) continue;
a3.push_back(t | (1<<c));
}
}
// a3 (chow)
for(int a=0; a<13 && p[a].x<3; a++)
for(int b=a+1; b<13; b++){
if(p[b].x != p[a].x) continue;
if(p[b].y != p[a].y+1) continue;
for(int c=b+1; c<13; c++){
if(p[c].x != p[b].x) continue;
if(p[c].y != p[b].y+1) continue;
int t = (1<<a) | (1<<b) | (1<<c);
a3.push_back(t);
}
}
// a6
for(int a=0; a<a3.size(); a++)
for(int b=a+1; b<a3.size(); b++){
int t = a3[a] | a3[b];
if( t== a3[a] + a3[b] )
a6.push_back(t);
}
// a9
for(int a=0; a<a6.size(); a++)
for(int b=0; b<a3.size(); b++){
int t = a6[a] | a3[b];
if( t== a6[a] + a3[b] )
a9.push_back(t);
}
vector<P> ans;
// a11
for(int a=0; a<a2.size(); a++)
for(int b=0; b<a9.size(); b++){
int t = a2[a] | a9[b];
if( t== a2[a] + a9[b] ){
int res = ~t;
int x=0;
while(((1<<x)&res)==0) x++;
int y=x+1;
while(((1<<y)&res)==0) y++;
if(p[x].x != p[y].x) continue;
else{
if(p[x].y==p[y].y)
ans.push_back(p[x]);
else if(p[x].x < 3){
if(p[x].y==p[y].y-2) // "Ka Loong"
ans.push_back(P(p[x].x, p[x].y+1));
else if(p[y].y==p[x].y+1){ // "Double Fly"
int a[2] = {p[x].y-1, p[x].y+2};
for(int k=0; k<2; k++)
if(a[k]>=1 && a[k]<=9)
ans.push_back(P(p[x].x, a[k]));
}
}
}
}
}
// a12
for(int a=0; a<a3.size(); a++)
for(int b=0; b<a9.size(); b++)
{
int t = a3[a] | a9[b];
if( t== a3[a] + a9[b] ){
int res = ~t;
int x = 0;
while(!(res&(1<<x))) x++;
ans.push_back(p[x]);
}
}
// printf("a2=%d a3=%d a6=%d a9=%d\n",
// a2.size(), a3.size(), a6.size(), a9.size());
printf("Case %d:", ++Zz);
if(ans.size()){
sort(ans.begin(), ans.end());
vector<P>::iterator ed = unique(ans.begin(), ans.end());
for(vector<P>::iterator it = ans.begin(); it != ed; ++it){
// Chk whether there are 4 already
int cnt = 0;
for(int i=0; i<13; i++) cnt += *it==p[i];
if(cnt < 4)
toPai(*it);
}
puts("");
}else{
puts(" Not ready");
}
}
return 0;
}