C++20 sections · 1024 units
Open in Course

Beautiful Matrix - Implementation

Complete solution


plaintext
function solveBeautifulMatrix()
for i from 1 to 5
for j from 1 to 5
read value
if value = 1 then
 rowOfOne := i
 colOfOne := j distance := abs(rowOfOne - 3) + abs(colOfOne - 3)
 print distance return

You scan the entire matrix with nested loops. Then you calculate how many row moves and column moves are needed to reach the center.