263A. Beautiful Matrix

263A. Beautiful Matrix

  • time limit per test2 seconds
  • memory limit per test256 megabytes
  • inputstandard input
  • outputstandard output

You've got a 5 × 5 matrix, consisting of 24 zeroes and a single number one. Let's index the matrix rows by numbers from 1 to 5 from top to bottom, let's index the matrix columns by numbers from 1 to 5 from left to right. In one move, you are allowed to apply one of the two following transformations to the matrix:

你得了5 × 5矩阵,由24个零和一个数字1组成。让我们从上到下按1到5的数字对矩阵行进行索引,让我们从左到右按1到5的数字对矩阵列进行索引。在一次移动中,您可以对矩阵应用以下两种变换之一:

  • Swap two neighboring matrix rows, that is, rows with indexes i and i + 1 for some integer i (1 ≤ i < 5).

交换两个相邻的矩阵行,即索引为i和i + 1的行对于某些整数i(1 ≤ i < 5).

  • Swap two neighboring matrix columns, that is, columns with indexes j and j + 1 for some integer j (1 ≤ j < 5).

交换两个相邻的矩阵列,即索引为j和j + 1的列对于某些整数j(1 ≤ j < 5).

You think that a matrix looks beautiful, if the single number one of the matrix is located in its middle (in the cell that is on the intersection of the third row and the third column). Count the minimum number of moves needed to make the matrix beautiful.

你认为一个矩阵看起来很漂亮,如果矩阵中的一个数字位于它的中间(在第三行和第三列相交的单元格中)。计算使矩阵美观所需的最少移动次数。

Input

The input consists of five lines, each line contains five integers: the j-th integer in the i-th line of the input represents the element of the matrix that is located on the intersection of the i-th row and the j-th column. It is guaranteed that the matrix consists of 24 zeroes and a single number one.

输入由五行组成,每行包含五个整数:输入的第i行中的第j个整数表示位于第i行和第j列交点上的矩阵元素。保证矩阵由24个零和一个数字1组成。

Output

Print a single integer — the minimum number of moves needed to make the matrix beautiful.

打印一个整数——使矩阵美观所需的最小移动次数。

Examples
input1

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

output1

3

input2

0 0 0 0 0

0 0 0 0 0

0 1 0 0 0

0 0 0 0 0

0 0 0 0 0

output2

1

Solution

矩阵中间为i=2,j=2,只要识别到输入为1的部分,计算它的abs(i-2)+abs(j-2)即可

Code
#include <iostream>
using namespace std;

//263A. Beautiful Matrix
int main() {
    int arr[5][5] = {0};//arr:矩阵
    int res = 0;//res:使矩阵美观所需的最小移动次数
    for(int i = 0; i < 5;i++){
        for(int j = 0;j < 5;j++){
            cin >> arr[i][j];
            if(arr[i][j] == 1){
                res = abs(i-2) + abs(j-2);
            }
        }
    }
    cout << res << endl;
    return 0;
}
CodeForces 文章被收录于专栏

https://codeforces.ml/

全部评论

相关推荐

10-09 22:05
666 C++
找到工作就狠狠玩CSGO:报联合国演讲,报电子烟设计与制造
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务