3D Surface Area

Madison, is a little girl who is fond of toys. Her friend Mason works in a toy manufacturing factory . Mason has a 2D board  of size  with  rows and  columns. The board is divided into cells of size  with each cell indicated by it's coordinate . The cell  has an integer  written on it. To create the toy Mason stacks  number of cubes of size  on the cell .
Given the description of the board showing the values of  and that the price of the toy is equal to the 3d surface area find the price of the toy.
image
Input Format
The first line contains two space-separated integers  and  the height and the width of the board respectively.
The next  lines contains  space separated integers. The  integer in  line denotes .
Constraints
Output Format
Print the required answer, i.e the price of the toy, in one line.
Sample Input 0
1 1
1
Sample Output 0
6
Explanation 0
image The surface area of  cube is 6.
Sample Input 1
3 3
1 3 4
2 2 3
1 2 4
Sample Output 1
60
Explanation 1
The sample input corresponds to the figure described in problem statement.

 php
<?php

// Complete the surfaceArea function below.
function surfaceArea($A) {
$dt = 0;
for($i = 0; $i < count($A); $i++) {
$area = 0;
for($j = 0; $j < count($A[$i]); $j++) {
$area = 2*($A[$i][$j]*2 + 1*1);
if($j > 0) {
$area -= 2*min($A[$i][$j-1], $A[$i][$j]);
}
if($i > 0) {
$area -= 2*min($A[$i][$j], $A[$i-1][$j]);
}
$dt+= $area;
}
}
return $dt;
}

$fptr = fopen(getenv("OUTPUT_PATH"), "w");

$stdin = fopen("php://stdin", "r");

fscanf($stdin, "%[^\n]", $HW_temp);
$HW = explode(' ', $HW_temp);

$H = intval($HW[0]);

$W = intval($HW[1]);

$A == array();

for ($i = 0; $i < $H; $i++) {
fscanf($stdin, "%[^\n]", $A_temp);
$A[] = array_map('intval',
preg_split('/ /', $A_temp, -1, PREG_SPLIT_NO_EMPTY));
}

$result = surfaceArea($A);

fwrite($fptr, $result . "\n");

fclose($stdin);
fclose($fptr);

Comments

Popular posts from this blog

Intro to Tutorial Challenges

Mini-Max Sum