6163 - Myth Busters
来源:互联网 发布:阅读器哪个好用 知乎 编辑:程序博客网 时间:2024/05/18 10:12
#include <iostream>#include<cstring>#include<cstdio>using namespace std; const int INF = 1 << 30; int a[4];int res[4];bool vis[4];bool flag; int deal(int x,int y,int i) //四则运算{ switch(i) { case 0 : return x + y; case 1 : return x - y; case 2 : return x * y; case 3 : if(y != 0) return x / y;else return INF; }} bool can() //判断该种排列运用四则运算是否能算出{ int i,j,k; int t1,t2,t3; for(i = 0;i < 4;i ++) //(a @ b) @ (c @ d) { t1 = deal(res[0],res[1],i); if(t1 == INF) continue; for(j = 0;j < 4;j ++) { t2 = deal(res[2],res[3],j); if(t2 == INF) continue; for(k = 0;k < 4;k ++) { t3 = deal(t1,t2,k); if(t3 == 10) return 1; } } } for(i = 0;i < 4;i ++) //((a @ b) @ c) @ d { t1 = deal(res[0],res[1],i); if(t1 == INF) continue; for(j = 0;j < 4;j ++) { t2 = deal(t1,res[2],j); if(t2 == INF) continue; for(k = 0;k < 4;k ++) { t3 = deal(t2,res[3],k); if(t3 == 10) return 1; } } } for(i = 0;i < 4;i ++) //(a @ (b @ c)) @ d { t1 = deal(res[1],res[2],i); if(t1 == INF) continue; for(j = 0;j < 4;j ++) { t2 = deal(res[0],t1,j); if(t2 == INF) continue; for(k = 0;k < 4;k ++) { t3 = deal(t2,res[3],k); if(t3 ==10) return 1; } } } for(i = 0;i < 4;i ++) //a @ (b @ (c @ d)) { t1 = deal(res[2],res[3],i); if(t1 == INF) continue; for(j = 0;j < 4;j ++) { t2 = deal(res[1],t1,j); if(t2 == INF) continue; for(k = 0;k < 4;k ++) { t3 = deal(res[0],t2,k); if(t3 == 10) return 1; } } } for(i = 0;i < 4;i ++) //a @ ((b @ c) @ d) { t1 = deal(res[1],res[2],i); if(t1 == INF) continue; for(j = 0;j < 4;j ++) { t2 = deal(t1,res[3],j); if(t2 == INF) continue; for(k = 0;k < 4;k ++) { t3 = deal(res[0],t2,k); if(t3 == 10) return 1; } } } return 0; //都不能算出,说明无解} void dfs(int p) //全排列{ if(p == 4) { if(can()) flag = 1; return; } int i,j; for(i = 0;i < 4;i ++) { if(!vis[i]) { res[p] = a[i]; vis[i] = 1; dfs(p + 1); if(flag) return; vis[i] = 0; } }} int main(){ char s[3]; int i,j,k; int n; while(cin>>n&&n) { int f=0; while(n--) { cin>>s; if(f) continue; for(i = 0;i < 4;i ++) { a[i] = s[i] - '0'; } flag = 0; memset(vis,0,sizeof(vis)); dfs(0); if(flag==0) f=1; } if(f==0) printf("TRUE\n"); else printf("BUSTED\n"); } return 0;}