Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def neighboringCells(matrix)
- x = 0
- y = 0
- array = Array.new(matrix.length) {Array.new(matrix[0].length)}
- while (x < matrix.length)
- while (y < matrix[0].length)
- neighbor = 0
- if x - 1 >= 0 then
- if matrix[x - 1][y] == 0 then
- neighbor = neighbor + 1
- end
- end
- if y - 1 >= 0 then
- if matrix[x][y - 1] == 0 then
- neighbor = neighbor + 1
- end
- end
- if y + 1 < matrix[0].length then
- if matrix[x][y + 1] == 0 then
- neighbor = neighbor + 1
- end
- end
- if x + 1 < matrix.length then
- if matrix[x + 1][y] == 0 then
- neighbor = neighbor + 1
- end
- end
- array[x][y] = neighbor
- y = y + 1
- end
- y = 0
- x = x + 1
- end
- return array
- end
Advertisement
Add Comment
Please, Sign In to add comment