なあ、再帰関数好きな人いる? パート3 [転載禁止]©2ch.net (914レス)
前次1-
抽出解除 必死チェッカー(本家) (べ) 自ID レス栞 あぼーん

リロード規制です。10分ほどで解除するので、他のブラウザへ避難してください。
573: NAS6 ◆n3AmnVhjwc [] 2016/01/05(火) 06:30:43.49 ID:FnNfbNzM(1/11) AAS
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
574: NAS6 ◆n3AmnVhjwc [] 2016/01/05(火) 06:44:29.17 ID:FnNfbNzM(2/11) AAS
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
575
(1): NAS6 ◆n3AmnVhjwc [] 2016/01/05(火) 07:30:28.12 ID:FnNfbNzM(3/11) AAS
#同色上書き塗りつぶし
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

↑のループ等価が↓
576
(2): NAS6 ◆n3AmnVhjwc [] 2016/01/05(火) 07:31:25.96 ID:FnNfbNzM(4/11) AAS
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
577
(2): NAS6 ◆n3AmnVhjwc [] 2016/01/05(火) 07:31:53.09 ID:FnNfbNzM(5/11) AAS
  #下
  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
578: NAS6 ◆n3AmnVhjwc [] 2016/01/05(火) 07:34:06.45 ID:FnNfbNzM(6/11) AAS
再帰関数を無理矢理ループで書くことが
正解だなんてとても思えないんだけど・・・
579: NAS6 ◆n3AmnVhjwc [] 2016/01/05(火) 07:57:54.39 ID:FnNfbNzM(7/11) AAS
>>575-577
はdestにcolorが最初から使われていると、それ以上塗れないバグがあるな
ま、いいか
580: NAS6 ◆n3AmnVhjwc [] 2016/01/05(火) 08:01:46.66 ID:FnNfbNzM(8/11) AAS
destはコピー先だからそういう条件でクリア済みってことで
581: NAS6 ◆n3AmnVhjwc [] 2016/01/05(火) 08:14:57.62 ID:FnNfbNzM(9/11) AAS
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
582: NAS6 ◆n3AmnVhjwc [] 2016/01/05(火) 08:16:57.10 ID:FnNfbNzM(10/11) AAS
>>576-577
も同様に直してね
583: NAS6 ◆n3AmnVhjwc [] 2016/01/05(火) 08:48:45.20 ID:FnNfbNzM(11/11) AAS
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()に飛ばすように考えるのもめんどくさい
つまり、再帰関数の構造化の部分までなんでわざわざ
コーディングする必要があるのか謎
前次1-
スレ情報 赤レス抽出 画像レス抽出 歴の未読スレ AAサムネイル

ぬこの手 ぬこTOP 0.033s