如何用PHP编写一个算法,在以下约束条件下将对象移动到正确的位置
slot-1 slot-2 slot-3 slot-4 slot-5
obj-4 obj-5 Empty obj-3 obj-1任何想法都是有帮助的,这样我就可以自己尝试了。
发布于 2015-08-27 07:35:50
简单地说:
代码编辑:
variable boolean1 = False
Execute for-loop so many times until boolean1 is True
for x in arrayslots: (1st for-loop)
if x is empty:
get x.slot_number # Step 1
for y in objects:
if slot_number==y_object_number:
transfer object # Step 2
break 1st for-loop #(and start anew -> Step 3)
if x == empty # no obj. could be found for the slot
for x in objects:
if x is NOT in the right slot:
transfer x to empty slot
break the 1st for--loop #(and start anew -> Step 3)
boolean1 = True #You only reach this when the loop finds that no objects are in the wrong place anymore.https://stackoverflow.com/questions/32243130
复制相似问题