Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $matrix = json_decode( $_GET[ 'jsonTable' ] );
- $largestRectangle = [];
- $area = 0;
- for ($row = 0; $row < count( $matrix ); $row++) {
- for ($col = 0; $col < count( $matrix[ 0 ] ); $col++) {
- for ($height = 1; $height <= count( $matrix ) - $row; $height++) {
- for ($width = 1; $width <= count( $matrix[ 0 ] ) - $col; $width++) {
- if (isRect( $matrix, $row, $col, $width, $height )) {
- if ($width * $height > $area) {
- $area = $width * $height;
- $largestRectangle = [ 'startRow' => $row, 'startCol' => $col,
- 'width' => $width, 'height' => $height ];
- }
- }
- }
- }
- }
- }
- function isRect( $matrix, $startRow, $startCol, $width, $height) {
- $pattern = $matrix[ $startRow ][ $startCol ];
- for ($row = $startRow + 1; $row < $startRow + $height && $row < count($matrix); $row++) {
- if ($matrix[ $row ][ $startCol ] != $pattern || $matrix[ $row ][ $startCol + $width - 1 ] != $pattern) {
- return false;
- }
- }
- for ($col = $startCol + 1; $col < $startCol + $width && $col < count($matrix[0]); $col++) {
- if ($matrix[ $startRow ][ $col ] != $pattern || $matrix[ $startRow + $height - 1 ][ $col ] != $pattern) {
- return false;
- }
- }
- return true;
- }
- echo "<table border='1' cellpadding='5'>";
- for ($row = 0; $row < count( $matrix ); $row++) {
- echo '<tr>';
- for ($col = 0; $col < count( $matrix[ 0 ] ); $col++) {
- $isTopBorder = $row == $largestRectangle[ 'startRow' ] && $col >= $largestRectangle[ 'startCol' ] &&
- $col <= $largestRectangle[ 'startCol' ] + $largestRectangle[ 'width' ] - 1;
- $isBotBorder = $row == $largestRectangle[ 'startRow' ] + $largestRectangle[ 'height' ] - 1 &&
- $col >= $largestRectangle[ 'startCol' ] && $col <= $largestRectangle[ 'startCol' ] + $largestRectangle[ 'width' ] - 1;
- $isLeftBorder = $col == $largestRectangle[ 'startCol' ] && $row >= $largestRectangle[ 'startRow' ] &&
- $row <= $largestRectangle[ 'startRow' ] + $largestRectangle[ 'height' ] - 1;
- $isRightBorder = $col == $largestRectangle[ 'startCol'] + $largestRectangle['width' ] - 1 &&
- $row >= $largestRectangle[ 'startRow' ] && $row <= $largestRectangle[ 'startRow' ] + $largestRectangle[ 'height' ] - 1;
- if ($isBotBorder || $isTopBorder || $isLeftBorder || $isRightBorder) {
- echo "<td style='background:#CCC'>";
- } else {
- echo '<td>';
- }
- echo htmlspecialchars( $matrix[ $row ][ $col ] ).'</td>';
- }
- echo '</tr>';
- }
- echo '</table>';
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement