Problem 11 (Project Euler) [和訳])
In the 20×20 grid below, four numbers along a diagonal line have been marked in red.
(中略)
The product of these numbers is 26 × 63 × 78 × 14 = 1788696.
What is the greatest product of four adjacent numbers in any direction (up, down, left, right, or diagonally) in the 20×20 grid?
一要素ずつスキャンしていき、縦・横・右下・左下の要素を拾っていくコードを書きました。この際、範囲外の要素にアクセスする場合を考慮したif文を書くのは面倒なので、提示された行列の右辺と下辺に三要素分の番兵(値は0)を立てました。
上辺にこのような番兵を配置しないのは当然のこととして、マイナスの添え字が有効なRubyでは左辺にも番兵を立てる必要はありません。