[英]R: recursive division maze / problem with enviroments & defining the return in a recursive function
为了更好地了解R并了解递归的工作原理,我实现了递归除法迷宫生成算法。 我的实现有效(据我所知),但仅当我使用超级赋值运算符“<<-”来存储中间递归步骤时。
因此,很明显我缺少关于 return() 函数和环境如何工作的一些东西,但我无法弄清楚那是什么,或者如何使代码工作而不必使用全局分配。 我也不知道如何使这个例子更小,因为我从来没有遇到过这样的问题,很抱歉代码太长。
这是有效的代码:
N = 10 # extent x & y
fin = 2 # finish recursion
maze = array(1,dim = c(N,N))
divide <- function(grid, width, height, fin, xoff = 0, yoff = 0){
set.seed(123)
# end recursion
if (width <= fin || height <= fin){
maze <<- grid #this is the part i want to get rid of
return(grid)
}
# choose if vertical or horizontal wall
if (width < height){ orient = 2 } #horizontal
else if (height < width ){ orient = 1 } # vertical
else { orient = sample.int(2, 1, TRUE) }
# build wall with a passage in it
if (orient == 2){
wall <- 2L * sample.int((height-1)/2, 1, TRUE)
pass <- 2L * sample.int(width/2, 1, TRUE) -1
build <- 1:width
build <- build[-pass] + xoff
grid[wall + yoff, build] <- 0
}else {
wall <- 2L * sample.int((width-1)/2, 1, TRUE)
pass <- 2L * sample.int(height/2, 1, TRUE)-1
build <- 1:height
build <- build[-pass] + yoff
grid[build, wall + xoff] <- 0
}
# this is just for plotting, so that the image faces the right direction
pic <- apply(grid, 2, rev)
image(t(pic))
Sys.sleep(1.5)
# recusion in the sub areas
if (orient == 2)
{
# Top & Bottom
divide(grid = grid,
width = width,
height = wall,
fin = fin,
xoff = xoff,
yoff = yoff)
# i would expect the following to work when i just pass the grid
# that has been returned from the function call above (grid = grid)
# but that doesnt work
divide(grid = maze,
width = width,
height = height - wall,
fin = fin,
xoff = xoff,
yoff = yoff + wall)
}
else
{
# Left and Right
divide(grid = grid,
width = wall,
height = height,
fin = fin,
xoff = xoff,
yoff = yoff)
divide(grid = maze,
width = width - wall,
height = height,
fin = fin,
xoff = xoff + wall,
yoff = yoff)
}
}
divide(maze, N, N ,fin)
我的目标是通过将网格内部提供给下一个递归步骤而不是将其存储在全局对象“迷宫”中来完成这项工作。 或者作为一个问题:为什么返回函数不将网格传递给下一个函数调用?
sessionInfo()
R version 4.0.2 (2020-06-22)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19041)
为了更好地了解R并了解递归的工作原理,我实现了递归除法迷宫生成算法。 我的实现有效(据我所知),但仅当我使用超级赋值运算符“<<-”来存储中间递归步骤时。
因此,很明显我缺少关于 return() 函数和环境如何工作的一些东西,但我无法弄清楚那是什么,或者如何使代码工作而不必使用全局分配。 我也不知道如何使这个例子更小,因为我从来没有遇到过这样的问题,很抱歉代码太长。
这是有效的代码:
N = 10 # extent x & y
fin = 2 # finish recursion
maze = array(1,dim = c(N,N))
divide <- function(grid, width, height, fin, xoff = 0, yoff = 0){
set.seed(123)
# end recursion
if (width <= fin || height <= fin){
maze <<- grid #this is the part i want to get rid of
return(grid)
}
# choose if vertical or horizontal wall
if (width < height){ orient = 2 } #horizontal
else if (height < width ){ orient = 1 } # vertical
else { orient = sample.int(2, 1, TRUE) }
# build wall with a passage in it
if (orient == 2){
wall <- 2L * sample.int((height-1)/2, 1, TRUE)
pass <- 2L * sample.int(width/2, 1, TRUE) -1
build <- 1:width
build <- build[-pass] + xoff
grid[wall + yoff, build] <- 0
}else {
wall <- 2L * sample.int((width-1)/2, 1, TRUE)
pass <- 2L * sample.int(height/2, 1, TRUE)-1
build <- 1:height
build <- build[-pass] + yoff
grid[build, wall + xoff] <- 0
}
# this is just for plotting, so that the image faces the right direction
pic <- apply(grid, 2, rev)
image(t(pic))
Sys.sleep(1.5)
# recusion in the sub areas
if (orient == 2)
{
# Top & Bottom
divide(grid = grid,
width = width,
height = wall,
fin = fin,
xoff = xoff,
yoff = yoff)
# i would expect the following to work when i just pass the grid
# that has been returned from the function call above (grid = grid)
# but that doesnt work
divide(grid = maze,
width = width,
height = height - wall,
fin = fin,
xoff = xoff,
yoff = yoff + wall)
}
else
{
# Left and Right
divide(grid = grid,
width = wall,
height = height,
fin = fin,
xoff = xoff,
yoff = yoff)
divide(grid = maze,
width = width - wall,
height = height,
fin = fin,
xoff = xoff + wall,
yoff = yoff)
}
}
divide(maze, N, N ,fin)
我的目标是通过将网格内部提供给下一个递归步骤而不是将其存储在全局对象“迷宫”中来完成这项工作。 或者作为一个问题:为什么返回函数不将网格传递给下一个函数调用?
sessionInfo()
R version 4.0.2 (2020-06-22)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19041)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.