C++20 sections · 1024 units
Open in Course

Beautiful Matrix - Algorithm

Breaking down the solution

Here's your approach:

1.1. Create a 5×55 \times 5 array to store the matrix.

2.2. Use nested loops to read the input: outer loop for rows (11 to 55), inner loop for columns (11 to 55).

3.3. While reading, check if the current element is 11. If yes, remember its row and column.

4.4. After finding the 11 at position (r,c)(r, c), compute the distance: distance = abs(r - 3) + abs(c - 3).

5.5. Output the distance. That's the minimum number of swaps needed.