您的位置:首页 > 其它

24点游戏算法

2016-04-18 17:08 288 查看
问题描述:给出4个1-10的数字,通过加减乘除,得到数字为24就算胜利

输入:
4个1-10的数字。[数字允许重复,测试用例保证无异常数字]
输出:(或者输出求得24点的表达式)
True or False
问题分析:
24点游戏作为扑克牌中最经典的游戏,也是平时经常玩的益智游戏。对于此题主要是用穷举的思想,再加上一点点实行的编程技巧,令代码更简洁明晰。本代码主要是利用符号的不断变化,再加上4个计算数值的不同排列组合,最终求出24点。默认是4个数字a,b,c,d是按顺序计算的,即默认是加了括号的,即(((a op1 b)op2 c)op3 d)。而4个数字要组合顺序有A(4,4)=24种,两种数字之间的运算符号也有四种,故有很多种组合。
本编程在实现的过程中主要是用三层循环来实现4个数字之间的不同运算符号,而4个数字之间的排列用到了两种方法:一种为穷举出24中排列,另外一种是用到了C++中next_permutation的算法。
//24点游戏算法,主要使用的枚举法
//date:2016.4.18
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
static char opt[]={'+','-','*','/'};

int Cal(int a,int b,int c,int d,int i,int j,int k) //将多元运算转换为二元运算
{
int sum=0;
if(i==0)sum=a+b;
else if(i==1)sum=a-b;
else if(i==2)sum=a*b;
else if(i==3)sum=a/b;

if(j==0)sum=sum+c;
else if(j==1)sum=sum-c;
else if(j==2)sum=sum*c;
else if(j==3)sum=sum/c;

if(k==0)sum=sum+d;
else if(k==1)sum=sum-d;
else if(k==2)sum=sum*d;
else if(k==3)sum=sum/d;
return sum;
}

bool Game24Points1(int a, int b, int c, int d)
{
//TODO: Add codes here ...
int i,j,k;
for(i=0;i<4;i++)
for(j=0;j<4;j++)
for(k=0;k<4;k++)
if(Cal(a,b,c,d,i,j,k)==24) return true;  //方法一:给出24种排列即可
if(Cal(a,b,d,c,i,j,k)==24) return true;
if(Cal(a,c,b,d,i,j,k)==24) return true;
if(Cal(a,c,d,b,i,j,k)==24) return true;
if(Cal(a,d,b,c,i,j,k)==24) return true;
if(Cal(a,d,c,b,i,j,k)==24) return true;

if(Cal(b,a,c,d,i,j,k)==24) return true;
if(Cal(b,a,d,c,i,j,k)==24) return true;
if(Cal(b,c,a,d,i,j,k)==24) return true;
if(Cal(b,c,d,a,i,j,k)==24) return true;
if(Cal(b,d,a,c,i,j,k)==24) return true;
if(Cal(b,d,c,a,i,j,k)==24) return true;

if(Cal(c,a,b,d,i,j,k)==24) return true;
if(Cal(c,a,d,b,i,j,k)==24) return true;
if(Cal(c,b,a,d,i,j,k)==24) return true;
if(Cal(c,b,d,a,i,j,k)==24) return true;
if(Cal(c,d,a,b,i,j,k)==24) return true;
if(Cal(c,d,b,a,i,j,k)==24) return true;

if(Cal(d,a,b,c,i,j,k)==24) return true;
if(Cal(d,a,c,b,i,j,k)==24) return true;
if(Cal(d,b,a,c,i,j,k)==24) return true;
if(Cal(d,b,c,a,i,j,k)==24) return true;
if(Cal(d,c,a,b,i,j,k)==24) return true;
if(Cal(d,c,b,a,i,j,k)==24) return true;
return false;
}

bool Game24Points(int a, int b, int c, int d) //方法二:利用C++ next_premutation函数实现全排列,并对具体的求解过程进行了输出
{
//TODO: Add codes here ...
int i,j,k;
int source[4]={a,b,c,d};
vector<int> vec(source,source+4);
for(i=0;i<4;i++)
for(j=0;j<4;j++)
for(k=0;k<4;k++)
if(Cal(a,b,c,d,i,j,k)==24)
{
cout<<"("<<"("<<vec[0]<<opt[i]<<vec[1]<<")"<<opt[j]<<vec[2]<<")"<<opt[k]<<vec[3]<<endl;
return true;
}
sort(vec.begin(),vec.end());
while(next_permutation(vec.begin(),vec.end()))
{
for(i=0;i<4;i++)
for(j=0;j<4;j++)
for(k=0;k<4;k++)
if(Cal(vec[0],vec[1],vec[2],vec[3],i,j,k)==24)
{
cout<<"("<<"("<<vec[0]<<opt[i]<<vec[1]<<")"<<opt[j]<<vec[2]<<")"<<opt[k]<<vec[3]<<endl;
return true;
}
}
return false;
}
void main()
{
int a,b,c,d;
m_ret:  //做标记
cout<<"请输入1-10范围内四个数:"<<endl;
cin>>a>>b>>c>>d;
if ((a<1)||(a>10)||(b<1)||(b>10)||(c<1)||(c>10)||(d<1)||(d>10))
goto m_ret;  //返回标记,重新输入

if(Game24Points(a,b,c,d)==true) cout<<"True"<<endl;
else cout<<"False"<<endl;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息