OJ 系列之24點遊戲算法

1、問題描述

這裏寫圖片描述

2、解題思路

沒找到好的辦法,採用窮舉法。所謂窮舉法就是列出4個數字加減乘除的各種可能性。我們可以將表達式分成以下幾種:首先我們將4個數設爲a,b,c,d,,將其排序列出四個數的所有排序序列組合(共有A44=24種組合)。再進行符號的排列表達式,其中算術符號有加、減、乘、除。其中有效的表達式有:

if(a+b+c+d==24)
        return 1;
    else if(a+b+c-d==24)
        return 1;
    else if((a+b)*(c+d)==24)
        return 1;
    else if((a-b)*(c+d)==24)
        return 1;
    else if((a-b)*(c-d)==24)
        return 1;
    else if((a+b+c)*d==24)
        return 1;
    else if((a+b-c)*d==24)
        return 1;
    else if((a-b-c)*d==24)
        return 1;
    else if((a*b*c)/d==24)
        return 1;
    else if((a*b)*(c-d)==24)
        return 1;
    else if((a*b)*c-d==24)
        return 1;
    else if((a*b)*c+d==24)
        return 1;
    else if(a*b*c*d==24)
        return 1;
    else if((a+b)*(c/d)==24)
        return 1;
    else if((a+b)+(c/d)==24)
        return 1;
    else if((a*b)+c+d==24)
        return 1;
    else if((a*b)+c-d==24)
        return 1;
    else if((a*b)-(c/d)==24)
        return 1;
    else if((a*b)+(c/d)==24)
        return 1;
    else if((a*b)-c-d==24)
        return 1;
    else if((a*b)+(c*d)==24)
        return 1;
    else if((a*b)-(c*d)==24)
        return 1;
    else if((a*b)/(c*d)==24)
        return 1;
    else if((a*b)/(c+d)==24)
        return 1;
    else if(c!=d)
        if((a*b)/(c-d)==24)
            return 1;

首先列出所有有效的表達式,其中a,b,c,d的範圍是1到10。下面我介紹下窮舉法的主要實現,我們知道要實現24點的算法,就是通過4個數字,4個運算符號和2對括號(最多爲2對),通過各種組合判斷其結果是否爲24。我們用a,b,c,d代替4個數字。通過變換其不同的排列組合,依次判斷其結果是否爲24。

3、代碼實現

include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

int check(int a,int b,int c,int d)
{
    if(a+b+c+d==24)
        return 1;
    else if(a+b+c-d==24)
        return 1;
    else if((a+b)*(c+d)==24)
        return 1;
    else if((a-b)*(c+d)==24)
        return 1;
    else if((a-b)*(c-d)==24)
        return 1;
    else if((a+b+c)*d==24)
        return 1;
    else if((a+b-c)*d==24)
        return 1;
    else if((a-b-c)*d==24)
        return 1;
    else if((a*b*c)/d==24)
        return 1;
    else if((a*b)*(c-d)==24)
        return 1;
    else if((a*b)*c-d==24)
        return 1;
    else if((a*b)*c+d==24)
        return 1;
    else if(a*b*c*d==24)
        return 1;
    else if((a+b)*(c/d)==24)
        return 1;
    else if((a+b)+(c/d)==24)
        return 1;
    else if((a*b)+c+d==24)
        return 1;
    else if((a*b)+c-d==24)
        return 1;
    else if((a*b)-(c/d)==24)
        return 1;
    else if((a*b)+(c/d)==24)
        return 1;
    else if((a*b)-c-d==24)
        return 1;
    else if((a*b)+(c*d)==24)
        return 1;
    else if((a*b)-(c*d)==24)
        return 1;
    else if((a*b)/(c*d)==24)
        return 1;
    else if((a*b)/(c+d)==24)
        return 1;
    else if(c!=d)
        if((a*b)/(c-d)==24)
            return 1;
    else
        return 0;
    return 0;

}

bool Game24Points(int a, int b, int c, int d)
{
    //TODO: Add codes here ...
    if(a<=0||a>10||b<=0||b>10||c<=0||c>10||d<=0||d>10)
        return false;

    int source[4];
    source[0]=a;
    source[1]=b;
    source[2]=c;
    source[3]=d;

    vector<int> sort1(source,source+4);
    if(check(sort1[0],sort1[1],sort1[2],sort1[3])==1) {
          return true;
    }

    sort(sort1.begin(),sort1.end());
    /*next_permutation 重新全排列*/
     while (next_permutation(sort1.begin(), sort1.end())) {
        if(check(sort1[0],sort1[1],sort1[2],sort1[3])==1) {
          return true;
        }
    //  cout<<sort1[0]<<" "<<sort1[1]<<" "<<sort1[2]<<" "<<sort1[3]<<endl;

    }
    return false;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章