Advertisement
d0ntth1nc

JsonTable

Dec 20th, 2014
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.70 KB | None | 0 0
  1. <?php
  2. $matrix = json_decode( $_GET[ 'jsonTable' ] );
  3. $largestRectangle = [];
  4. $area = 0;
  5. for ($row = 0; $row < count( $matrix ); $row++) {
  6.     for ($col = 0; $col < count( $matrix[ 0 ] ); $col++) {
  7.         for ($height = 1; $height <= count( $matrix ) - $row; $height++) {
  8.             for ($width = 1; $width <= count( $matrix[ 0 ] ) - $col; $width++) {
  9.                 if (isRect( $matrix, $row, $col, $width, $height )) {
  10.                     if ($width * $height > $area) {
  11.                         $area = $width * $height;
  12.                         $largestRectangle = [ 'startRow' => $row, 'startCol' => $col,
  13.                             'width' => $width, 'height' => $height ];
  14.                     }
  15.                 }
  16.             }
  17.         }
  18.     }
  19. }
  20.  
  21. function isRect( $matrix, $startRow, $startCol, $width, $height) {
  22.     $pattern = $matrix[ $startRow ][ $startCol ];
  23.     for ($row = $startRow + 1; $row < $startRow + $height && $row < count($matrix); $row++) {
  24.         if ($matrix[ $row ][ $startCol ] != $pattern || $matrix[ $row ][ $startCol + $width - 1 ] != $pattern) {
  25.             return false;
  26.         }
  27.     }
  28.     for ($col = $startCol + 1; $col < $startCol + $width && $col < count($matrix[0]); $col++) {
  29.         if ($matrix[ $startRow ][ $col ] != $pattern || $matrix[ $startRow + $height - 1 ][ $col ] != $pattern) {
  30.             return false;
  31.         }
  32.     }
  33.     return true;
  34. }
  35.  
  36. echo "<table border='1' cellpadding='5'>";
  37. for ($row = 0; $row < count( $matrix ); $row++) {
  38.     echo '<tr>';
  39.     for ($col = 0; $col < count( $matrix[ 0 ] ); $col++) {
  40.         $isTopBorder = $row == $largestRectangle[ 'startRow' ] && $col >= $largestRectangle[ 'startCol' ] &&
  41.             $col <= $largestRectangle[ 'startCol' ] + $largestRectangle[ 'width' ] - 1;
  42.         $isBotBorder = $row == $largestRectangle[ 'startRow' ] + $largestRectangle[ 'height' ] - 1 &&
  43.             $col >= $largestRectangle[ 'startCol' ] && $col <= $largestRectangle[ 'startCol' ] + $largestRectangle[ 'width' ] - 1;
  44.         $isLeftBorder = $col == $largestRectangle[ 'startCol' ] && $row >= $largestRectangle[ 'startRow' ] &&
  45.             $row <= $largestRectangle[ 'startRow' ] + $largestRectangle[ 'height' ] - 1;
  46.         $isRightBorder = $col == $largestRectangle[ 'startCol'] + $largestRectangle['width' ] - 1 &&
  47.             $row >= $largestRectangle[ 'startRow' ] && $row <= $largestRectangle[ 'startRow' ] + $largestRectangle[ 'height' ] - 1;
  48.  
  49.         if ($isBotBorder || $isTopBorder || $isLeftBorder || $isRightBorder) {
  50.             echo "<td style='background:#CCC'>";
  51.         } else {
  52.             echo '<td>';
  53.         }
  54.         echo htmlspecialchars( $matrix[ $row ][ $col ] ).'</td>';
  55.     }
  56.     echo '</tr>';
  57. }
  58. echo '</table>';
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement