flippingMatrix

HackerRank 问题: flippingMatrix

Question

HackerRank 问题: flippingMatrix

Answer

<?php

function flippingMatrix($matrix_list) {
    $max_length_of_matrix = count($matrix_list);

    $half_of_matrix_length = (int) floor($max_length_of_matrix / 2);
    $position_mapping = [];

    $max_length_counter = $max_length_of_matrix - 1;
    for ($i = 0; $i < $max_length_of_matrix; $i++) {
        $position_mapping[$i] = $max_length_counter;
        $max_length_counter--;
    }

    $max_sum = 0;
    for ($sum_x_index = 0; $sum_x_index < $half_of_matrix_length; $sum_x_index++) {
        for ($sum_y_index = 0; $sum_y_index < $half_of_matrix_length; $sum_y_index++) {
            $position_sum_list = [];
            $position_sum_list[] = $matrix_list[$sum_x_index][$sum_y_index];
            $position_sum_list[] = $matrix_list[$sum_x_index][$position_mapping[$sum_y_index]];
            $position_sum_list[] = $matrix_list[$position_mapping[$sum_x_index]][$sum_y_index];
            $position_sum_list[] = $matrix_list[$position_mapping[$sum_x_index]][$position_mapping[$sum_y_index]];
            $find_max = max($position_sum_list);
            $max_sum+=$find_max;
        }
    }

    return $max_sum;
}