なあ、再帰関数好きな人いる? パート3 [転載禁止]©2ch.net (914レス)
なあ、再帰関数好きな人いる? パート3 [転載禁止]©2ch.net http://mevius.5ch.net/test/read.cgi/tech/1448704298/
上
下
前次
1-
新
通常表示
512バイト分割
レス栞
抽出解除
必死チェッカー(本家)
(べ)
自ID
レス栞
あぼーん
573: NAS6 ◆n3AmnVhjwc [] 2016/01/05(火) 06:30:43.49 ID:FnNfbNzM Array.push()、Array.pop()があるんね >>568で済む内容を、ループで書きたかったら↓しなければならない def loopHoge2(term,arg...) while term pushargstack(arg...) dobefore(arg...) if term next end popargstack(arg...) doafter(arg...) end end stk = Array.new() def pushargstack(arg1...argn) stk.push(arg1) ... stk.push(argn) end def popargstack(arg1...argn) argn = stk.pop() ... arg1 = stk.pop() end http://mevius.5ch.net/test/read.cgi/tech/1448704298/573
574: NAS6 ◆n3AmnVhjwc [] 2016/01/05(火) 06:44:29.17 ID:FnNfbNzM def recHoge2(term,arg1...argn) dobefore(arg1...argn) if term recHoge2(term,arg1...argn) end doafter(arg1...argn) end ↑は、こう↓書き換えられる def loopHoge2(term,arg1...argn) while term pushargstack(arg1...argn) dobefore(arg1...argn) if term next end popargstack(arg1...argn) doafter(arg1...argn) end end stk = Array.new() def pushargstack(arg1...argn) stk.push(arg1) ... stk.push(argn) end def popargstack(arg1...argn) argn = stk.pop() ... arg1 = stk.pop() end http://mevius.5ch.net/test/read.cgi/tech/1448704298/574
575: NAS6 ◆n3AmnVhjwc [] 2016/01/05(火) 07:30:28.12 ID:FnNfbNzM #同色上書き塗りつぶし def refill(dest,src,x,y,color,minx,miny,maxx,maxy) if (x < minx) || (maxx < x) ||(y < miny) || (maxy < y) return end dest[y][x] = color #上 if (src[y-1][x] == color) && (dest[y-1][x] != color) refill(dest,src,x,y-1,color,minx,miny,maxx,maxy) end #左 if (src[y][x-1] == color) && (dest[y][x-1] != color) refill(dest,src,x-1,y,color,minx,miny,maxx,maxy) end #下 if (src[y+1][x] == color) && (dest[y+1][x] != color) refill(dest,src,x,y+1,color,minx,miny,maxx,maxy) end #右 if (src[y][x+1] == color) && (dest[y][x+1] != color) refill(dest,src,x+1,y,color,minx,miny,maxx,maxy) end end ↑のループ等価が↓ http://mevius.5ch.net/test/read.cgi/tech/1448704298/575
576: NAS6 ◆n3AmnVhjwc [] 2016/01/05(火) 07:31:25.96 ID:FnNfbNzM stk =Array.new() #同色上書き塗りつぶし def loop_refill(dest,src,x,y,color,minx,miny,maxx,maxy) term = 0 while !((x < minx) || (maxx < x) ||(y < miny) || (maxy < y)) dest[y][x] = color #上 if (term < 1) && (src[y-1][x] == color) && (dest[y-1][x] != color) term = 0 stk.push(x) stk.push(y) stk.push(term) y = y - 1 term = 0 next end #左 if (term < 2) && (src[y][x-1] == color) && (dest[y][x-1] != color) term = 1 stk.push(x) stk.push(y) stk.push(term) x = x - 1 term = 0 next end http://mevius.5ch.net/test/read.cgi/tech/1448704298/576
577: NAS6 ◆n3AmnVhjwc [] 2016/01/05(火) 07:31:53.09 ID:FnNfbNzM #下 if (term < 3) && (src[y+1][x] == color) && (dest[y+1][x] != color) term = 2 stk.push(x) stk.push(y) stk.push(term) y = y + 1 term = 0 next end #右 if (term < 4) && (src[y][x+1] == color) && (dest[y][x+1] != color) term = 3 stk.push(x) stk.push(y) stk.push(term) x = x + 1 term = 0 next end term = stk.pop() y = stk.pop() x = stk.pop() end end http://mevius.5ch.net/test/read.cgi/tech/1448704298/577
578: NAS6 ◆n3AmnVhjwc [] 2016/01/05(火) 07:34:06.45 ID:FnNfbNzM 再帰関数を無理矢理ループで書くことが 正解だなんてとても思えないんだけど・・・ http://mevius.5ch.net/test/read.cgi/tech/1448704298/578
579: NAS6 ◆n3AmnVhjwc [] 2016/01/05(火) 07:57:54.39 ID:FnNfbNzM >>575-577 はdestにcolorが最初から使われていると、それ以上塗れないバグがあるな ま、いいか http://mevius.5ch.net/test/read.cgi/tech/1448704298/579
580: NAS6 ◆n3AmnVhjwc [] 2016/01/05(火) 08:01:46.66 ID:FnNfbNzM destはコピー先だからそういう条件でクリア済みってことで http://mevius.5ch.net/test/read.cgi/tech/1448704298/580
581: NAS6 ◆n3AmnVhjwc [] 2016/01/05(火) 08:14:57.62 ID:FnNfbNzM 2色必要だった・・・ srcのx,yからcolor2の連続部分をdestにcolor1で塗りつぶし #同色上書き塗りつぶし def refill(dest,src,x,y,color1,color2,minx,miny,maxx,maxy) if (x < minx) || (maxx < x) ||(y < miny) || (maxy < y) return end dest[y][x] = color1 #上 if (src[y-1][x] == color2) && (dest[y-1][x] != color1) refill(dest,src,x,y-1,color1,color2,minx,miny,maxx,maxy) end #左 if (src[y][x-1] == color2) && (dest[y][x-1] != color1) refill(dest,src,x-1,y,color1,color2,minx,miny,maxx,maxy) end #下 if (src[y+1][x] == color2) && (dest[y+1][x] != color1) refill(dest,src,x,y+1,color1,color2,minx,miny,maxx,maxy) end #右 if (src[y][x+1] == color2) && (dest[y][x+1] != color1) refill(dest,src,x+1,y,color1,color2,minx,miny,maxx,maxy) end end http://mevius.5ch.net/test/read.cgi/tech/1448704298/581
582: NAS6 ◆n3AmnVhjwc [] 2016/01/05(火) 08:16:57.10 ID:FnNfbNzM >>576-577 も同様に直してね http://mevius.5ch.net/test/read.cgi/tech/1448704298/582
583: NAS6 ◆n3AmnVhjwc [] 2016/01/05(火) 08:48:45.20 ID:FnNfbNzM recHoge2(term,arg1...argn){ dobefore(arg1...argn); if(term)recHoge2(term,arg1...argn); doafter(arg1...argn); } この↑再帰関数を無理矢理 loopHoge2(term,arg1...argn){ while(term){ pushargstack(arg1...argn); dobefore(arg1...argn); if(term){continue;} popargstack(arg1...argn); doafter(arg1...argn); } } ループで書くのはpushargstack()popargstack()書くのもだし 再帰関数でreturnされた時の箇所で 制御をdoafter()に飛ばすように考えるのもめんどくさい つまり、再帰関数の構造化の部分までなんでわざわざ コーディングする必要があるのか謎 http://mevius.5ch.net/test/read.cgi/tech/1448704298/583
メモ帳
(0/65535文字)
上
下
前次
1-
新
書
関
写
板
覧
索
設
栞
歴
スレ情報
赤レス抽出
画像レス抽出
歴の未読スレ
AAサムネイル
Google検索
Wikipedia
ぬこの手
ぬこTOP
1.363s*