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/

全部评论

相关推荐

叶扰云倾:进度更新,现在阿里云面完3面了,感觉3面答得还行,基本都答上了,自己熟悉的地方也说的比较细致,但感觉面试官有点心不在焉不知道是不是不想要我了,求阿里收留,我直接秒到岗当阿里孝子,学校那边的房子都退租了,下学期都不回学校,全职猛猛实习半年。这种条件还不诱人吗难道 然后现在约到了字节的一面和淘天的复活赛,外加猿辅导。华为笔试完没动静。 美团那边之前投了个base广州的,把我流程卡麻了,应该是不怎么招人,我直接简历挂了,现在进了一个正常的后端流程,还在筛选,不知道还有没有hc。
点赞 评论 收藏
分享
每晚夜里独自颤抖:要求太多的没必要理
点赞 评论 收藏
分享
秋招不是要开始了吗,我都打算润了,看大家还在找不敢润了
一条从:因为不是人人都像佬一样有实习像我们这种二本仔秋招没有实习也是白忙活
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务