Beautiful Matrix Codeforces Solution

Beautiful Matrix Codeforces Solution || Beautiful Matrix Codeforces Solution in c

Question Beautiful Matrix Codeforces Solution

Beautiful Matrix Codeforces Solution

Problem Specification


The goal of Codeforces Problem 263A is to determine the fewest moves required to convert a given 5×5 matrix into a “beautiful” matrix. A “beautiful” matrix is one in which the number 1 is in the center and the remaining elements are placed in a 3×3 grid around the center.

Understanding the Issue || Beautiful Matrix Solution

Let’s start with a thorough understanding of the problem needs before diving into the solution. We are given a 5×5 matrix and must determine the smallest number of moves required to place the number 1 in the middle of the matrix. A move is defined as replacing the number 1 with an adjacent (horizontal or vertical) element. The goal is to create a “beautiful” matrix by arranging the other elements in a 3×3 grid around the number 1.

Beautiful Matrix Codeforces in c

#include<stdio.h>

int main()
{
int i,j,x=0;
for(i=1;i<=5;i++){
for(j=1;j<=5;j++){
scanf("%d",&x);
if(x==1){
printf("%d\n",abs(i-3)+abs(j-3));
}

}

}
return 0;
}

Codeforces Solution in C++

#include <iostream>
#include <cmath> 

using namespace std;

int main()
{
    int i, j, x = 0;
    for (i = 1; i <= 5; i++) {
        for (j = 1; j <= 5; j++) {
            cin >> x;
            if (x == 1) {
                cout << abs(i - 3) + abs(j - 3) << endl;
            }
        }
    }
    return 0;
}

Solution Python

def main():
    for i in range(1, 6):
        for j in range(1, 6):
            x = int(input())
            if x == 1:
                print(abs(i - 3) + abs(j - 3))

if __name__ == "__main__":
    main()

Codeforces 282A. bit++ solution in C


6 thoughts on “Beautiful Matrix Codeforces Solution”

Leave a Comment