-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathterraces.cpp
More file actions
110 lines (85 loc) · 2.71 KB
/
terraces.cpp
File metadata and controls
110 lines (85 loc) · 2.71 KB
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
#include <iostream>
#include <vector>
#include <queue>
#include <string>
#include <sstream>
#include <limits>
using namespace std;
int rows, cols;
vector<vector<int>> grid;
vector<vector<bool>> visited(rows, vector<bool>(cols, false));
// left, right, top, bottom moves
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, -1, 1};
void bfs(const vector<vector<int>>& grid, int startX, int startY) {
//cout << "GOT HERE";
// need a queue of pairs [x,y] to check
// visited is our starting pair
queue<pair<int, int>> q;
q.push({startX, startY});
// this should be true because only bfs'ing on spots that have neighboring lower
visited[startY][startX] = true;
while (!q.empty()) {
// trying each new spot (should have been added to queue)
int x = q.front().first;
int y = q.front().second;
q.pop();
// trying each direction
for (int i = 0; i < 4; i++) {
int newX = x + dx[i];
int newY = y + dy[i];
// confirming each direction tried is possible for this search
if (newX >= 0 && newY >= 0 && newX < cols && newY < rows && !visited[newY][newX]) {
if (grid[newY][newX] >= grid[y][x]) {
q.push({newX, newY});
//visited[y][x] = true;
visited[newY][newX] = true;
}
}
}
}
}
int main() {
// attempt at beating runtime error with next 2 lines
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> cols >> rows;
cin.ignore(numeric_limits<streamsize>::max(), '\n'); // NEEDED
grid.resize(rows, vector<int>(cols));
visited.resize(rows, vector<bool>(cols, false));
int total = 0;
// make graph
// first step each line
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cin >> grid[i][j];
}
}
// grid checked -- made correctly
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
bool notLocalMin = false;
for (int k = 0; k < 4; k++) {
int newX = j + dx[k];
int newY = i + dy[k];
if (newX >= 0 && newY >= 0 && newX < cols && newY < rows) {
if (grid[newY][newX] < grid[i][j]) {
notLocalMin = true;
}
}
}
if (notLocalMin) { // this is going to be way too slow
bfs(grid, j, i);
}
}
}
for (int i = 0; i < rows; i++) { //y
for (int j = 0; j < cols; j++) { //x
if (visited[i][j] == false) {
total++;
}
}
}
cout << total;
return 0;
}