Last active
August 29, 2015 14:04
-
-
Save rangercyh/43b60594cfd1247cb141 to your computer and use it in GitHub Desktop.
lua code to solve maze 24 and any target though + - * /. Original source:http://blog.henix.info/dig/reverse-polish-notation-catalan-number/_.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| --[[ | |
| 24点解法:4个数字的逆波兰式形式只有以下5种。1代表数字,+代表运算符,括号隐藏在逆波兰式的形式中 | |
| 1111+++ | |
| 111+1++ | |
| 111++1+ | |
| 11+11++ | |
| 11+1+1+ | |
| ]] | |
| local target = 24 | |
| local num4 = { 8, 4, 2, 24 } | |
| local op = { '+', '-', '*', '/' } | |
| --计算一种逆波兰式的值 | |
| function evalPo(e) | |
| local s = {} --临时存储的栈 | |
| local str = {} --正常中缀表达式 | |
| local mask = {} --str中各项表达式的符号 | |
| local a, b, re, s1, s2, m1, m2 | |
| for i = 1, #e do | |
| if type(e[i]) == "number" then | |
| table.insert(s, e[i]) | |
| if e[i] < 0 then | |
| table.insert(str, "("..e[i]..")") | |
| else | |
| table.insert(str, e[i]) | |
| end | |
| table.insert(mask, 1) | |
| elseif type(e[i]) == "string" then | |
| if #s < 2 then | |
| error("Po statement wrong!") | |
| end | |
| b = table.remove(s) | |
| a = table.remove(s) | |
| s2 = table.remove(str) | |
| s1 = table.remove(str) | |
| m2 = table.remove(mask) | |
| m1 = table.remove(mask) | |
| if e[i] == '+' then | |
| re = a + b | |
| table.insert(mask, 0) | |
| elseif e[i] == '-' then | |
| re = a - b | |
| table.insert(mask, 0) | |
| elseif e[i] == '*' then | |
| re = a * b | |
| table.insert(mask, 1) | |
| elseif e[i] == '/' then | |
| if b == 0 then | |
| --除0的直接返回-1吧 | |
| return -1 | |
| end | |
| re = a / b | |
| table.insert(mask, 1) | |
| else | |
| error("Po wrong op: "..e[i]) | |
| end | |
| if mask[#mask] == 1 then | |
| if m1 == 0 then | |
| s1 = '('..s1..')' | |
| end | |
| if m2 == 0 then | |
| s2 = '('..s2..')' | |
| end | |
| end | |
| table.insert(str, s1..e[i]..s2) | |
| table.insert(s, re) | |
| end | |
| end | |
| if #s ~= 1 then | |
| error("Po statement not enough op!") | |
| end | |
| return s[1], str[1] | |
| end | |
| --local test = { 5, 3, 14, 2, '/', '+', '*' } | |
| --print(evalPo(test)) | |
| local exp = {} | |
| local pos = { | |
| [1] = { 1, 2, 3, 4, 5, 6, 7 }, --1111+++ | |
| [2] = { 1, 2, 3, 5, 4, 6, 7 }, --111+1++ | |
| [3] = { 1, 2, 3, 6, 4, 5, 7 }, --111++1+ | |
| [4] = { 1, 2, 4, 5, 3, 6, 7 }, --11+11++ | |
| [5] = { 1, 2, 4, 6, 3, 5, 7 }, --11+1+1+ | |
| } | |
| for a = 1, 4 do | |
| for b = 1, 4 do | |
| if b ~= a then | |
| for c = 1, 4 do | |
| if c ~= a and c ~= b then | |
| for d = 1, 4 do | |
| if d ~= a and d ~= b and d ~= c then | |
| for op1 = 1, 4 do | |
| for op2 = 1, 4 do | |
| for op3 = 1, 4 do | |
| for model = 1, 5 do | |
| exp[pos[model][1]] = num4[a] | |
| exp[pos[model][2]] = num4[b] | |
| exp[pos[model][3]] = num4[c] | |
| exp[pos[model][4]] = num4[d] | |
| exp[pos[model][5]] = op[op1] | |
| exp[pos[model][6]] = op[op2] | |
| exp[pos[model][7]] = op[op3] | |
| local value, str = evalPo(exp) | |
| if value == target then | |
| io.write(str.."="..value, '\n') | |
| os.exit() | |
| end | |
| end | |
| end | |
| end | |
| end | |
| end | |
| end | |
| end | |
| end | |
| end | |
| end | |
| end | |
| print("maze can't solve!") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment