-
Notifications
You must be signed in to change notification settings - Fork 0
/
t.cpp
199 lines (161 loc) · 5.21 KB
/
t.cpp
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include <bits/stdc++.h>
using namespace std;
string ltrim(const string &);
string rtrim(const string &);
vector<string> split(const string &);
/*
* Complete the 'findHighPoints' function below.
*
* The function is expected to return a 2D_BOOLEAN_ARRAY.
* The function accepts 2D_INTEGER_ARRAY elevations as parameter.
*/
bool isHighPoint(vector<vector<int>>& e, int x, int y){
cout<<"grid:"<<endl;
for(unsigned int i = 0; i < e.size(); i++){
for(unsigned int j = 0; j < e[i].size(); j++){
cout<<e[i][j]<<" ";
}
cout<<endl;
}
cout<<e.size()<<" "<<e[0].size()<<" "<<x<<" "<<y<<endl;
if(x == 0){
if(y == 0){
if(e[x][y] <= e[x+1][y] || e[x][y] <= e[x+1][y+1] || e[x][y] <= e[x][y+1]){
return false;
}
}
else if(y == e[x].size()-1){
if(e[x][y] <= e[x+1][y] || e[x][y] <= e[x+1][y-1] || e[x][y] <= e[x][y-1]){
return false;
}
}
else{
if(e[x][y] <= e[x+1][y] || e[x][y] <= e[x+1][y-1] || e[x][y] <= e[x][y-1] ||
e[x][y] <= e[x+1][y+1] || e[x][y] <= e[x][y+1]){
return false;
}
}
}
if(x == e.size()-1){
if(y == 0){
if(e[x][y] <= e[x-1][y] || e[x][y] <= e[x-1][y+1] || e[x][y] <= e[x][y+1]){
return false;
}
}
else if(y == e[x].size()-1){
if(e[x][y] <= e[x-1][y] || e[x][y] <= e[x-1][y-1] || e[x][y] <= e[x][y-1]){
return false;
}
}
else{
if(e[x][y] <= e[x-1][y] || e[x][y] <= e[x-1][y-1] || e[x][y] <= e[x][y-1] ||
e[x][y] <= e[x-1][y+1] || e[x][y] <= e[x][y+1]){
return false;
}
}
}
else{ //x is in the middle somewhere
int temp = e[x].size();
if(y == 0){
if(e[x][y] <= e[x-1][y] || e[x][y] <= e[x-1][y+1] || e[x][y] <= e[x][y+1] ||
e[x][y] <= e[x+1][y+1] || e[x][y] <= e[x+1][y]){
return false;
}
}
else if(y == temp-1){
if(e[x][y] <= e[x-1][y] || e[x][y] <= e[x-1][y-1] || e[x][y] <= e[x][y-1] ||
e[x][y] <= e[x+1][y-1] || e[x][y] <= e[x+1][y]){
return false;
}
}
else{ //all 8 directions
if(e[x][y] <= e[x-1][y] || e[x][y] <= e[x-1][y+1] || e[x][y] <= e[x][y+1] ||
e[x][y] <= e[x+1][y+1] || e[x][y] <= e[x+1][y] || e[x][y] <= e[x+1][y-1] || e[x][y] <= e[x][y-1] ||
e[x][y] <= e[x-1][y-1]){
return false;
}
}
}
return true;
}
vector<vector<bool>> findHighPoints(vector<vector<int>> elevations) {
vector<vector<bool>> ret;
for(unsigned int i = 0; i < elevations.size(); i++){
vector<bool> temp(elevations[i].size(), false);
ret.push_back(temp);
}
cout<<elevations.size()<<" "<<elevations[0].size()<<endl;
for(unsigned int i = 0; i < elevations.size(); i++){
for(unsigned int j = 0; j<elevations[i].size(); j++){
if(i == 0){
if(isHighPoint(elevations,i,j)){
ret[i][j] = true;
}
}
}
}
return ret;
}
int main()
{
ofstream fout(getenv("OUTPUT_PATH"));
string elevations_rows_temp;
getline(cin, elevations_rows_temp);
int elevations_rows = stoi(ltrim(rtrim(elevations_rows_temp)));
string elevations_columns_temp;
getline(cin, elevations_columns_temp);
int elevations_columns = stoi(ltrim(rtrim(elevations_columns_temp)));
vector<vector<int>> elevations(elevations_rows);
for (int i = 0; i < elevations_rows; i++) {
elevations[i].resize(elevations_columns);
string elevations_row_temp_temp;
getline(cin, elevations_row_temp_temp);
vector<string> elevations_row_temp = split(rtrim(elevations_row_temp_temp));
for (int j = 0; j < elevations_columns; j++) {
int elevations_row_item = stoi(elevations_row_temp[j]);
elevations[i][j] = elevations_row_item;
}
}
vector<vector<bool>> result = findHighPoints(elevations);
for (int i = 0; i < result.size(); i++) {
for (int j = 0; j < result[i].size(); j++) {
fout << result[i][j];
if (j != result[i].size() - 1) {
fout << " ";
}
}
if (i != result.size() - 1) {
fout << "\n";
}
}
fout << "\n";
fout.close();
return 0;
}
string ltrim(const string &str) {
string s(str);
s.erase(
s.begin(),
find_if(s.begin(), s.end(), not1(ptr_fun<int, int>(isspace)))
);
return s;
}
string rtrim(const string &str) {
string s(str);
s.erase(
find_if(s.rbegin(), s.rend(), not1(ptr_fun<int, int>(isspace))).base(),
s.end()
);
return s;
}
vector<string> split(const string &str) {
vector<string> tokens;
string::size_type start = 0;
string::size_type end = 0;
while ((end = str.find(" ", start)) != string::npos) {
tokens.push_back(str.substr(start, end - start));
start = end + 1;
}
tokens.push_back(str.substr(start));
return tokens;
}