Nyoj 24 Point game dfs

原创 2013年12月03日 13:22:07
/******
首先说明题意,
1,就是根据 + - * / 和这几个数能否得到一个数
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
double a[10];  // 储存数据
int vis[100];   
int n,m;
char s[10]; // 存储操作符
int fun()     // 生成a[]的全排列,对于每一个s[],枚举所有的a[]排列;
{
    do
    {
        double sum = a[0];
        for(int i=0;i<n-1;i++)
        if(s[i]=='+')
        sum+=a[i+1];
        else
        if(s[i]=='-')
        sum-=a[i+1];
        else
        if(s[i]=='*')
        sum*=a[i+1];
        else
        if(s[i]=='/')
        {
            if(a[i+1]==0)break;
            else
            sum/=a[i+1];
        }
        int x = int(sum+0.5);
       if(x==m){return 1;}

    }while(next_permutation(a,a+n));
    return 0;
}
int flog = 0;
void dfs(int count) // 生成操作符的全排列
{
    if(count==4)
    {
        if(fun())flog = 1;
        return ;
    }
    if(!vis[count])
    {
    vis[count] = 1;
    s[count] = '+';
    dfs(count+1);
    s[count] = '-';
    dfs(count+1);
     s[count] = '*';
    dfs(count+1);
    s[count] = '/';
    dfs(count+1);

    vis[count] = 0;
    }
}

int main()
{

    int icase;
    scanf("%d",&icase);
    while(icase--)
    {
        flog = 0;
        memset(vis,0,sizeof(vis));
        scanf("%d%d",&n,&m);
        for(int i=0;i<n;i++)
        scanf("%lf",&a[i]);
        sort(a,a+n);
        dfs(0);
        if(flog==1)puts("Yes");
        else puts("No");
    }
}

相关文章推荐

nyoj43 24 Point game(DFS)

题目43 题目信息 运行结果 本题排行 讨论区 24 Point game 时间限制:3000 ms  |  内存限制:65535 KB 难度:5 描述 There is a ...

NYOJ-43 24 Point game-DFS

24 Point game 时间限制:3000 ms  |  内存限制:65535 KB 难度:5 描述 There is a game which is call...

Nyoj 43 24 Point game 【DFS】

24 Point game 时间限制:3000 ms  |  内存限制:65535 KB 难度:5 描述 There is a game which is call...

NYOJ 43 24 Point game

24 Point game 时间限制:3000 ms  |  内存限制:65535 KB 难度:5 描述 There is a game which is called 24 Point...

深搜 nyoj 43 24 Point game

24 Point game 时间限制:3000 ms  |  内存限制:65535 KB 难度:5 描述 There is a game which is call...

NYOJ 43-24 Point game,24点游戏:记忆搜索

点击打开链接 24 Point game 时间限制:3000 ms  |  内存限制:65535 KB 难度:5 描述 There is a game which is c...

NYOJ 43 —24 Point game

24 Point game 时间限制:3000 ms  |  内存限制:65535 KB 难度:5 描述 There is a game which is call...

NYOJ43 24 Point game

描述 There is a game which is called 24 Point game. In this game , you will be given some numbers. Y...

nyoj43 24 Point game

24 Point game 时间限制:3000 ms  |  内存限制:65535 KB 难度:5 描述 There is a game which is called 24 ...

NYOJ43 24 Point game 【回溯】

24 Point game 时间限制:3000 ms  |  内存限制:65535 KB 难度:5 描述 There is a game which is called 24 Point...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Nyoj 24 Point game dfs
举报原因:
原因补充:

(最多只允许输入30个字)