kenaxt #4656(2007/12/06 19:14 GMT) [ Ruby ] Rating0/0=0.00
汎用化を狙いました。 ”ウリリ”とするとエラーになりますが暫定版としてアップします。 連続する文字列をベクトルと考え、1行読むごとにベクトルが成長したら出力しています。 例:ウオ=移動量1のベクトル、リオウ=移動量2のベクトル
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
def dir8(f) ch = Hash[*(w = f[1].split(//)).zip((0..vmax = w.length-1).to_a).flatten] hrz, vrt = {1=>"右", 0=>"", -1=>"左"}, {1=>"下", 0=>"", -1=>"上"} va = [] #--- vector array row = -1 open(f[0]) {|fin| while (line = fin.gets) != "\n" row += 1 line = line[0..-2].split(//).map {|i|ch[i]} (0...line.size).each {|col| item = [col, row, line[col]] (0...va.size).each {|i| va[i] = nextVector(va[i], [vmax, hrz, vrt]) if va[i][0..2] === item } -1.upto(1){|ic| #--- make vector around -1.upto(1){|ir| dv = (ir > 0 || ic + ir > 0 ? 1 : -1) va << [col + ic * dv, row + ir * dv, line[col] + dv, ic, ir, 0, dv] } } } va = va.select {|i|i if (i[1] > row) || (i[0]+i[1] > 0)} end } end def nextVector(a, par) x, y, val, vx, vy, len, dv = a vmax, hrz, vrt = par if (len += 1) == vmax hosei = (dv == -1 ? 0 : -1) * vmax p [x + vx * hosei, y + vy * hosei, hrz[vx] + vrt[vy]] end [x + vx * dv, y + vy * dv, val + dv, vx, vy, len, dv] end if $FILE != $0 dir8(ARGV) end #ruby -Ku dir8.rb dir8.in "ウオリ"
Rating0/0=0.00-0+
[ reply ]
kenaxt #4656() [ Ruby ] Rating0/0=0.00
Rating0/0=0.00-0+
[ reply ]