让我们调用一个二维矩形数组(这意味着每个子数组都有相同的长度),一个网格。网格的每个单元要么是空空间,要么是边框。在字符网格中,空空间由单个空格表示;任何其他字符都被视为边框。示例网格(+'s,|'s和-'s )增加了可读性--它们不是网格的一部分):
+----+
| |
| |
| |
| |
| |
+----+ an empty 4x5 grid
+------+
| |
| # |
| # |
+------+ a 6x3 grid with 2 borders
+----------+
| |
| |
| ##### |
| # # |
| ## # <------ enclosed area
| # # |
| ###### |
| |
+----------+ a 10x8 grid with an enclosed area给定一个2D网格和一对坐标,填充由坐标所表示的点周围的封闭区域。
0 0
+----------+ +----------+
| | |XXXXXXXXXX|
| | -> |XXXXXXXXXX|
| | |XXXXXXXXXX|
+----------+ +----------+6 5
+-----------------+ +-----------------+
| | | |
| | | |
| ######## | | ######## |
| # # | | #XXXXXXX# |
| # #### | | #XXXX#### |
| # # | | #XXXX# |
| # # | -> | #XXXX# |
| # # | | #XXXX# |
| #### | | #### |
| | | |
| | | |
+-----------------+ +-----------------+4 6
+-----------------+ +-----------------+
| | |XXXXXXXXXXXXXXXXX|
| #### | |XXXX####XXXXXXXXX|
| # # | -> |XXX# #XXXXXXXX|
| #### | |XXXX####XXXXXXXXX|
| | |XXXXXXXXXXXXXXXXX|
+-----------------+ +-----------------+4 5
+-----------------+ +-----------------+ +-----------------+
| | | | | |
| | | | | |
| #### | | #### | | XXXX |
| #### | -> | #### | or | XXXX |
| #### | | #### | | XXXX |
| | | | | |
+-----------------+ +-----------------+ +-----------------+2 6
+----------------+ +----------------+
| | |XXXXXXXXXXXXXXXX|
| | |XXXXXXXXXXXXXXXX|
| | |XXXXXXXXXXXXXXXX|
| | -> |XXXXXXXXXXXXXXXX|
| | |XXXXXXXXXXXXXXXX|
|BBBBBBBBBBBBBBBB| |BBBBBBBBBBBBBBBB|
| | | |
| | | |
+----------------+ +----------------+#### ##
# # # #
# # == # #
# # # #
#### ##char以外的类型更适合您的目的,则可以使用ints (0表示空空间,1表示边框)或booleans (true和false )或任何其他类型--只需确保在提交的文件中包含这些信息,(row, column)坐标,因为它更方便二维数组。如果要使用(column, row) (笛卡尔)系统和/或非0索引坐标,请在提交中指定它。发布于 2017-03-22 10:39:10
在网上试试。简单递归解
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数组的数组,以及两个数字。
发布于 2021-12-15 10:16:19
[©g_#®ć©d®IgIнg‚‹«PisD®нèD®θèi2®θǝ®нǝs1Ý‚D(«ε®+}ìë\s输入-坐标作为一个包装的整数对[[x,y]],输入网格作为一个矩阵1s和0分别为空空间和边框。充满2s的洪水。
在网上试试或验证所有测试用例.(单个TIO页脚中的]J»用于打印输出矩阵.您可以随意删除它,以查看实际输出。)
尽管作为一种合作语言,05AB1E的矩阵内置在这里并不太有用。而05AB1E的模块化索引只是在这样的挑战中的一个不利因素,在这种情况下,您希望保持在网格的维度内。
[ # 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)发布于 2021-12-15 10:53:06
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。
使用我在我的这个答案、这个答案和这个答案中使用的洪水填充递归方法。
// 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)https://codegolf.stackexchange.com/questions/83808
复制相似问题