A - Beautiful Matrix
Problem Link: [Codeforces 263A##link##]
- [tab]
- C++
#include <iostream> using namespace std; int main() { int el, min = 0; for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { cin >> el; if (el == 1){ if(i - 2 < 0){ min += (i - 2) * -1; }else{ min += i - 2; } if(j - 2 < 0){ min += (j - 2) * -1; }else{ min += j - 2; } cout << min; return 0; } } } }
- Python 3
matrix = [] for i in range(5): matrix.append([int(i) for i in input().split(' ')]) x, y = (0, 0) for i in range(5): if matrix[i].count(1) > 0: y, x = (i, matrix[i].index(1)) print(abs(2-y)+abs(2-x))
- JAVA
import java.util.Scanner; import static java.lang.Math.abs; public class MyClass { public static void main(String args[]) { Scanner scan = new Scanner(System.in); int x; int ans = 0; for(int i1 = 0; i1 < 5; i1++) { for (int j1 = 0; j1 < 5; j1++) { x = scan.nextInt(); if (x == 1) { ans += Math.abs(i1 - 2); ans += Math.abs(j1 - 2); } } } System.out.println(ans); } }