def color(s,c,h,w) def dfs(s,c,x,y) s[x][y]=c [[1,0],[0,1],[-1,0],[0,-1]].each{|dx,dy| nx=x+dx ny=y+dy dfs(s,c,nx,ny) if s[nx][ny]==?. } end h.times{|i|w.times{|j| if s[i][j]==?. dfs(s,c,i,j) return true end }} false end def dist(p1,p2) (p1[0]-p2[0]).abs+(p1[1]-p2[1]).abs-1 end def min_dist(pts1,pts2) ret=1<<30 pts1.each{|p1|pts2.each{|p2| d=dist(p1,p2) ret=d if d