#CCBCHBA0000107. Beautiful Matrix
Beautiful Matrix
Beautiful Matrix
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
You are given a 5-by-5 matrix with exactly one 1 and 24 zeros. One move swaps two adjacent rows or two adjacent columns. Find the minimum number of moves needed to place the 1 at row 3, column 3 (1-based).
Input
Five lines of five space-separated integers 0 or 1, with exactly one 1.
Output
Print the minimum number of moves.
Subtasks
-
Subtask 1 (20%): The Manhattan distance of 1 to the center is at most 1; exactly one 1 in the 5x5 matrix.
-
Subtask 2 (30%): The Manhattan distance of 1 to the center is at most 2; exactly one 1 in the 5x5 matrix.
-
Subtask 3 (50%): The Manhattan distance of 1 to the center is at most 4; exactly one 1 in the 5x5 matrix.
Examples
Example 1
Input:
0 0 0 0 0
0 0 0 0 1
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
Output:
3
Explanation:
The 1 is at (2,5); |2-3|+|5-3|=3 adjacent swaps are needed.
Example 2
Input:
0 0 0 0 0
0 0 0 0 0
0 0 1 0 0
0 0 0 0 0
0 0 0 0 0
Output:
0
Explanation:
The 1 is at (3,3); |3-3|+|3-3|=0 adjacent swaps are needed.