货架内放置代码(二)
货架内放置代码(二)
上周小编为大家介绍了货架内放置代码,但是它依旧存在一定的问题,便是可能会存在重叠的部分,将很多个货物放置在所谓的同一个位置,认定为同一个顺序,因为之前我们仅仅是因为容量来确定实体的位置,在模型运行时间长了之后,实体有进有出,就会造成堆叠的情况,就像下面这样。
Last week, the editor introduced the code placed on the shelf, but it still has certain problems, that is, there may be overlapping parts. Many goods are placed in the so-called same position, which is regarded as the same order, because before We only determine the location of the entity based on the capacity. After the model runs for a long time, the entity will enter and exit, which will cause the stacking situation, as shown below.
所以为了避免这样的情况发生,我们就需要记录实体放置的位置,当新的实体进入的时候,放在货格的位置需要进行循环的比对货格内已有货物的位置记录,直到找到所缺的顺序,并放入其中。因此需要进行代码的优化。
Therefore, in order to avoid such a situation, we need to record the location of the entity. When a new entity enters, the location of the cargo compartment needs to be cyclically compared with the location record of the existing goods in the cargo compartment until the location is found. The missing sequence and put it into it. Therefore, code optimization is required.
首先我们需要有一个代表需放置的实体的顺序值,记录为flag,以及位置的初始标签值,然后可以利用while语句,将flag与当前货格容量num进行比较,获取当前货物的目标位置,具体代码如下所示
First, we need to have an order value representing the entity to be placed, recorded as the flag, and the initial label value of the position, and then we can use the while statement to compare the flag with the current cargo compartment capacity num to obtain the target position of the current cargo. The code is as follows
这样就可以避免上面所说到的问题,我们一起来看一看模型运行的结果吧!
In this way, the problems mentioned above can be avoided. Let"s take a look at the results of the model running!
参考资料:腾讯课堂《从小白到大神》
英文翻译:Google翻译
本文由LearningYard学苑原创,部分文字来源于网络,若有侵权请联系删除。