盒子的高度图


22

看一下这张各种盒子的ascii艺术图:

+--------------------------------------------------------------+
|                                                              |
|   +-------------------------------+          +-------+       |
|   |                               |          |       |       |
|   |                               |          |       |       |
|   |     +----------------+        |          |       |       |
|   |     |                |        |          +-------+       |
|   |     |                |        |                          |
|   |     |                |        |          +-------+       |
|   |     +----------------+        |          |       |       |
|   |                               |          |       |       |
|   |                               |          |       |       |
|   +-------------------------------+          +-------+       |
|                                                              |
+--------------------------------------------------------------+

每个框的垂直部分(|)均用竖线表示,水平部分(-)则用短划线表示,拐角处用加号(+)表示。

该图还显示了其他框内的框。我们称该盒子的图层中包含一个盒子的盒子数。再次显示该图,其中每个框的层都带有注释:

+--------------------------------------------------------------+
|                                                              |
|   +-------------------------------+          +-------+       |
|   |                               |          |       |       |
|   |                               |          |   1   |       |
|   |     +----------------+        |          |       |       |
|   |     |                |        |    0     +-------+       |
|   |     |        2       |   1    |                          |
|   |     |                |        |          +-------+       |
|   |     +----------------+        |          |       |       |
|   |                               |          |   1   |       |
|   |                               |          |       |       |
|   +-------------------------------+          +-------+       |
|                                                              |
+--------------------------------------------------------------+

您的程序将以类似于顶部的方框图形式作为输入。作为输出,您的程序应输出带有以下内容的框图:

  • 第0层上的框应填充字符#(注意:第0层上将永远只有一个框);
  • 第1层的框应填充字符=;
  • 第2层的框应填充字符-;
  • 第3层的框应填充字符.;
  • 请勿填充第4层及以上的框。

示例输入的输出如下所示:

+--------------------------------------------------------------+
|##############################################################|
|###+-------------------------------+##########+-------+#######|
|###|===============================|##########|=======|#######|
|###|===============================|##########|=======|#######|
|###|=====+----------------+========|##########|=======|#######|
|###|=====|----------------|========|##########+-------+#######|
|###|=====|----------------|========|##########################|
|###|=====|----------------|========|##########+-------+#######|
|###|=====+----------------+========|##########|=======|#######|
|###|===============================|##########|=======|#######|
|###|===============================|##########|=======|#######|
|###+-------------------------------+##########+-------+#######|
|##############################################################|
+--------------------------------------------------------------+

这是另一个显示第3、4和5层的输入和输出。请注意,顶部的水平线非常靠近。在这些情况下,没有足够的空间来填充任何字符。

+-----------------------------------------------------------------------+
|     +--------------------------------------------------------------+  |
|     |      +-----------------------------------------------------+ |  |
|     |      |         +-----------------------------------------+ | |  |
|     |      |         |           +---------------------------+ | | |  |
|     |      |         |           |         +-------------+   | | | |  |
|     |      |         |           |         |             |   | | | |  |
|     |      |         |           |         +-------------+   | | | |  |
|     |      |         |           +---------------------------+ | | |  |
|     |      |         |                                         | | |  |
|     |      |         +-----------------------------------------+ | |  |
|     |      |                                                     | |  |
|     |      |                                                     | |  |
|     |      +-----------------------------------------------------+ |  |
|     |                                                              |  |
|     +--------------------------------------------------------------+  |
|                                                                       |
|                                                                       |
|                                                                       |
+-----------------------------------------------------------------------+

输出:

+-----------------------------------------------------------------------+
|#####+--------------------------------------------------------------+##|
|#####|======+-----------------------------------------------------+=|##|
|#####|======|---------+-----------------------------------------+-|=|##|
|#####|======|---------|...........+---------------------------+.|-|=|##|
|#####|======|---------|...........|         +-------------+   |.|-|=|##|
|#####|======|---------|...........|         |             |   |.|-|=|##|
|#####|======|---------|...........|         +-------------+   |.|-|=|##|
|#####|======|---------|...........+---------------------------+.|-|=|##|
|#####|======|---------|.........................................|-|=|##|
|#####|======|---------+-----------------------------------------+-|=|##|
|#####|======|-----------------------------------------------------|=|##|
|#####|======|-----------------------------------------------------|=|##|
|#####|======+-----------------------------------------------------+=|##|
|#####|==============================================================|##|
|#####+--------------------------------------------------------------+##|
|#######################################################################|
|#######################################################################|
|#######################################################################|
+-----------------------------------------------------------------------+

