HDU 1427 dfs 速算24点

原文链接: HDU 1427 dfs 速算24点

上一篇: HDU 1429 bfs 状态压缩

下一篇: 爬虫下载文章 BeautifulSoup

4个数通过 +,—,*,/和加括号,计算得24,

枚举数字和运算符,DFS即可,注意题目要求计算过程中都不能出现小数,所以做除法时稍作处理

枚举数组可用algorithm里的next_permutation

The next_permutation() function attempts to transform the given range of elements [start,end) into the next lexicographically greater permutation of elements. If it succeeds, it returns true, otherwise, it returns false.

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>

#define LL long long
int const MAX = 1e6 + 1;
int const INF = 1 << 30;
double const EPS = 0.00000001;
using namespace std;

int num[4];
bool flag;

//注意输入的10是两个字节
int getNum(char c){
    if (c == 'A') return 1;
    else if (c == 'J') return 11;
    else if (c == 'Q') return 12;
    else if (c == 'K') return 13;
    else if (c == '1') return 10;
    else return c - '0';
}

//括号内计算的pre,括号外的值next,计算到第pos个
void dfs(int pre, int next, int pos){
    if (flag == 0) return;

    //正在使用第四张
    if (pos == 3){
        if (pre - next == 24 || pre + next == 24 || pre * next == 24)
            flag = 0;

        if (next != 0 && pre % next == 0 && pre / next == 24)
            flag = 0;
        return;
    }

    //不加括号
    dfs(pre + next, num[pos + 1], pos + 1);
    dfs(pre - next, num[pos + 1], pos + 1);
    dfs(pre * next, num[pos + 1], pos + 1);
    if (next != 0 && pre % next == 0)
        dfs(pre / next, num[pos + 1], pos + 1);

    //加括号
    dfs(pre, next + num[pos + 1], pos + 1);
    dfs(pre, next - num[pos + 1], pos + 1);
    dfs(pre, next * num[pos + 1], pos + 1);
    if (num[pos + 1] != 0 && next % num[pos + 1] == 0)
        dfs(pre, next / num[pos + 1], pos + 1);

}
int main(){
    char str[3];
    while (scanf("%s", str) == 1){
        num[0] = getNum(str[0]);
        for (int i = 1; i < 4; i++){
            scanf("%s", str);
            num[i] = getNum(str[0]);
        }
        flag = 1;
        sort(num, num + 4);
        do {
            dfs(num[0], num[1], 1);
        } while (flag && next_permutation(num, num + 4));

        printf("%s\n", flag ? "No" : "Yes");
    }
    return 0;
}

/*分析:对于a,b,c,d四个数进行+,-,*,/。
有这几种情况:1.(a@b)@(c@d),(c@d)@(a@b),(b@a)@(c@d),... 综合为(x1@y1)@(x1@y1)这种情况,不过得到的值可以为-24
2.((a@b)@c)@d,a@((b@c)@d),(d@(a@b)@c),...综合为((x1@y1)@x2)@y2这种情况,不过得到的值可以为-24
*/

所以只要求a,b,c,d的全排列在用(a@b)@(c@b),((a@b)@c))@d去计算即可

1.用next_permutation求全排列:注意next_permutation求的是按字典序的全排列,所以要先排序

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <queue>
#include <algorithm>
#include <map>
#include <iomanip>
#define INF 99999999
using namespace std;

const int MAX = 5;
char s[3];
int number[MAX];

void check(char ch, int &num){
    if (ch == 'A') num = 1;
    else if (ch == 'J') num = 11;
    else if (ch == 'Q') num = 12;
    else if (ch == 'K') num = 13;
    else for (int i = 2; i < 10; ++i){
            if (ch == i + '0'){
                num = i; break;
            }
        }
    if (ch == '1') num = 10;
}

int f(int a, int op, int c){
    if (op == 0) return a + c;
    if (op == 1) return a - c;
    if (op == 2) return a * c;
    if (c && a % c == 0) return a / c;
    return INF;
}

bool calculate(int i, int j, int k){
    int temp1, temp2;
    temp1 = f(number[0], i, number[1]);
    if (temp1 != INF) temp2 = f(number[2], k, number[3]);
    if (temp2 == INF) temp1 = INF;
    if (temp1 != INF) temp1 = f(temp1, j, temp2);
    if (temp1 == 24 || temp1 == -24) return true;
    temp1 = f(number[0], i, number[1]);
    if (temp1 != INF) temp1 = f(temp1, j, number[2]);
    if (temp1 != INF) temp1 = f(temp1, k, number[3]);
    if (temp1 == 24 || temp1 == -24) return true;
    return false;
}

int main(){
    while (~scanf("%s", s)){
        check(s[0], number[0]);
        for (int i = 1; i <= 3; ++i){
            scanf("%s", s);
            check(s[0], number[i]);
        }
        sort(number, number + 4);
        bool flag = false;
        do {
            for (int i = 0; i < 4 && !flag; ++i) for (int j = 0; j < 4 && !flag; ++j) for (int k = 0; k < 4 && !flag; ++k){
                        flag = calculate(i, j, k);
                        //if(flag)cout<<number[0]<<' '<<number[1]<<' '<<number[2]<<' '<<number[3]<<endl;
                        //if(flag)cout<<i<<' '<<j<<' '<<k<<endl;
                    }
        } while (!flag && next_permutation(number, number + 4));
        if (flag) printf("Yes\n");
        else printf("No\n");
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值