6163 - Myth Busters



#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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值