DEV Community

Thivyaa Mohan
Thivyaa Mohan

Posted on

263 A.Beautiful Matrix | Codeforces Solution

A 5 x 5 matrix is given which contains 24 zeros and 1 ones.

And the matrix is beautiful if the number one is located in the middle.

We need to count the minimum number of moves to make the matrix beautiful.

#include<iostream>
#include<cmath>
using namespace std;

int main(){
    int c =0;
    for(int i =1;i<=5;i++)
    {
        for(int j=1;j<=5;j++)
        {
            cin>>c;
            if(c == 1){
                cout<< abs(i-3)+abs(j-3)<<endl;
            }
        }
    }
    return 0;
Enter fullscreen mode Exit fullscreen mode

Oldest comments (1)

Collapse
 
abdullakamal profile image
Abdulla-kamal

in java

import java.util.Scanner;

public class Problem {
    public static void main(String[] args) {
      Scanner input = new Scanner(System.in);
    int[][] nums = new int[5][5];
    for(int i = 0; i < nums.length; i++) {
    for(int j = 0; j < nums.length; j++) {
      nums[i][j] = input.nextInt();
    }
    }

    for(int i = 0; i < nums.length; i++) {
      for(int j = 0; j < nums.length; j++) {
        if(nums[i][j] == 1) {
          System.out.println(Math.abs(i-2) + Math.abs(j-2));
        }
      }
      }

    }
}


Enter fullscreen mode Exit fullscreen mode