#!/usr/bin/env ruby

require "sp_ext"

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

BLOCKED = 255

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)
$w = w = map[0].size
h = map.size
sp = PathSearch.new(w,h,conv(map));

5.times do |i|
  x,y = find(map,i.to_s)
  if( x )
    sp = PathSearch.new(w,h,conv(map));
    puts "source found : #{x},#{y}"
    result = sp.block_unreachable(x+y*$w)
    puts result
    map3 = map.map{ |s| s.tr("0-9"," ") }
    
    h.times do |y|
      w.times do |x|
        map3[y][x] = "#"[0] if (sp[x+y*$w] == BLOCKED)
      end
    end
    puts map3
  end
end
