Last active
August 29, 2015 14:10
-
-
Save holyketzer/582e18a37751134cbc11 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| m1 = [] # массивы | |
| m2 = [] | |
| s = gets.chomp #chomp на всякий случай, "обрезает строку" - удаляет пробелы и знаки переноса из конца строки (если есть) | |
| while s != 'end' | |
| row = s.split(' ').map(&:to_i) # разбиваем строку на массив, конвертируем элементы в числа | |
| m1 << row # добавляем массив-строку в первую матрицу | |
| m2 << row.dup # добавляем копию массива-строки во вторую матрицу | |
| # копию потому что иначе можификация данных во втором массиве меняла бы их и в первом | |
| s = gets.chomp | |
| end | |
| width = m1[0].size | |
| height = m1.size | |
| width.times do |col| | |
| height.times do |row| | |
| left = | |
| if col == 0 # если левый край | |
| m1[row][width-1] | |
| else | |
| m1[row][col-1] | |
| end | |
| rigth = | |
| if rigth == width-1 # если правый край | |
| m1[row][0] | |
| else | |
| m1[row][col+1] | |
| end | |
| top = | |
| if row == 0 # если верхний край | |
| m1[height-1][col] | |
| else | |
| m1[row-1][col] | |
| end | |
| bottom = | |
| if row == height-1 # если нижний край | |
| m1[0][col] | |
| else | |
| m1[row+1][col] | |
| end | |
| m2[row][col] = left + rigth + top + bottom | |
| end | |
| end | |
| m2.each do |row| | |
| puts row.join(' ') | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment