C语言

24点游戏求解算法

2019-04-15  本文已影响10人  客昂康

24点游戏简介
一副牌中抽去大小王,从剩下的52张中任意抽取4张牌,用加、减、乘、除和括号把牌面上的数算成24,每张牌都必须使用,且只能用一次。举例如下:
【1】 1、2、3、4,那么1x2x3x4 = 244/(1/(2x3)) = 24(1+2+3)x4 = 24等等。
【2】 4、6、8、10,那么(8-6)x10+4 = 24(10-6)x4+8 = 24
【3】 5、4、3、3,那么(5+4)x3-3 = 24(5-3)x4x3 = 24(3/3+5)x4 = 24等等。

求解算法
用穷举法列出所有算式,如果结果等于24则输出。那么如何穷举呢?

C语言代码
实际代码中,对于求解算法的步骤【3】、【6】、【8】中的减法,只保留了较大的数减去较小的数的运算,丢弃了较小的数减去较大的数的运算。总的穷举次数也能算出来,6 x 5 x 3 x 5 x 1 x 5 = 2250 次。

//==============================================================================
//  Copyright (C) 2019 王小康. All rights reserved.
//
//  作者: 王小康
//  描述: 24点游戏求解程序
//  日期: 2019-04-15
//
//==============================================================================
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

typedef struct {
    int   integer;
    int   remainder;
    int   denominator;
    char *string;
} VALUE_T;

static int level_3(VALUE_T *a, VALUE_T *b){
    int va, vb, integer, remainder, denominator = a->denominator * b->denominator;
    int num = 0;
    // +
    va = (a->integer * denominator + a->remainder * b->denominator);
    vb = (b->integer * denominator + b->remainder * a->denominator);
    integer = (va + vb) / denominator;
    remainder = (va + vb) % denominator;
    if((integer == 24) && (remainder == 0)){
        printf("%s+%s = 24\n", a->string, b->string);
        num++;
    }
    
    // -
    va = (a->integer * denominator + a->remainder * b->denominator);
    vb = (b->integer * denominator + b->remainder * a->denominator);
    if(va >= vb){
        integer = (va - vb) / denominator;
        remainder = (va - vb) % denominator;
        if((integer == 24) && (remainder == 0)){
            printf("%s-%s = 24\n", a->string, b->string);
            num++;
        }
    }
    else{
        integer = (vb - va) / denominator;
        remainder = (vb - va) % denominator;
        if((integer == 24) && (remainder == 0)){
            printf("%s-%s = 24\n", b->string, a->string);
            num++;
        }
    }
    
    // *
    va = (a->integer * a->denominator + a->remainder);
    vb = (b->integer * b->denominator + b->remainder);
    integer = (va * vb) / denominator;
    remainder = (va * vb) % denominator;
    if((integer == 24) && (remainder == 0)){
        printf("%sx%s = 24\n", a->string, b->string);
        num++;
    }
    
    // /
    va = (a->integer * a->denominator + a->remainder) * b->denominator;
    vb = (b->integer * b->denominator + b->remainder) * a->denominator;
    if(vb){
        integer = va / vb;
        remainder = va % vb;
        if((integer == 24) && (remainder == 0)){
            printf("%s/%s = 24\n", a->string, b->string);
            num++;
        }
    }
    if(va){
        integer = vb / va;
        remainder = vb % va;
        if((integer == 24) && (remainder == 0)){
            printf("%s/%s = 24\n", b->string, a->string);
            num++;
        }
    }
    return num;
}

static int level_2(VALUE_T *a, VALUE_T *b, VALUE_T *c){
    char stringBuffer[32];
    VALUE_T value;
    int va, vb, denominator = a->denominator * b->denominator;
    int num = 0;
    
    // +
    va = (a->integer * denominator + a->remainder * b->denominator);
    vb = (b->integer * denominator + b->remainder * a->denominator);
    value.integer = (va + vb) / denominator;
    value.remainder = (va + vb) % denominator;
    value.denominator = denominator;
    sprintf(stringBuffer, "(%s+%s)", a->string, b->string);
    value.string = stringBuffer;
    num += level_3(&value, c);

    // -
    va = (a->integer * denominator + a->remainder * b->denominator);
    vb = (b->integer * denominator + b->remainder * a->denominator);
    if(va >= vb){
        value.integer = (va - vb) / denominator;
        value.remainder = (va - vb) % denominator;
        sprintf(stringBuffer, "(%s-%s)", a->string, b->string);
    }
    else{
        value.integer = (vb - va) / denominator;
        value.remainder = (vb - va) % denominator;
        sprintf(stringBuffer, "(%s-%s)", b->string, a->string);
    }
    value.denominator = denominator;
    value.string = stringBuffer;
    num += level_3(&value, c);
    
    // *
    va = (a->integer * a->denominator + a->remainder);
    vb = (b->integer * b->denominator + b->remainder);
    value.integer = (va * vb) / denominator;
    value.remainder = (va * vb) % denominator;
    value.denominator = denominator;
    sprintf(stringBuffer, "(%sx%s)", a->string, b->string);
    value.string = stringBuffer;
    num += level_3(&value, c);
    
    // /
    va = (a->integer * a->denominator + a->remainder) * b->denominator;
    vb = (b->integer * b->denominator + b->remainder) * a->denominator;
    if(vb){
        value.integer = va / vb;
        value.remainder = va % vb;
        value.denominator = vb;
        sprintf(stringBuffer, "(%s/%s)", a->string, b->string);
        value.string = stringBuffer;
        num += level_3(&value, c);
    }
    if(va){
        value.integer = vb / va;
        value.remainder = vb % va;
        value.denominator = va;
        sprintf(stringBuffer, "(%s/%s)", b->string, a->string);
        value.string = stringBuffer;
        num += level_3(&value, c);
    }
    return num;
}

static int level_1(VALUE_T *a, VALUE_T *b, VALUE_T *c, VALUE_T *d){
    char stringBuffer[32];
    VALUE_T value;
    int va, vb, denominator = a->denominator * b->denominator;
    int num = 0;
    
    // +
    va = (a->integer * denominator + a->remainder * b->denominator);
    vb = (b->integer * denominator + b->remainder * a->denominator);
    value.integer = (va + vb) / denominator;
    value.remainder = (va + vb) % denominator;
    value.denominator = denominator;
    sprintf(stringBuffer, "(%s+%s)", a->string, b->string);
    value.string = stringBuffer;
    num += level_2(&value, c, d);
    num += level_2(&value, d, c);
    num += level_2(c, d, &value);
    
    // -
    va = (a->integer * denominator + a->remainder * b->denominator);
    vb = (b->integer * denominator + b->remainder * a->denominator);
    if(va >= vb){
        value.integer = (va - vb) / denominator;
        value.remainder = (va - vb) % denominator;
        sprintf(stringBuffer, "(%s-%s)", a->string, b->string);
    }
    else{
        value.integer = (vb - va) / denominator;
        value.remainder = (vb - va) % denominator;
        sprintf(stringBuffer, "(%s-%s)", b->string, a->string);
    }
    value.denominator = denominator;
    value.string = stringBuffer;
    num += level_2(&value, c, d);
    num += level_2(&value, d, c);
    num += level_2(c, d, &value);
    
    // *
    va = (a->integer * a->denominator + a->remainder);
    vb = (b->integer * b->denominator + b->remainder);
    value.integer = (va * vb) / denominator;
    value.remainder = (va * vb) % denominator;
    value.denominator = denominator;
    sprintf(stringBuffer, "(%sx%s)", a->string, b->string);
    value.string = stringBuffer;
    num += level_2(&value, c, d);
    num += level_2(&value, d, c);
    num += level_2(c, d, &value);
    
    // /
    va = (a->integer * a->denominator + a->remainder) * b->denominator;
    vb = (b->integer * b->denominator + b->remainder) * a->denominator;
    if(vb){
        value.integer = va / vb;
        value.remainder = va % vb;
        value.denominator = vb;
        sprintf(stringBuffer, "(%s/%s)", a->string, b->string);
        value.string = stringBuffer;
        num += level_2(&value, c, d);
        num += level_2(&value, d, c);
        num += level_2(c, d, &value);
    }
    if(va){
        value.integer = vb / va;
        value.remainder = vb % va;
        value.denominator = va;
        sprintf(stringBuffer, "(%s/%s)", b->string, a->string);
        value.string = stringBuffer;
        num += level_2(&value, c, d);
        num += level_2(&value, d, c);
        num += level_2(c, d, &value);
    }
    return num;
}

static int level_0(int a, int b, int c, int d){
    char buffer[4][4];
    sprintf(buffer[0],"%d", a);
    sprintf(buffer[1],"%d", b);
    sprintf(buffer[2],"%d", c);
    sprintf(buffer[3],"%d", d);
    
    VALUE_T value[4];
    
    value[0].integer = a;
    value[0].remainder = 0;
    value[0].denominator = 1;
    value[0].string = buffer[0];
    
    value[1].integer = b;
    value[1].remainder = 0;
    value[1].denominator = 1;
    value[1].string = buffer[1];
    
    value[2].integer = c;
    value[2].remainder = 0;
    value[2].denominator = 1;
    value[2].string = buffer[2];
    
    value[3].integer = d;
    value[3].remainder = 0;
    value[3].denominator = 1;
    value[3].string = buffer[3];
    
    int num = 0;
    num += level_1(&value[0], &value[1], &value[2], &value[3]);
    num += level_1(&value[0], &value[2], &value[1], &value[3]);
    num += level_1(&value[0], &value[3], &value[1], &value[2]);
    num += level_1(&value[1], &value[2], &value[0], &value[3]);
    num += level_1(&value[1], &value[3], &value[0], &value[2]);
    num += level_1(&value[2], &value[3], &value[0], &value[1]);
    return num;
}

////////////////////////////////////////////////////////////////////////////////

static int parameterCheck(int argc, char* argv[]){
    char *p;
    int value;
    while(*argv){
        p = *argv;
        if((*p < '1') || (*p > '9')) return 1;
        p++;
        while(*p){
            if((*p < '0') || (*p > '9')) return 1;
            p++;
        }
        value = atoi(*argv);
        if((value < 1) || (value > 13)) return 1;
        argv++;
    }
    return 0;
}

static void help(char *name){
    printf("Usage  : %s num1 num2 num3 num4\n", name);
    printf("Example: %s 5 5 5 1\n", name);
}

int main(int argc, char* argv[]){
    if(argc < 5){
        help(argv[0]);
        return 0;
    }
    if(parameterCheck(argc-1, argv+1)){
        help(argv[0]);
        return 0;
    }
    int num = level_0(atoi(argv[1]), atoi(argv[2]), atoi(argv[3]), atoi(argv[4]));
    printf("total = %d\n", num);
    return 0;
}

运行测试

错误的参数测试
测试 12、2、9、10 以及运行时间
上面是普通测试,下面是网上搜索的,被称为超难的几组24点游戏题,看看求解效果:
(5、5、5、1)
(1、4、5、6)
(1、3、7、8)
(2、5、5、10)
(3、3、8、8)
(1、2、7、7)
(2、5、7、8)
(1、3、4、6)

如何去重问题
对于算式字符串字面值完全相同的算式非常容易去重,但是
有些算式字符串字面值不同而含义明显相同,例如:
((1+2)+3)x4 = 24(1+(2+3))x4 = 24含义重复;
((5+9)-6)x3 = 24((9-6)+5)x3 = 24含义重复;
((9-5)x3)x2 = 24((9-5)x2)x3 = 24含义重复等等。
留待以后想办法解决。

记于2019-04-15

上一篇下一篇

猜你喜欢

热点阅读