#!/usr/bin/env ruby

require "sp_ext"

map=[
'###########',
'# #  4   @#',
'#2####    #',
'#######   #',
'#3#   ##  #',
'# # # @   #',
'#   #######',
'# 1     0 #',
'###########'
]


def find(m,a)
  i = 0
  x = nil
  y = 0
  for s in m
    break if (x = s.index(a))
    y+=1;
  end
  [x,y]
end

def conv(m)
  s1 = "@ #0-9"
  s2 = "    "
  s2[0],s2[1],s2[2],s2[3] = 0,1,255,1
  m.map { |s| s.tr(s1,s2) }
end

puts map[1][9]
cm = conv(map)
puts "hmm: #{cm[1][9]}"
$w = map[0].size
sp = PathSearch.new($w,map.size,conv(map));


5.times do |i|
  x,y = find(map,i.to_s)
  if( x )
    puts "source found : #{x},#{y}"
    pos = x+y*$w
    result = sp.unweighted_search(x+y*$w)
    puts result
    map3 = map.map{ |s| s.tr("0-9"," ") }
    while (pos = sp.next(pos)) >=0
      map3[pos/$w][pos%$w] = "."[0] 
    end
    puts map3
  end
end
