首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >注满二维网格

注满二维网格
EN

Code Golf用户
提问于 2016-06-25 20:27:15
回答 3查看 3.1K关注 0票数 16

挑战描述

让我们调用一个二维矩形数组(这意味着每个子数组都有相同的长度),一个网格。网格的每个单元要么是空空间,要么是边框。在字符网格中,空空间由单个空格表示;任何其他字符都被视为边框。示例网格(+'s,|'s和-'s )增加了可读性--它们不是网格的一部分):

代码语言:javascript
运行
复制
+----+
|    |
|    |
|    |
|    |
|    |
+----+  an empty 4x5 grid

+------+
|      |
|  #   |
|  #   |
+------+  a 6x3 grid with 2 borders

+----------+
|          |
|          |
|  #####   |
|  #   #   |
| ##   # <------ enclosed area
| #    #   |
| ######   |
|          |
+----------+  a 10x8 grid with an enclosed area

给定一个2D网格和一对坐标,填充由坐标所表示的点周围的封闭区域。

样本输入/输出

1)

代码语言:javascript
运行
复制
0 0
+----------+      +----------+
|          |      |XXXXXXXXXX|
|          |  ->  |XXXXXXXXXX|
|          |      |XXXXXXXXXX|
+----------+      +----------+

2)

代码语言:javascript
运行
复制
6 5
+-----------------+      +-----------------+
|                 |      |                 |
|                 |      |                 |
|    ########     |      |    ########     |
|    #       #    |      |    #XXXXXXX#    |
|    #    ####    |      |    #XXXX####    |
|    #    #       |      |    #XXXX#       |
|    #    #       |  ->  |    #XXXX#       |
|    #    #       |      |    #XXXX#       |
|     ####        |      |     ####        |
|                 |      |                 |
|                 |      |                 |
+-----------------+      +-----------------+

3)

代码语言:javascript
运行
复制
4 6
+-----------------+      +-----------------+
|                 |      |XXXXXXXXXXXXXXXXX|
|    ####         |      |XXXX####XXXXXXXXX|
|   #    #        |  ->  |XXX#    #XXXXXXXX|
|    ####         |      |XXXX####XXXXXXXXX|
|                 |      |XXXXXXXXXXXXXXXXX|
+-----------------+      +-----------------+

4)

代码语言:javascript
运行
复制
4 5
+-----------------+      +-----------------+      +-----------------+ 
|                 |      |                 |      |                 |
|                 |      |                 |      |                 |
|    ####         |      |    ####         |      |     XXXX        |
|    ####         |  ->  |    ####         |  or  |     XXXX        |
|    ####         |      |    ####         |      |     XXXX        |
|                 |      |                 |      |                 |
+-----------------+      +-----------------+      +-----------------+

5)

代码语言:javascript
运行
复制
2 6
+----------------+      +----------------+
|                |      |XXXXXXXXXXXXXXXX|
|                |      |XXXXXXXXXXXXXXXX|
|                |      |XXXXXXXXXXXXXXXX|
|                |  ->  |XXXXXXXXXXXXXXXX|
|                |      |XXXXXXXXXXXXXXXX|
|BBBBBBBBBBBBBBBB|      |BBBBBBBBBBBBBBBB|
|                |      |                |
|                |      |                |
+----------------+      +----------------+

Notes

  • 空网格被认为是封闭的,即边界也隐式地位于网格的边缘(见示例1和5)。
  • 封闭区域的一角不需要L形状.因此,以下两个领域是同等的:
代码语言:javascript
运行
复制
####         ##
#  #        #  #
#  #   ==   #  #
#  #        #  #
####         ##
  • 如果坐标下的单元恰好是边框,则可以将网格保持不变(如示例4)。或者把它当作一个空空间,
  • 只要在提交文件中包含此信息,就可以选择填充/空空间的任何字符,
  • 如果使用char以外的类型更适合您的目的,则可以使用ints (0表示空空间,1表示边框)或booleans (truefalse )或任何其他类型--只需确保在提交的文件中包含这些信息,
  • 上面的例子中使用的坐标是0索引的(row, column)坐标,因为它更方便二维数组。如果要使用(column, row) (笛卡尔)系统和/或非0索引坐标,请在提交中指定它。
  • 如果您不知道从哪里开始,请查看维基百科关于洪水填埋的文章
  • 请记住,这是一个密码-高尔夫挑战,所以尽量缩短代码!
EN

回答 3

Code Golf用户

发布于 2017-03-22 10:39:10

Python 2,158字节

在网上试试。简单递归解

代码语言:javascript
运行
复制
a,X,Y=input()
M=len(a)
N=len(a[0])
def R(x,y):
 if~0<x<M and~0<y<N and a[x][y]:a[x][y]=0;R(x-1,y);R(x+1,y);R(x,y-1);R(x,y+1)
