24点游戏
##### Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others)
Submit Status

2424点就是给你一串数字，问你是否通过加减乘除括号构成2424点。

## Sample input and output

Sample Input Sample Output
2
3 3 8 8
1 1 1 1
yes
no

## Hint

33 33 88 88

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<map>
#include<string>
#include<vector>
using namespace std;
const int INF=0x3f3f3f3f;
#define SI(x) scanf("%d",&x)
#define PI(x) printf("%d",x)
#define P_ printf(" ")
#define mem(x,y) memset(x,y,sizeof(x))
typedef long long LL;
int flot;
double a[110];
int vis[110];
/*void dfs(int pos,double cur);
void ary(int num){
if(flot)return;
if(num==4){
//    for(int i=0;i<4;i++)printf("%d ",ans[i]);puts("");
dfs(1,ans[0]);
return;
}
for(int i=0;i<4;i++){
if(vis[i])continue;
ans[num]=a[i];
vis[i]=1;
ary(num+1);
vis[i]=0;
}
}
*/
void dfs(int usd,int tp){
if(flot)return;
if(usd==4){
if(abs(a[tp-1]-24)<1e-8)flot=1;
return;
}
/*
dfs(pos+1,cur+ans[pos]);
dfs(pos+1,cur-ans[pos]);
dfs(pos+1,cur*ans[pos]);
dfs(pos+1,cur/ans[pos]);
dfs(pos+1,-cur*ans[pos]);
dfs(pos+1,-cur/ans[pos]);
dfs(pos+1,ans[pos]/cur);
dfs(pos+1,ans[pos]/cur);
*/
for(int i=0;i<tp;i++){
if(vis[i])continue;
vis[i]=1;
for(int j=i+1;j<tp;j++){
if(vis[j])continue;
vis[j]=1;
a[tp]=a[i]+a[j];dfs(usd+1,tp+1);
a[tp]=a[i]*a[j];dfs(usd+1,tp+1);
a[tp]=a[i]-a[j];dfs(usd+1,tp+1);
a[tp]=a[j]-a[i];dfs(usd+1,tp+1);
if(a[i]!=0){
a[tp]=a[j]/a[i];dfs(usd+1,tp+1);
}
if(a[j]!=0){
a[tp]=a[i]/a[j];dfs(usd+1,tp+1);
}
vis[j]=0;
}
vis[i]=0;
}
}
int main(){
int T;
SI(T);
while(T--){
for(int i=0;i<4;i++)scanf("%lf",&a[i]);
flot=0;
mem(vis,0);
dfs(1,4);
if(flot)puts("yes");
else puts("no");
}
return 0;
}

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4250    Accepted Submission(s): 1044

Problem Description

Input

Output

Sample Input
A 2 3 6 3 3 8 8

Sample Output
Yes No

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
using namespace std;
int a[1010];
int vis[1010];
int ans;
void dfs(int usd, int tp){
if(ans)return;
if(usd == 4){
if(a[tp - 1] == 24)ans = 1;
return;
}
for(int i = 0; i < tp; i++){
if(vis[i])continue;
vis[i] = 1;
for(int j = i + 1; j < tp; j++){//注意应该从i + 1开始，可以让时间减少一般否则会超时；
if(vis[j])continue;
vis[j] = 1;
a[tp] = a[i] + a[j];dfs(usd + 1, tp + 1);
a[tp] = a[i] - a[j];dfs(usd + 1, tp + 1);
a[tp] = a[j] - a[i];dfs(usd + 1, tp + 1);
a[tp] = a[i] * a[j];dfs(usd + 1, tp + 1);

if(a[j] != 0 && a[i] % a[j] == 0){
a[tp] = a[i] / a[j];dfs(usd + 1, tp + 1);
}
if(a[i] != 0 && a[j] % a[i] == 0){
a[tp] = a[j] / a[i];dfs(usd + 1, tp + 1);
}

vis[j] = 0;
}
vis[i] = 0;
}
}
int main(){
char s[4][4];
while(~scanf("%s %s %s %s",s[0],s[1],s[2],s[3])){
for(int i =0;i < 4;i++){
if(strlen(s[i]) == 2)a[i] = 10;
else if(s[i][0] == 'A')a[i] = 1;
else if(s[i][0] == 'J')a[i] = 11;
else if(s[i][0] == 'Q')a[i] = 12;
else if(s[i][0] == 'K')a[i] = 13;
else a[i] = s[i][0] - '0';
}
ans = 0;
memset(vis, 0, sizeof(vis));
dfs(1, 4);
if(ans)
puts("Yes");
else
puts("No");
}
return 0;
}