Algorithm

3D Surface Area by ON March 20 2019

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

  • 1 <= H , W <= 100
  • 1 <= Aij <= 100

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.

#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>

int checkN(int n)
{
    if(n < 0)
        return 0;
    return n;
}
int surfaceArea(int A_size_rows, int A_size_cols, int A[A_size_rows][A_size_cols]) {
    unsigned int buttomUpper= 2,right =1, back =1,lift=1,front=1;
    int total = 0,i,j;
	for(i=0;i<A_size_rows;i++)
	{
		for(j=0;j<A_size_cols;j++)
		{
			int tmp=A[i][j];
			if(i > 0 )
				back = checkN(tmp - A[i-1][j]);
			else
				back = tmp;

			if(j > 0)
				lift = checkN(tmp - A[i][j-1]);
			else
				lift = tmp;

			if(j+1 < A_size_cols)
				right = checkN(tmp -A[i][j+1]);
			else
				right = tmp;

			if(i+1 < A_size_rows)
				front =checkN(tmp -A[i+1][j]);
			else
				front = tmp;
		
			total = total + (buttomUpper + lift + back + front + right);
		}

	}
	return total;
}

int main() {
    int H,A_i;
    int W,A_j;
    scanf("%i %i", &H, &W);
    int A[H][W];
    for (A_i = 0; A_i < H; A_i++) {
       for (A_j = 0; A_j < W; A_j++) {

          scanf("%i",&A[A_i][A_j]);
       }
    }
    int result = surfaceArea(H, W, A);
    printf("%d\n", result);
    return 0;
}
author's avatar ALL POST
Knowledge contribution is better than any contribution.

RELATED POST


Sorry no more post for this category.
LEAVE A COMMENT

Leave a Reply

Your e-mail address will not be published. Required fields are marked *