-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path3d-surface-area.java
132 lines (114 loc) · 3.23 KB
/
3d-surface-area.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
public class Solution {
// Complete the surfaceArea function below.
static int surfaceArea(int H,int W,int[][] A) {
int n = H;//A.length;
int m = W;//A[0].length;
//System.out.println(m);
int k,pos=0;//,flag=0;
int cost=0;
int noO=0;
int tba=0;
int a=0,b=0,c=0,d=0,e=0;
cost = n*m*2;
int i,j,max=0;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(A[i][j]==0)
noO++;
}
}
cost=cost-(noO*2);
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
e = A[i][j];
a = va(n,m,i,j,A);
b = vb(n,m,i,j,A);
c = vc(n,m,i,j,A);
d = vd(n,m,i,j,A);
if(e>a)
{
tba = tba+(e-a);
}
if(e>b)
{
tba = tba+(e-b);
}
if(e>c)
{
tba = tba+(e-c);
}
if(e>d)
{
tba = tba+(e-d);
}
}
}
cost = cost+tba;
return cost;
}
static int va(int n,int m,int i, int j, int[][] A)
{
if(j==0)
return 0;
else
return(A[i][j-1]);
}
static int vb(int n,int m,int i, int j, int[][] A)
{
if(j==m-1)
return 0;
else
return(A[i][j+1]);
}
static int vc(int n,int m,int i, int j, int[][] A)
{
if(i==0)
return 0;
else
return(A[i-1][j]);
}
static int vd(int n,int m,int i, int j, int[][] A)
{
if(i==n-1)
return 0;
else
return(A[i+1][j]);
}
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) throws IOException {
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
String[] nm = scanner.nextLine().split(" ");
int H = Integer.parseInt(nm[0].trim());
int W = Integer.parseInt(nm[1].trim());
/*String[] HW = scanner.nextLine().split(" ");
int H = Integer.parseInt(HW[0]);
System.out.println(H);
int W = Integer.parseInt(HW[1]);*/
//System.out.println(H);
int[][] A = new int[H][W];
for (int i = 0; i < H; i++) {
String[] ARowItems = scanner.nextLine().split(" ");
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
for (int j = 0; j < W; j++) {
int AItem = Integer.parseInt(ARowItems[j]);
A[i][j] = AItem;
}
}
int result = surfaceArea(H,W,A);
bufferedWriter.write(String.valueOf(result));
bufferedWriter.newLine();
bufferedWriter.close();
scanner.close();
}
}