另一个输入,这次的垂直线也紧靠在一起:

+-------------+
|+-----------+|
||           ||
||           ||
||           ||
|+-----------+|
+-------------+

输出:

+-------------+
|+-----------+|
||===========||
||===========||
||===========||
|+-----------+|
+-------------+

附加条款

  • 最外面的盒子周围可能有空白。
  • 框的内部宽度或高度不能为0(因此框内始终会有一些空间)
  • 同一层上的盒子可以互相接触。

Answers:


3

红宝石163 164

w=l=-1
x=$<.map{|l|w=l.size;l}.join
b=[]
x.size.times{|i|c=x[i]
x[i..i+1]=='+-'&&(x[i+w]!=?|?b-=[i%w]:b<<i%w)
c>?z&&l+=b&[i%w]!=[]?1:-1
$><<(c==' '&&'#=-.'[l]||c)}

在线尝试:测试用例#1测试用例#2

非高尔夫节目:

  # read all lines from STDIN
  input = $<.map{|l|l}.join
  width = input.index(?\n)+1

  box_left_margins = []
  current_layer = -1

  input.size.times{|i|
    c = input[i]

    if c == ?+ && input[i+1] == ?-
      #we're at a box's left margin
      if input[i+width] == ?|
        # we're at the box's top - mark this index as a left margin
        box_left_margins << i%width
      else
        # we're at the box's bottom - this index is no longer a left margin
        box_left_margins-=[i%width]
      end
    end

    if c == ?|
      if box_left_margins.include? (i%width)
        current_layer += 1
      else
        current_layer -= 1
      end
    end

    if c == ' '
      $><< ('#=-.'[current_layer]||' ')
    else
      $><<c
    end
  }

2

Java,476466字节

import java.util.*;class H{public static void main(String[]a){Scanner p=new Scanner(System.in);char[]l=p.nextLine().toCharArray(),d={'#','=','-','.'};int s=l.length,b,i;int[]m=new int[s];String o=new String(l);for(;;){o+='\n';l=p.nextLine().toCharArray();if(l[0]=='+')break;o+='|';b=0;for(i=1;i<s;++i){char c=l[i];switch(c){case' ':c=b>3?' ':d[b];break;case'+':m[i]=l[i-1]=='-'?-++m[i]:- --m[i];break;case'|':b+=m[i];}o+=c;}}o+=new String(l);System.out.println(o);}}

这将读取第一行以确定最外面的盒子的宽度。这样,它就可以保留一个长度为s的数组。此数组从左到右存储框开始和结束的位置,并以0s初始化。它还存储盒子的高度。

程序逐行读取输入,并注意以下字符:

  • 众所周知,“ +”是盒子的边缘。如果左侧的输入字符为“-”,则表示该框的末尾,否则为开头。标记数组更新如下:
    • 如果此索引处的标记为0,则将值设置为1(开始)或-1(结束)。
    • 否则将值设置为0。(我们到达框的底部,不再重要)
  • '|' 通过当前索引处的标记更改当前框高。
  • ''每个字符都按原样输出,但空格除外,空格会根据当前的框高替换。

编辑:感谢TheNumberOne的建议。我还用for(;;)替换了while(true)。


1
您可以替换import java.util.Scannerimport java.util.*
TheNumberOne 2015年

2

CJam,114 111 108 104 103 102 98字节

q"-+":R/Ws*N/z{_,{"|1"/Bs*}*}%z{_,,{_I=S&{_I>_1sf&s,\"|+"f&s,m5e<" #=2."=I\t}&}fI}%N*Ws/R*Cs"|-"er

CJam解释器中在线尝试。

怎么运行的

