-
Notifications
You must be signed in to change notification settings - Fork 0
/
tree.java
135 lines (131 loc) · 4.34 KB
/
tree.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
133
134
135
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class tree {
public static void main(String[] args) {
Scanner sc = null;
try {
sc = new Scanner(new File("treemap"));
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
List<List<Integer>> grid = new ArrayList<>();
while (sc.hasNextLine()) {
String row = sc.nextLine();
if (row.isEmpty()) {
break;
}
List<Integer> rowList = new ArrayList<>();
for (char c : row.toCharArray()) {
rowList.add(Character.getNumericValue(c));
}
grid.add(rowList);
}
int visibleTrees = countVT(grid);
System.out.println("Total visible trees: " + visibleTrees);
int highScore = findHighScore(grid);
System.out.println("Hightest scenic score: " + highScore);
sc.close();
}
public static int countVT(List<List<Integer>> grid) {
int rows = grid.size();
int cols = grid.get(0).size();
int count = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
int currentHeight = grid.get(i).get(j);
if (i == 0 || i == rows - 1 || j == 0 || j == cols - 1) {
count++;
continue;
}
boolean leftVisible = true;
boolean rightVisible = true;
boolean upVisible = true;
boolean downVisible = true;
// left
for (int k = 0; k < j; k++) {
if (grid.get(i).get(k) >= currentHeight) {
leftVisible = false;
break;
}
}
// right
for (int k = j + 1; k < cols; k++) {
if (grid.get(i).get(k) >= currentHeight) {
rightVisible = false;
break;
}
}
// up
for (int k = 0; k < i; k++) {
if (grid.get(k).get(j) >= currentHeight) {
upVisible = false;
break;
}
}
// down
for (int k = i + 1; k < rows; k++) {
if (grid.get(k).get(j) >= currentHeight) {
downVisible = false;
break;
}
}
if (leftVisible || rightVisible || upVisible || downVisible) {
count++;
}
}
}
return count;
}
public static int findHighScore(List<List<Integer>> grid) {
int rows = grid.size();
int cols = grid.get(0).size();
int highestScore = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
int currentHeight = grid.get(i).get(j);
int scenicScore = calculScore(grid, i, j, currentHeight);
highestScore = Math.max(highestScore, scenicScore);
}
}
return highestScore;
}
public static int calculScore(List<List<Integer>> grid, int row, int col, int height) {
if (row == 0 || row == grid.size() - 1 || col == 0 || col == grid.get(0).size() - 1) {
return 0;
}
int rows = grid.size();
int cols = grid.get(0).size();
int left = 0;
int right = 0;
int up = 0;
int down = 0;
for (int i = col - 1; i >= 0; i--) {
left++;
if (grid.get(row).get(i) >= height) {
break;
}
}
for (int i = col + 1; i < cols; i++) {
right++;
if (grid.get(row).get(i) >= height) {
break;
}
}
for (int i = row - 1; i >= 0; i--) {
up++;
if (grid.get(i).get(col) >= height) {
break;
}
}
for (int i = row + 1; i < rows; i++) {
down++;
if (grid.get(i).get(col) >= height) {
break;
}
}
return left*right*up*down;
}
}