首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >自动驾驶仪模式

自动驾驶仪模式
EN

Code Golf用户
提问于 2016-01-23 15:16:53
回答 1查看 295关注 0票数 10

从左上角开始的直升机正向地面下降(为了这个问题,在2D空间中)。它有自动驾驶模式和手动模式。

自动驾驶仪模式表现如下:

  • 如果下面的空间是空的,请下降到它。
  • 否则向左或向右移动一步,完全是随机的。(它可以以这种方式移动多个步骤。)

它不停地重复这两个步骤,直到它落到地面。手动模式更聪明,即使这需要向上移动或一些熟练的操纵,也会找到通往地面的最佳路径。

你的工作是确定

  1. 自动驾驶仪会在给定的情况下通过,
  2. 在给定的情况下自动驾驶仪可能会失败,
  3. 自动驾驶仪将失败,但手动模式将通过,或
  4. 这两种模式都将失败(没有通往地面的有效路径)。

输入

  • 给定场景为1d或2d非空数组,使用两个不同的字符来表示空闲和阻塞的空间。标点符号可选。
  • 可选:数组的尺寸

输出

四个预定义字符中的一个,指示发生了哪一种情况。

样本数据

在输入中使用0(空)和1(阻塞),在输出中使用1 2 3 4(如上面所示)

代码语言:javascript
复制
0 0 0 0
0 1 0 0
0 0 0 1
1 1 0 0

输出:1

代码语言:javascript
复制
0 0 1 0
1 0 0 1
0 0 0 0
0 1 1 0
0 0 0 1

输出:2 (直升机将在第四行遇到1,如果在自动驾驶模式下,它可能会在第5行的末尾设置陷阱)

代码语言:javascript
复制
0 0 0 1 0
0 1 1 0 0
0 1 0 0 0
0 0 0 1 0
1 1 1 1 0

输出:3 (这需要向上移动,因此自动驾驶仪失败)

代码语言:javascript
复制
1 0 0
0 0 0

输出:4

代码语言:javascript
复制
0 0 0 0 1
1 1 1 0 0
1 0 0 1 0
0 1 0 0 0
0 0 1 1 1

输出:4

EN

回答 1

Code Golf用户

发布于 2016-02-11 06:07:56

Ruby,259

我玩得很开心。谢谢!网格的挑战往往是极好的乐趣和有趣的挑战。这假设问题中的“字符”可以是整数。

我认为这里的主要改进之处是:

  1. r的创建
  2. 第三行的可怕的三重滥用职权很可能会变成一种更可怕的,但更简单的东西。
代码语言:javascript
复制
->a,h,w{f=->l,s=0,y=[0]{r=w-2<s%w ?w:1,1>s%w ?w:-1,w
v=(l ?r+[-w]:a[s+w]==0?[w]:r).map{|d|a[m=s+d]==0&&!y[m]?m:p}-q=[p]
a[s]>0?q:s/w>h-2?8:v[0]?v.map{|o|f[l,y[o]=o,y]}.flatten-q :r.any?{|i|a[s+i]<1}?p: !0}
g=f[p]
[8,g[0]&&g.all?,g.any?,f[8].any?,!p].index !p}

不打高尔夫球(有点过时,但非常接近):

代码语言:javascript
复制
# a is a one-dimensional array of 0s and 1s, h is height, w is width
->a,h,w{
  # f recursively walks the array and returns true/false/nil for each path.
  #    True means we can reach ground.
  #    False means we are stuck in a local minimum and cannot escape
  #    Nil means we reached a local dead-end and need to backtrack.
  # l: {true=>"manual", false=>"autopilot"}
  # s: the position index
  # y: an array of booleans - true-ish means we've visited that square before
  #         (this is to prevent infinite loops, esp in manual mode)
  f=->l,s=0,y=[0]{
    # d: all the legal deltas from s (maximally, that's [1,-1,w,-w], aka [LRDU])
    # r: but the right and left get tricky on the edges, so let's pre-calculate those
    #    we'll default to "down" if "left" or "right" are illegal
    r=[w-2<s%w ?w:1,1>s%w ?w:-1]
    # if manual, [LRDU]; if auto, and you can go down, [D]. else, [LR] 
    d=l ?r+[w,-w]:a[s+w]==0?[w]:r
    # v: the legal deltas that you can go to from s (that is, unvisited and unblocked)
    v=d.map{|d|a[m=s+d]==0&&!y[m]?m:p}-[p]


    a[s]>0 ? [p]     # if we're currently blocked, return [nil] (this is only the case when a[0]==1)
      : s/w>h-2 ? !p # if we're at the bottom, return true
        : v[0] ?     # if we have a place to go....
        v.map{|o|f[l,y[o]=o,y]}.flatten-[p] # recurse with each step.
                                            # y[o]=o is a neat trick to make y[o] truthy and return o
          : r.any?{|i|a[s+i]==0} ? # otherwise, we have nowhere to go. If we could visit left/right, but have already been there
            p                      #    this is not a dead-end - return nil to remove this path
            : !!p                  # If we have a true dead-end (auto-mode with a local minimum), false
  }
  # g is the auto flight
  g=f[p]
  # finally, choose the first "true" out of:
  # 0: always 8.  Cuz why not 8?
  # 1: there is at least one auto path, and all are truthy
  # 2: any auto path is truthy
  # 3: any manual path is truthy
  # 4: true
  [8,g[0]&&g.all?,g.any?,f[!p].any?,!p].index !p
}
票数 1
EN
页面原文内容由Code Golf提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codegolf.stackexchange.com/questions/69929

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档