q               e# Read all input from STDIN.
"-+":R/Ws*      e# Replace each "-+" with "-1".
N/z             e# Split at linefeeds and zip. Pushes the array of columns.
{               e# For each column:
  _,            e#   Push its length.
  {             e#   Do that many times:
    "|1"/Bs*    e#   Replace each "|1" with "11".
  }*            e#
}%              e#
z               e# Transpose. Goes back to array of rows.
{               e# For each row:
  _,,           e#   Push the array of its indexes.
  {             e#   For each index I:
    _I=         e#     Get the Ith character of the row.
    S&{         e#     If it is a space:
      _I>       e#       Get the characters after the Ith.
      _1sf&s,   e#       Count how many characters are 1's.
      \"|+"f&s, e#       Count how many are |'s or +'s.
      m5e<      e#       Subtract and truncate at 5.
      " #=2."=  e#       Retrieve the corresponding character.
      I\t       e#       Replace the Ith character of the row with that one.
    }&          e#
  }fI           e#
}%              e#
N*              e# Join the rows, separating by linefeeds.
Ws/R*           e# Turn "-1"s back to "-+"s.
Cs"|-"er        e# Turn 1's and 2's into |'s and -'s.

2

JavaScript(ES6)156

在Firefox中运行代码段进行测试

F=b=>(
  r=b.split(/\n/),q=[n=0],
  r.map((r,i)=>(
    [...r].map((c,p)=>c=='+'?(q[p]=r[p-1]=='-'?-1:1,c):c<'!'?' #=-.'[n]||' ':((n+=q[p]|0),c)).join(''))
  ).join('\n')
)

// TEST

o=x=>O.innerHTML += x+'\n\n'


;[`+--------------------------------------------------------------+
|                                                              |
|   +-------------------------------+          +-------+       |
|   |                               |          |       |       |
|   |                               |          |       |       |
|   |     +----------------+        |          |       |       |
|   |     |                |        |          +-------+       |
|   |     |                |        |                          |
|   |     |                |        |          +-------+       |
|   |     +----------------+        |          |       |       |
|   |                               |          |       |       |
|   |                               |          |       |       |
|   +-------------------------------+          +-------+       |
|                                                              |
+--------------------------------------------------------------+`
,`+-----------------------------------------------------------------------+
|     +--------------------------------------------------------------+  |
|     |      +-----------------------------------------------------+ |  |
|     |      |         +-----------------------------------------+ | |  |
|     |      |         |           +---------------------------+ | | |  |
|     |      |         |           |         +-------------+   | | | |  |
|     |      |         |           |         |             |   | | | |  |
|     |      |         |           |         +-------------+   | | | |  |
|     |      |         |           +---------------------------+ | | |  |
|     |      |         |                                         | | |  |
|     |      |         +-----------------------------------------+ | |  |
|     |      |                                                     | |  |
|     |      |                                                     | |  |
|     |      +-----------------------------------------------------+ |  |
|     |                                                              |  |
|     +--------------------------------------------------------------+  |
|                                                                       |
|                                                                       |
|                                                                       |
+-----------------------------------------------------------------------+`
,`+-------------+
|+-----------+|
||           ||
||           ||
||           ||
|+-----------+|
+-------------+`  
].forEach(t=>o(t+'\n'+F(t)+'\n'))
pre { font-size:10px;}
<pre id=O></pre>


1

CJam,76 74字节

q:Q"-+":R/Ws*{_"| "#"]_QN#~%'|m0='+=2*(U+:U+~; \"#=-.\"+U5e<= "S/=~}%Ws/R*

CJam解释器中在线尝试。

怎么运行的

q:Q        e# Read all input from STDIN and save it in the variable Q.
"-+":R/Ws* e# Replace each "-+" with "-1".
           e# This allows us to easily keep track of right corners.
{          e# For each charcter in the modified input:
  _"| "#   e#   Push its index in the string (0 for '|', 1 for ' ', -1 otherwise).

  "]_QN#~%'|m0='+=2*(U+:U+~; \"#=-.\"+U5e<= "S/

           e#   Split the pushed string at spaces, which results in three chunks:

           e#     ]        Turn the entire stack into a string.
           e#     _QN#     Compute the first index of a linefeed (row length).
           e#     ~%       Retrieve every previous character in the current column,
           e#              starting with the last.
           e#     '|m0=    Get the first character that is not a vertical bar.
           e#     '+=2*(   Push 1 if it's a plus sign and -1 otherwise.
           e#     U+:U     Add to U (initially 0) to keep track of the layer.
           e#     +~;      Add U to the string (casts to Array), dump and discard U.

           e#     "#=-."+  Concatenate this string with the space on the stack.
           e#     U5e<     Truncate U at 5.
           e#     =        Retrieve the corresponding character to replace the space.

           e#     (empty)

  =~       e#   Select and execute the proper chunk.
}%         e#
Ws/R*      e# Replace each "-1" with "-+".

1

APL(Dyalog Unicode),50字节SBCS

s[⊃¨0~¨⍨a5|5⌊+⍀+\(⊢ׯ1*+⍀++\)5=a←⎕⍳⍨s' #=-.+|']

在线尝试!

s←' #=-.+|' 给变量分配一个字符串 s

评估输入,它必须是一个字符矩阵

⎕⍳⍨s用中的索引替换其中的每个元素s

a← 分配给 a

5=返回+-es在as[5]is '+')中的位置的布尔矩阵

