首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

这次是关于24点的算法有关问题,本人只能想到穷举,又没其他思路能减少代码量

2013-07-16 
这次是关于24点的算法问题,本人只能想到穷举,又没其他思路能减少代码量24点算法,实现成程序,貌似有点难度

这次是关于24点的算法问题,本人只能想到穷举,又没其他思路能减少代码量
24点算法,实现成程序,貌似有点难度啊
本人只能先到穷举,但是代码量太大了,而且运算一组数要循环几千次。
我觉得一定会有相对简单的思路,能简化代码和运算速度,求各位大牛指教。 24点?算法?穷举
[解决办法]
穷举不是运算量的问题吗, 怎么成了代码量的问题了?
如果是那种给定 4 个数填符号的, 没有括号的话也就 4 的三次方 64 种情况, 运算量也不大呀.
[解决办法]
本质都是穷举,就看你怎么穷举。。。
[解决办法]
#include <fstream> 
#include <algorithm> 
#include <string> 
#include <sstream> 
#include <list> 
#include <cmath> 
#include <climits> 
#include <bitset> 
using namespace std; 

const char* INPUT_FILE  = "game.in"; 
const char* OUTPUT_FILE = "game.out"; 
const int NUMBER_COUNT  = 4; 
const int STATE_COUNT   = (1 << NUMBER_COUNT); 
const int MAX_NUMBER    = 100; 
const int MAX_EXPECTION = 1000; 
const int MAX_VALUE             = MAX_EXPECTION * MAX_NUMBER; 

struct Node { 
int value; 
int left, right;        
int leftvalue, rightvalue; 
char opr; 
}; 

typedef list<Node> NodeList; 

struct State { 
bitset<MAX_VALUE+10> exist; 
NodeList nodelist; 
}; 

int number[NUMBER_COUNT], expection; 
State state[STATE_COUNT]; 

void ReadData() 

ifstream fin(INPUT_FILE); 

for (int i = 0; i < NUMBER_COUNT; i++) { 
fin >> number[i]; 

fin >> expection; 


void Init() 

Node node ; 
for (int i = 0; i < NUMBER_COUNT; i++) { 


node.value              = number[i]; 
node.left = node.right = -1; 
state[(1 << i)].nodelist.push_back(node); 
state[(1 << i)].exist[node.value] = true; 



void Merge(int a, int b, int x) 
{       
Node node;      
NodeList::const_iterator i, j; 

for (i = state[a].nodelist.begin(); i != state[a].nodelist.end(); i++) { 
for (j = state[b].nodelist.begin(); j != state[b].nodelist.end(); j++) 
{                                      
node.value = (*i).value + (*j).value; 
node.left  = a; 
node.right = b;                 
node.leftvalue  = (*i).value; 
node.rightvalue = (*j).value; 
node.opr   = '+'; 
if ( (node.value <= MAX_VALUE) && (!state[x].exist[node.value]) ) { 
state[x].nodelist.push_back(node); 
state[x].exist[node.value] = true; 


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

double tmp = double((*i).value) * double((*j).value); 

if (tmp < INT_MAX) { 
node.value = (*i).value * (*j).value; 
node.left  = a; 
node.right = b; 
node.leftvalue  = (*i).value; 
node.rightvalue = (*j).value; 
node.opr   = '*'; 
if ( (node.value <= MAX_VALUE) && (!state[x]. 

exist[node.value]) ) 

state[x].nodelist.push_back(node); 
state[x].exist[node.value] = true; 



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

if ((*i).value >= (*j).value) { 
node.value = (*i).value - (*j).value; 
node.left  = a; 
node.right = b; 
node.leftvalue  = (*i).value; 


node.rightvalue = (*j).value; 
node.opr   = '-'; 
} else { 
node.value = (*j).value - (*i).value; 
node.left  = b; 
node.right = a; 
node.leftvalue  = (*j).value; 
node.rightvalue = (*i).value; 
node.opr   = '-'; 


if ( (node.value <= MAX_VALUE) && (!state[x].exist[node.value]) ) { 
state[x].nodelist.push_back(node); 
state[x].exist[node.value] = true; 


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


if ( ((*j).value != 0) && ((*i).value >= (*j).value) &&
((*i).value % (*j).value == 0) ) 

node.value = (*i).value / (*j).value; 
node.left  = a; 
node.right = b;         
node.leftvalue  = (*i).value; 
node.rightvalue = (*j).value; 
node.opr   = '/'; 
} else if ( ((*i).value != 0) && ((*j).value >= (*i). 

value) && 
((*j).value % (*i).value == 0) ) 

node.value = (*j).value / (*i).value; 
node.left  = b; 
node.right = a; 
node.leftvalue  = (*j).value; 
node.rightvalue = (*i).value; 
node.opr   = '/'; 


if ( (node.value <= MAX_VALUE) && (!state[x].exist[node.value]) ) 
{                               
state[x].nodelist.push_back(node); 
state[x].exist[node.value] = true; 
}                       
///////////////////////////////////////////////////// 





void Solve() 

Init(); 

for (int x = 2; x < STATE_COUNT; x++) { 
for (int i = 1; i < x; i++) {                   


if ( (x & i) == i ) { 
int j = x - i; 
if (i <= j) { 
Merge(i, j, x);         






void PrintExpression(ostream& out, Node node) 

if (node.left == -1) { 
out << node.value; 
} else { 
NodeList::const_iterator iter; 

out << "("; 

for (iter = state[node.left].nodelist.begin(); 
iter != state[node.left].nodelist.end(); 
iter++) 

if ((*iter).value == node.leftvalue) { 
PrintExpression(out, *iter); 
break; 



out << node.opr; 

for (iter = state[node.right].nodelist.begin(); 
iter != state[node.right].nodelist.end(); 
iter++) 

if ((*iter).value == node.rightvalue) { 
PrintExpression(out, *iter); 
break; 



out << ")"; 
}               


void Output() 

ofstream fout(OUTPUT_FILE); 

int bestValue = -INT_MAX;       
NodeList::const_iterator iter, bestIter; 

NodeList& nodelist = state[STATE_COUNT-1].nodelist; 

for (iter = nodelist.begin(); iter != nodelist.end(); iter++) 
{       
if ( ((*iter).value <= expection) && (bestValue < (*iter).value) ) { 
bestValue = (*iter).value; 
bestIter  = iter; 

}       
fout << bestValue << endl; 
PrintExpression(fout, *bestIter ); 
fout << endl; 


int main() 

ReadData(); 
Solve(); 
Output(); 
return 0; 

[解决办法]

热点排行