R(X,Y)
print'\n'.join(map(str,a))

0-按行列顺序编制索引

1-空空间,0填充空间

将输入作为1和0数组的数组,以及两个数字。

票数 1
EN

Code Golf用户

发布于 2021-12-15 10:16:19

05AB1E,53 字节数

代码语言:javascript
运行
复制
[©g_#®ć©d®IgIнg‚‹«PisD®нèD®θèi2®θǝ®нǝs1Ý‚D(«ε®+}ìë\s

输入-坐标作为一个包装的整数对[[x,y]],输入网格作为一个矩阵1s和0分别为空空间和边框。充满2s的洪水。

在网上试试验证所有测试用例.(单个TIO页脚中的]J»用于打印输出矩阵.您可以随意删除它,以查看实际输出。)

解释:

尽管作为一种合作语言,05AB1E的矩阵内置在这里并不太有用。而05AB1E的模块化索引只是在这样的挑战中的一个不利因素,在这种情况下,您希望保持在网格的维度内。

代码语言:javascript
运行
复制
[                  # Start an infinite loop:
 ©                 #  Store the list of coordinates in variable `®` (without
                   #  popping)
                   #  (which is the first implicit input in the first iteration)
  g_               #  If it's empty:
    #              #   Stop the infinite loop
  ©ć               #  Extract head of `®`; pop and remainder-list and first
                   #  coordinate separated to the stack
    ©              #  Store this coordinate as new variable `®` (without popping)
     d             #  Check [x>=0,y>=0]
       Ig          #  Push the amount of rows of the input-matrix
         Iнg       #  Push the amount of columns of the input-matrix
            ‚      #  Pair them together
      ®      ‹     #  Check [x<rowCount,y<columnCount]
              «    #  Merge the two lists together
               Pi  #  Pop, and if all were truthy (so [x,y] is within bounds):
 s                 #   Swap so the matrix is at the top
  D                #   Duplicate this matrix
   ®нè             #   Pop and get the x'th (of coordinate `®`) row of the matrix
      D            #   Duplicate this row
       ®θè         #   Pop and get the y'th (of coordinate `®`) cell of this row
          i        #   If this value is 1:
           2®θǝ    #    Insert a 2 at this y'th cell of the row
               ®нǝ #    Insert this modified x'th row into the matrix
 s                 #    Swap so the list of coordinates is at the top again
  1Ý               #    Push [0,1]
    ‚             #    Bifurcate (Duplicate & Reverse copy) and pair:
                   #    [[0,1],[1,0]]
      D(«          #    Duplicate, negate, merge: [[0,1],[1,0],[0,-1],[-1,0]]
         ε®+}      #    Add coordinate `®` to each pair:
                   #    [[x,y+1],[x+1,y],[x,y-1],[x-1,y]]
             ì     #    Prepend-merge it at the front of the coordinates-list
          ë        #   Else: the value was 0 or 2 instead:
           \       #    Discard the duplicated row
            s      #    And swap so the coordinates list is at the top again
]                  # Close the open if-statements and infinite loop
 \                 # Discard the empty coordinates list
                   # (after which the matrix is output implicitly as result)
票数 1
EN

Code Golf用户

发布于 2021-12-15 10:53:06

Java,112个字节

代码语言:javascript
运行
复制
int f(int[][]m,int x,int y){try{if(m[x][y]==0)f(m,x+f(m,x,y+f(m,x-f(m,x,y+--m[x][y]),y)),y);}finally{return 1;}}

输入为0和1s的矩阵,分别表示空空间和边框。直接修改输入矩阵,并填充-1s。

使用我在我的这个答案这个答案这个答案中使用的洪水填充递归方法。

在网上试试。

解释:

代码语言:javascript
运行
复制
// Recursive method with integer matrix & x,y parameters and integer return-type
int f(int[][]m,int x,int y){
  try{if(m[x][y]==0)   //  If the current cell-value is 0:
            --m[x][y]  //   Decrement the current cell-value to -1
           f(m,x,y+...)//   Recursive call west
          f(m,x-...,y) //   Recursive call north
         f(m,x,y+...)  //   Recursive call east
        f(m,x+...,y);  //   Recursive call south
  }finally{            //  Catch and swallow any ArrayIndexOutOfBoundsExceptions
                       //  (shorter than manual if-checks)
           return 1;}  //  Always result in 1, so we use it to our advantage to
                       //  save bytes here by nesting, instead of having four
                       //  loose calls to each direction)
票数 1
EN
页面原文内容由Code Golf提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

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

复制
相关文章

相似问题

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