(⊢ׯ1*+⍀++\)这是一个列车的功能:

  • +\ 按行的部分和矩阵

  • +

  • +⍀ 列的部分和矩阵

  • ¯1* 负的力量-将几率变成¯1并将偶数变成1

  • ⊢× 乘以火车的参数-将除框角以外的所有内容清零

+⍀+\ 按列的部分总和按行的部分总和

5⌊ 最少的那个和5

5| 模5

a,¨配对a当前矩阵的和的元素

0~¨⍨ 从对中删除0

⊃¨ 首先剩下的每个

s[ ] 使用每个元素作为索引 s



@EriktheOutgolfer不用担心,答案是正确的:)准备参数(将原始输入行混合到矩阵中)的表达式应该将其反转,因为↑⍞⍞...⍞从右到左求值。对于第一个示例,这并不重要,我忘了提及这一点。
ngn


@EriktheOutgolfer啊,我知道了...我现在将删除答案,稍后再进行修复。谢谢。
ngn

应该立即修复
ngn

0

> <>118115 87字节

]0{i:0(?;:"-"=?\:"+"=?\:" "=?\$3l$g+}:ob(?
~$?:g2.10p1+4f:<p3l+10/.16@:$/>.!0"#"$
 #=-.

在线尝试!

如果其中一个符号不是a,-则可能要短6个字节。嗯,还是要缩小一点

怎么运行的:

] Resets the stack
 0{ Pushes a 0 and rotates the stack
    If the stack is empty, this initialises 0 as the counter
    Otherwise it adds an extra 0 to the stack and pushes the counter to the top
   i:0(?; Gets input and ends if it is EOF
         :"-"=?\ If the inputted character is a -
         p1+4f:< Put a - at cell (19, 1)
      .10        And skip to the end of this line

         :"+"=?\ Else if the inputted character is +
            ?10/ Generate either -1 or 1, depending on what was last put into cell (19,1)
                 The question mark represents cell (19,1), which is either + or -
         p3l     Put this number on (3, length of the stack)
.10p1+4f:<       Repeat the exact same code as with the -, except we put a + at cell (19,1)
         :" "=?\ Else if the character is a space
            @:$/ Create a copy of the counter
         .16     Skip to cell (1,6)
      g2         Get the cell (2, counter) (the  #=-.)
   ~$?           If that cell is a zero, pop it, leaving the initial space. 
                 Else pop the space, leaving the fetched character
        Else if the character is a | or a newline
        $3l$g+   Get the cell at (3, length of the stack) and add it to the counter
    For everything but the last else, we are at the end of the second line
       >.!0"#"$ Skip to the 35th instruction on the first line

 } Push the counter to the bottom of the stack
  :o Output the current character
    b(? If the character is smaller than 11 (a newline)
 ]          Skip the clear stack at the start of the line
 Repeat this until EOF

0

C(GCC) 190个 179字节

-11字节归功于ceilingcat

如果sizeof(int)> 9失败,但是您可以放心,因为您的计算机来自未来。

l;*H,*h,d;f(S){h=H=calloc(l=index(S,10)-S,9);for(char*s=S;*s;s++)h=*s^10?h:H-1,d=*s-43?d:s!=S&s[-1]==45|s-S>l&s[~l]=='|'?-1:1,*h+=*s^45?0:d,h+=write(1,*s^32|*h>4?s:" #=-."+*h,1);}

在线尝试!

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.