English 中文(简体)
编程两列火车在没有位置数据或通信的情况下交叉(逻辑谜题)[关闭]
原标题:Programming two trains to intersect without positional data or communication (logic puzzle) [closed]
Closed. This question is off-topic. It is not currently accepting answers.

想改进这个问题吗 更新问题,使其成为堆栈溢出的主题。

Closed 12 years ago.

一架直升机将两列火车降落在一条笔直的无限铁路线上,每列火车都用降落伞固定。

两列火车之间有一段不确定的距离。

每列火车都面向同一个方向,着陆时,连接在每列火车上的降落伞落在火车旁边的地面上并分离。

每列火车都有一个控制其运动的微芯片。芯片完全相同。

火车没有办法知道它们在哪里。

你需要在芯片中写入代码,使列车相互碰撞。

每一行代码都需要一个时钟周期来执行。

您可以使用以下命令(并且只能使用这些命令):

  • MF - moves the train forward
  • MB - moves the train backward
  • IF (P) - conditional that is satisfied if the train is next to a parachute. There is no "then" to this IF statement.
  • GOTO
问题回答

让每列火车缓慢前行,直到找到降落伞。当后列车发现前列车的降落伞时,使其更快地向前移动以赶上前列车。

1.  MF
2.  IF(P)
3.    GOTO 5
4.  GOTO 1
5.  MF
6.  GOTO 5

如果你想以额外的几行代码为代价,减少列车相互到达的时间,你可以展开第二个环路。

label1: MF
If (P)
{
   // Do nothing (because of no then?)
}
ELSE
{
   MF;
   MB;
   GOTO label1;
}
label 2:MF
GOTO label2;

GO forward 2 times, backward 1 times until meet the other train s parachute go forward like crazy (to bump into the other - it s still Forward then backward - meaning it s go slower). I use MF one time in label 2, mean it take 2 clock cycle to go one step forward. In label1 it took 5 clock cycle to go forward one steps. So if we use more MF in label2 two of them will bump into eachother faster.
No variable used.





相关问题
How to add/merge several Big O s into one

If I have an algorithm which is comprised of (let s say) three sub-algorithms, all with different O() characteristics, e.g.: algorithm A: O(n) algorithm B: O(log(n)) algorithm C: O(n log(n)) How do ...

Grokking Timsort

There s a (relatively) new sort on the block called Timsort. It s been used as Python s list.sort, and is now going to be the new Array.sort in Java 7. There s some documentation and a tiny Wikipedia ...

Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Enumerating All Minimal Directed Cycles Of A Directed Graph

I have a directed graph and my problem is to enumerate all the minimal (cycles that cannot be constructed as the union of other cycles) directed cycles of this graph. This is different from what the ...

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签