List all combinations of 4 operators and 4 numbers then test their final value C#
I'm trying to make a 24 solver (the game 24's objective is to use +,-,*,/ to get to the number 24)
I'm reasonably confident that the only way to do this is using a brute force method (try each combination of the numbers with the operators in between them开发者_运维百科.
To eliminate the need for parenthesis, I think the number placement should also be randomized.
I want to do this in C#.
How should I go about doing this? (basic outline, plan...)
Taken from: google codeplex
Algorithm
1. Think 24 game result as a binary tree(if you don't know what's it, check data structure book first).
2. There are 3 types of binary branches. "D OP D", "P OP D or D OP P", "P OP P". In which, D is digit(integer larger than 1), P is pointer to another downstream branch, and OP is operator "+,-,,/". Especially, "D OP D" must be in the leaf position of the tree.
3. Generate all the possible trees. And apply some optimization, like element at the two sides of "+," are switchable, while "-,/" are not.
4. Generate all the possible permutation of 4 digits (4! = 24). And remove the same copies.
5. Mix trees and digits permutations, do calculation, and we get the result!
精彩评论