-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprob_1.php
51 lines (45 loc) · 965 Bytes
/
prob_1.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php
$in = STDIN;
$out = STDOUT;
$T = trim(fgets($in));
for ($x=1; $x <= $T; $x++) {
$matrix = [];
$N = trim(fgets($in));
for ($i=0; $i < $N; $i++) {
$matrix[$i] = explode(' ', trim(fgets($in)));
}
$k = get_trace($matrix, $N);
$r = get_rows($matrix, $N);
$c = get_columns($matrix, $N);
fputs($out, "Case #$x: $k $r $c\n");
}
function get_trace($matrix, $N) {
$trace = 0;
for ($i = 0; $i < $N; $i++) {
$trace += $matrix[$i][$i];
}
return $trace;
}
function get_rows($matrix, $N) {
$rows = 0;
for ($i = 0; $i < $N; $i++) {
$row = $matrix[$i];
if (has_dupes($row)) {
$rows++;
}
}
return $rows;
}
function get_columns($matrix, $N) {
$columns = 0;
for ($i = 0; $i < $N; $i++) {
$column = array_column($matrix, $i);
if (has_dupes($column)) {
$columns++;
}
}
return $columns;
}
function has_dupes($input_array) {
return count($input_array) !== count(array_flip($input_array));
}