-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpizza.java
87 lines (65 loc) · 2 KB
/
pizza.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
public class pizza
{
//THIS PROBLEM WAS USED AT THE 2017 FIU COMP SCI COMPETITION, SO THIS
//SOLUTION IS VERY SIMILAR TO THEIRS
static int getCenterRow(int rows, int cols, int numDeliveries, int[][] L) {
int rowSum = 0;
for(int i=0; i<rows; i++) {
for(int j=0; j<cols; j++) {
rowSum += L[i][j];
if( rowSum >= numDeliveries / 2) {
return i;
}
}
}
return 0;
}
static int getCenterColumn(int rows, int cols, int numDeliveries, int[][] L) {
int sum = 0;
for(int j=0; j<cols; j++) {
for(int i=0; i<rows; i++) {
sum += L[i][j];
if( sum >= numDeliveries / 2) {
return j;
}
}
}
return 0;
}
public static int sum(int[] a)
{
int sum = 0;
for(int i=0; i < a.length; i++)
sum += a[i];
return sum;
}
public static void main(String[] args)
{
Kattio scan = new Kattio(System.in);
int cases = scan.getInt();
while( cases --> 0 )
{
int cols = scan.getInt();
int rows = scan.getInt();
int[][] city = new int[rows][cols];
int numDeliv = 0;
for(int r=0; r < rows; r++)
for(int c=0; c < cols; c++)
{
city[r][c] = scan.getInt();
numDeliv += city[r][c];
}
int centerRow = getCenterRow(rows,cols, numDeliv, city);
int centerCol = getCenterColumn(rows,cols, numDeliv, city);
int cost = 0;
for(int i=0; i<rows; i++) {
for(int j=0; j<cols; j++) {
int dist = Math.abs(centerRow-i) + Math.abs(centerCol-j);
cost += (dist * city[i][j]);
}
}
System.out.println( cost + " blocks");
}
scan.close();
}
}