Skip to content

Instantly share code, notes, and snippets.

@inage
Created December 7, 2013 14:08
Show Gist options
  • Select an option

  • Save inage/7842092 to your computer and use it in GitHub Desktop.

Select an option

Save inage/7842092 to your computer and use it in GitHub Desktop.
Lake Counting(POJ No.2386)
## Lake Counting(POJ No.2386)
## http://rubyfiddle.com/riddles/2f66f
N = 10
M = 12
$field = []
#庭の状態
data = <<"EOS"
w........ww.
.www.....www
....ww...ww.
.........ww.
.........w..
..w......w..
.w.w.....ww.
w.w.w.....w.
.w.w......w.
..w.......w.
EOS
data = data.split("\n")
data.size.times{|i|
$field[i] = data[i].split("")
}
def dfs(x,y)
$field[x][y]= "."
-1.upto(1){|dx|
-1.upto(1){|dy|
nx = x + dx
ny = y + dy
if(0<=nx && nx<N && 0<=ny && ny<M && $field[nx][ny]=="w")
dfs(nx,ny)
end
}
}
end
res = 0
N.times{|i|
M.times{|j|
if $field[i][j] == "w"
dfs(i,j)
res += 1
end
}
}
puts res
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment