TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Solving a Combination Lock Puzzle with JuMP and Julia

28 点作者 idunning超过 11 年前

5 条评论

psibi超过 11 年前
Haskell Solution:<p><pre><code> [(a,b,c,d,e,f) | a &lt;- [39, 90, 75, 15, 57], b &lt;- [9, 2, 58, 68, 48, 64], c &lt;- [91, 29, 55, 16, 67, 8], d &lt;- [22, 32, 25, 40, 54, 66], e &lt;- [41, 14, 30, 49, 01, 17], f &lt;- [44, 63, 10, 83, 46, 03], a+b+c+d+e+f == 419 ]</code></pre>
评论 #6428549 未加载
评论 #6425584 未加载
评论 #6426277 未加载
jamesk_au超过 11 年前
I also find it fascinating to read the algebraic solutions of others. This algorithm seems to rely on an open source integer problem solver called &quot;CBC&quot; [1]. But is it necessary for a general &#x27;searching solver&#x27; of that kind to be used for this particular problem?<p>The way in which the problem is framed suggests there is a unique solution. The model is presented as a square matrix subject to linear constraints. There are 36 unknowns, being x[1][1] through to x[36][36], each of which is 0 or 1. There is the constraint that the sum of the unknowns is 419. And there are two constraints to ensure that only one number is chosen from each row and each column, which together give rise to 36 constraint equations.<p>If any one of the latter constraints is replaced by the sum constraint, there is a linear system of 36 equations in 36 unknowns. Is it possible for that system to be constructed and solved directly through matrix inversion?<p>(For those trying the exercise at home, the number at P[1,2] appears as 90 but from the full problem statement I think it should be 6.)<p>[1]: <a href="https://projects.coin-or.org/Cbc" rel="nofollow">https:&#x2F;&#x2F;projects.coin-or.org&#x2F;Cbc</a>
评论 #6427049 未加载
nly超过 11 年前
Algebraic solutions are fascinating. I was hooked on Sage for a week or two solving similar problems. I solved this one using brute force though:<p><pre><code> array&lt;array&lt;int, 6&gt;, 6&gt; tumblers = {{{39, 90, 75, 88, 15, 57}, {9, 2, 58, 68, 48, 64}, {29, 55, 16, 67, 8, 91}, {40, 54, 66, 22, 32, 25}, {49, 1, 17, 41, 14, 30}, {44, 63, 10, 83, 46, 3}}}; template &lt;typename It&gt; bool solve (It tumbler, It end, int const sum) { if (tumbler == end) return (sum == 0); for (auto const pin: *tumbler) { if (solve (next(tumbler), end, sum - pin)) { std::cout &lt;&lt; pin &lt;&lt; std::endl; return true; } } return false; } int main() { solve (tumblers.rbegin(), tumblers.rend(), 419); }</code></pre>
comex超过 11 年前
As a note, this kind of problem is NP complete in the number of locks because the subset sum problem can be embedded into it.
tokipin超过 11 年前
not quite sure i understand the problem, but here&#x27;s one Mathematica approach:<p><pre><code> m = {{39, 90, 75, 88, 15, 57}, {9, 2, 58, 68, 48, 64}, {29, 55, 16, 67, 8, 91}, {40, 54, 66, 22, 32, 25}, {49, 1, 17, 41, 14, 30}, {44, 63, 10, 83, 46, 3}}; Select[ Extract[m, #] &amp; &#x2F;@ Transpose[{Range[6], #}] &amp; &#x2F;@ Tuples[Range[6], 6], Total[#] == 419 &amp;]</code></pre>