写了俩小时+把....有一种情况写的时候漏了...代码还算清晰把,想了很久才开写的。
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 using namespace std; 5 int p[21],flag[60],o[21],ans[60]; 6 int sp[21]; 7 int que[21]; 8 char str[15][3]; 9 int hu13[13] = {1,11,21,9,19,29,33,36,39,42,45,48,51}; 10 void judge13() 11 { 12 int i; 13 int s1,s0,s2,key; 14 s1 = s2 = s0 = 0; 15 for(i = 0; i < 13; i ++) 16 { 17 if(flag[hu13[i]] == 0) 18 { 19 s0 ++; 20 key = hu13[i]; 21 } 22 else if(flag[hu13[i]] == 1) 23 s1 ++; 24 else if(flag[hu13[i]] == 2) 25 s2 ++; 26 } 27 if(s1 == 13) 28 { 29 for(i = 0; i < 13; i ++) 30 ans[hu13[i]] = 1; 31 } 32 else if(s1 == 11&&s2 == 1&&s0 == 1) 33 { 34 ans[key] = 1; 35 } 36 return ; 37 } 38 void judge7d() 39 { 40 int i,num = 0,key; 41 for(i = 1; i < 60; i ++) 42 { 43 if(flag[i] == 2) 44 num ++; 45 else if(flag[i] == 1) 46 key = i; 47 } 48 if(num == 6) 49 ans[key] = 1; 50 return ; 51 } 52 int dfs(int n,int x,int step) 53 { 54 int i,j,a,b,c; 55 if(x == step) 56 return 1; 57 for(i = 0; i < n; i ++) 58 { 59 if(o[i] == 0) 60 { 61 if(sp[i+1] == sp[i]&&sp[i+2] == sp[i]&&o[i+1] == 0&&o[i+2] == 0) 62 { 63 o[i] = 1; 64 o[i+1] = 1; 65 o[i+2] = 1; 66 if(dfs(n,x+1,step)) 67 return 1; 68 o[i] = 0; 69 o[i+1] = 0; 70 o[i+2] = 0; 71 } 72 a = b = c = -1; 73 a = i; 74 for(j = i+1; j < n; j ++) 75 { 76 if(sp[j] > sp[i] + 2) break; 77 if(sp[j] == sp[i] + 1&&o[j] == 0) 78 b = j; 79 if(sp[j] == sp[i] + 2&&o[j] == 0) 80 { 81 c = j; 82 break; 83 } 84 } 85 if(b != -1&&c != -1) 86 { 87 o[a] = 1; 88 o[b] = 1; 89 o[c] = 1; 90 if(dfs(n,x+1,step)) 91 return 1; 92 o[a] = 0; 93 o[b] = 0; 94 o[c] = 0; 95 } 96 break; 97 } 98 } 99 return 0; 100 } 101 int main() 102 { 103 int t,i,num,j,k,u; 104 scanf("%d",&t); 105 while(t--) 106 { 107 memset(flag,0,sizeof(flag)); 108 memset(ans,0,sizeof(ans)); 109 for(i = 0; i < 13; i ++) 110 { 111 scanf("%s",str[i]); 112 num = str[i][0] - '0'; 113 if(str[i][1] == 'm') 114 que[i] = num; 115 else if(str[i][1] == 's') 116 que[i] = num + 10; 117 else if(str[i][1] == 'p') 118 que[i] = num + 20; 119 else 120 que[i] = num*3 + 30; 121 flag[que[i]] ++; 122 } 123 sort(que,que+13); 124 judge13(); 125 judge7d(); 126 for(i = 0; i < 13; i ++) 127 { 128 if(que[i] == que[i+1]) 129 { 130 num = 0; 131 for(j = 0; j < 13; j ++) 132 { 133 if(j == i||j == i+1) continue; 134 p[num++] = que[j]; 135 } 136 for(j = 0; j < 11; j ++) 137 { 138 for(k = j+1; k < 11; k ++) 139 { 140 if(p[j] == p[k]) 141 { 142 if(flag[p[j]] == 4) continue;//特判 143 num = 0; 144 for(u = 0; u < 11; u ++) 145 { 146 if(u == j||u == k) continue; 147 sp[num++] = p[u]; 148 } 149 memset(o,0,sizeof(o)); 150 if(dfs(9,0,3)) 151 { 152 ans[p[j]] = 1; 153 } 154 } 155 else if(p[j] + 1 == p[k]) 156 { 157 num = 0; 158 for(u = 0; u < 11; u ++) 159 { 160 if(u == j||u == k) continue; 161 sp[num++] = p[u]; 162 } 163 memset(o,0,sizeof(o)); 164 if(dfs(9,0,3)) 165 { 166 if(flag[p[j]-1] != 4) 167 ans[p[j]-1] = 1; 168 if(flag[p[k]+1] != 4) 169 ans[p[k]+1] = 1; 170 } 171 } 172 else if(p[j] + 2 == p[k]) 173 { 174 num = 0; 175 for(u = 0; u < 11; u ++) 176 { 177 if(u == j||u == k) continue; 178 sp[num++] = p[u]; 179 } 180 memset(o,0,sizeof(o)); 181 if(dfs(9,0,3)) 182 { 183 if(flag[p[j]+1] != 4) 184 ans[p[j]+1] = 1; 185 } 186 } 187 } 188 } 189 } 190 if(flag[que[i]] == 4) continue; 191 num = 0; 192 for(j = 0; j < 13; j ++) 193 { 194 if(j == i) continue; 195 sp[num++] = que[j]; 196 } 197 memset(o,0,sizeof(o)); 198 if(dfs(12,0,4)) 199 { 200 ans[que[i]] = 1; 201 } 202 } 203 int fi = 0; 204 for(i = 1; i <= 9; i ++) 205 if(ans[i])fi ++; 206 for(i = 11; i <= 19; i ++) 207 if(ans[i])fi ++; 208 for(i = 21; i <= 29; i ++) 209 if(ans[i])fi ++; 210 for(i = 33; i <= 51; i ++) 211 if(ans[i]) fi ++; 212 if(fi == 0) 213 printf("Nooten "); 214 else 215 { 216 printf("%d",fi); 217 for(i = 1; i <= 9; i ++) 218 if(ans[i])printf(" %dm",i); 219 for(i = 11; i <= 19; i ++) 220 if(ans[i])printf(" %ds",i-10); 221 for(i = 21; i <= 29; i ++) 222 if(ans[i])printf(" %dp",i-20); 223 for(i = 33; i <= 51; i ++) 224 if(ans[i])printf(" %dc",(i-30)/3); 225 printf(" "); 226 } 227 } 228 return 0; 229 } 230 /* 231 4 232 1s 1s 1s 1s 2s 2s 2s 2s 3s 3s 3s 3s 4s 233 */