保护我的墙壁免受这些讨厌的门的侵害


20

门把手非常好,但是当您打开一扇门时,它总是会使周围的墙壁凹陷。我需要您输入房间的ASCII艺术作品,如下所示:

+---------+--X  --X    --+-----+
|       \     \   |\     |   \ |
|        \     \  | \    |    \|
|         X       |  \   |     X
|      /  |       |   \  X      
|     /   |     \       /       
|    /    |      \     /       |
+---X   --+-------X------+-----+

并输出带有门挡的房间,如下所示:

+---------+--X  --X    --+-----+
|       \  .  \   |\     |   \.|
|        \     \  | \   .|    \|
|         X       |  \   |     X
|      /  |       |.  \  X      
|     /  .|     \       /       
|.   /    |     .\     /       |
+---X   --+-------X------+-----+

规格:

  • ASCII码室(输入)将包括+-,和|。这些角色纯粹是化妆品。它们都可能是+s,但这看起来太可怕了。它还将包含铰链(X)和门(/\)。
  • 门由/或组成\。从“铰链”字符(即)开始X,它们将直接沿对角线(1英寸x和1英寸的变化y)移动2个或更多单位(字符)。
  • 要找到放置门挡的位置(每个门始终只有一个门挡),请找到该门的门道。门口将始终从一个铰链开始,并从那里向上,向下,向左或向右移动与门的长度相同的空间。在那之后的下一个空间将永远是一堵墙。例如,在此门中,门口标有Ds:

       \
        \
    ---DDX-----
    

    找到一扇门,找出要顺时针还是逆时针才能到达门。例如,在上面的示例门中,您必须顺时针旋转,而在这一扇门中,您必须逆时针旋转:

       \ <-
        \  )
    -----X  ---
    

    一旦知道要走的路,就继续走那条路(忽略门),直到碰到墙为止。

    这是上面示例门的可视化效果:

    可视化

    蓝色是门口,橙色是您必须顺时针旋转,红色是顺时针旋转,直到到达墙为止。

    到达墙壁后,从X墙壁上的铰链()移开(门的长度)空间,将墙壁向墙壁移开一个空间(这样就不会将门挡直接放在墙壁上),然后插入.那里。这是同样的示例门,显示了门挡的放置方式:

       \
        \  .
    ---DDX12---
    

    对每个门重复一次,然后输出结果!使用本文顶部的示例输入作为测试案例,以检查您的程序是否有效。

    请注意,您不必处理不适合墙壁的门,例如:

    |     /
    |    /
    |   /
    |  /
    +-X    --
    

    要么:

         /
        /
       /
    +-X   --
    |
    |
    
  • 这是,因此以字节为单位的最短代码将获胜。

门口有什么规则?它们必须正交,长度与门相同,并在一侧被一堵墙包围,而在另一侧被一扇铰链(用于右门)包围?
John Dvorak 2014年

@JanDvorak好,为澄清而编辑
Doorknob

3
我们是否可以假定以铰链为起点的墙的长度至少与门的长度相同,并且没有其他壁(不是以铰链为起点的墙)不会干扰该特定门?
2014年

@霍华德,我不确定你在说什么。您是在问是否可以假设与门口相对的墙的长度与门口的长度相同?如果是这样,则否,因为门只能像测试案例中的第二个一样旋转90度(通过从左上角开始的铰链放置来计算)。
门把手

1
??门是对角的。所有这些字符串都是6个字符宽,因此没有中间的列。
彼得·泰勒

Answers:


4

Scala,860字节

Golfed

    object D extends App{val s=args(0)split("\n")
    val r=Seq(P(1,0),P(1,-1),P(0,-1),P(-1,-1),P(-1,0),P(-1,1),P(0,1),P(1,1))
    var m=r(0)
    val e=s.map(_.toCharArray)
    case class P(x:Int,y:Int){def u=x==0||h
    def h=y==0
    def p(o:P)=P(x+o.x,y+o.y)
    def o="\\/".contains(c)
    def w="-|+".contains(c)
    def c=try s(y)(x) catch {case _=>'E'}
    def n=r.filter(!_.u).map(d => d.j(p(d))).sum
    def j(t:P):Int=if(t.o)1+j(p(t))else 0
    def q=if(c=='X'){m=this
    r.filter(_.u).map{d=>if(p(d).c==' '&&p(P(d.x*(n+1),d.y*(n+1))).w)d.i}}
    def i:Unit=Seq(r++r,(r++r).reverse).map(l=>l.drop(l.indexOf(this)+1)).map(_.take(4)).filter(_.exists(a=>a.p(m)o))(0).grouped(2).foreach{p=>if(p(1)p(m)w){p(0)add;return}}
    def add=if(r.filter(_.h).map(p(_)p(m)).exists(_.w))e(y*m.n+m.y)(x+m.x)='.'else e(y+m.y)(x*m.n+m.x)='.'}
    val f=args(0).size
    Array.tabulate(f,f){(i,j)=>P(i,j)q} 
    e.map(_.mkString).map(println)}

未打高尔夫球

    object DoorknobCleanVersion extends App {
            val s = args(0) split ("\n")

            val r = Seq(P(1, 0), P(1, -1), P(0, -1), P(-1, -1), P(-1, 0), P(-1, 1), P(0, 1), P(1, 1))
            val HorizontalDirections = r.filter(_.isHorizontal)

            var hinge = r(0)
            val result = s.map(_.toCharArray)

            type I = Int
            case class P(x: Int, y: Int) {
                    def isCardinal = x == 0 || isHorizontal
                    def isHorizontal = y == 0

                    override def toString = x + "," + y

                    def p(o: P) = P(x + o.x, y + o.y)

                    def isDoor = Seq('\\', '/').contains(charAt)
                    def isWall = Seq('-', '|', '+').contains(charAt)

                    def charAt = try s(y)(x) catch { case _ => 'E' }

                    def doorLength = r.filter(!_.isCardinal).map(d => d.recursion2(p(d))).sum

                    def recursion2(currentPosition: P): Int =
                            if (currentPosition.isDoor)
                                    1 + recursion2(p(currentPosition))
                            else
                                    0

                    def findDoorway =
                            if (charAt == 'X') {
                                    hinge = this
                                    r.filter(_.isCardinal).map { d =>
                                            if (p(d).charAt == ' ' && p(P(d.x * (doorLength + 1), d.y * (doorLength + 1))).isWall)
                                                    d.getCorrectRotation2
                                    }
                            }

                    def getCorrectRotation2: Unit = Seq(r ++ r, (r ++ r).reverse).map(l => l.drop(l.indexOf(this) + 1))
                            .map(_.take(4))
                            .filter(_.exists(a => a.p(hinge)isDoor))(0)
                            .grouped(2)
                            .foreach {
                                    p =>
                                            if (p(1) p (hinge)isWall) {
                                                    p(0)add;
                                                    return
                                            }
                            }

                    def add =
                            if (HorizontalDirections.map(p(_) p (hinge)).exists(_.isWall))
                                    result(y * hinge.doorLength + hinge.y)(x + hinge.x) = '.'
                            else
                                    result(y + hinge.y)(x * hinge.doorLength + hinge.x) = '.'

            }

            val size = args(0).size
            Array.tabulate(size, size) { (i, j) => P(i, j).findDoorway }

            result.map(_.mkString).map(println)
    }

事后看来,在这里使用OOP绝对是错误的方法。如果我能再做一次,那么我肯定会使用一堆经过硬编码的真值表。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.