Description
You've designed a computer and implemented all the common arithmetic operators: addition, subtraction, multiplication and integer division. However, your budget was very limited, so you could only afford to place a single register in the computer. The register can store any non-negative integer value. Since there is only one register, there is no need to identify the store location or the operands of each operation or its result. The programming language has four instructions: '+', '-', '*' and '/'. Each instruction performs the corresponding operation using the value in the register as both its parameters. It then stores the result in the same register, overwriting the previous content.
A program for your computer is a sequential list of zero or more instructions. You want to show that, even with its limitations, your newly constructed computer is powerful. You will be given two ints s and t. Return the shortest program that finishes with a value of t in the register if it contained s before executing. If there is more than one possible answer, return the one that comes earliest lexicographically. If there is no program that can do the job, return ":-(" (quotes for clarity) instead.
Input
There are at most 100 cases,each case one line contain 2 space seperated integers S and T. (1 <= S,T <= 10^9)
Output
Output the shortest program that finishes with a value of t in the register if it contained s before executing. If there is more than one possible answer, return the one that comes earliest lexicographically. If there is no program that can do the job, return ":-(" (quotes for clarity) instead.
Sample Input
7 3927 2564 2567 9
Sample Output
+*+/+*****:-(
//--------------------------------------------------------------------------------------
簡單廣搜題,卻被我做的很悲劇,無限WA。今天可恥地去看了測試資料才發現錯在哪裡。
//----------------------------------------
起初思路是:
1如果目標數可以拆成起始數與2的積就用*、+向上廣搜
2如果不能但目標數因數只有2,先除成1後再向上廣搜
3以上不符合或者搜不到就輸出:-(
錯誤:以為能直接向上搜到的話,直接向上搜肯定比除成1後再向上搜要快,其實不是。
比如,起始數為2^n,目標數為2^(2n-1),起始數要不斷的加才能上去,但除成1後可以用乘,當n比較大時,後者會較快到達目標。
//----------------------------------------
於是改了下,不去分情況,直接廣搜。
因為除法只可能在第一次用,所以把起始數插進隊列後,把1和除號也插進隊列(腦子果斷進水),依然WA。
錯誤:假設兩種方法以同樣的步數到目標,一個/開頭,一個*開頭,按字典序應該輸出後者,但由於/是先插進去的,反而會輸出前者。
//----------------------------------------
最後還是回到最老老實實的廣搜,把起始數插入隊列,先插乘再插加最後除沒插過就插下除,唉~~~
//----------------------------------------
每次陷進一個錯誤出來後都會很混亂,而且沒有從頭理下頭緒的習慣,只是在原來的基礎上改下去。這種錯誤跟著代碼走一遍就能發現的,下次要冷靜點了,唉~~~