Broken Calculator

て烟熏妆下的殇ゞ 提交于 2019-12-04 21:54:07

Giving some more context what vish4071 said in the comments.

Set up a graph in the following way: Starting the graph with a root, and than the new node are the number you're aloud to use (for the example this is 2 and 5). And build up the graph level by level. Make each level in the following way, a new node will consist either of adding number or a operator which you're aloud to use. After each operator there cannot be another operator.

If the node has a higher value than the Target value, than kill the node (target as end note), this only works for this example (if the operators are * and +). If you would be able to use the - and / operator this is not vallid.

Do this till you find the required value, and the level (+1, due to the = operation) is you're answer.

And example of the graph is given below

for your first example:
D=0    D=1    
       5
      /
Root /
     \
      \2


D=1    D=2   d=3   d=4
            --2
           / 
          /
        (*)___5  --> reaches answer but at level 6
        /   
       /     (*)___2  --> complete
      /     /   \  5
     /     /
  2 /____25_252    --> end node
    \     \
     \     \ 
      \     
       \    225    --> end node
        \  /
         22__222   --> end node
           \            
            (*)

This is slightly better than brute forcing, maybe there is a more optimal way.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!