-
-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathRabbitHouse.cpp
More file actions
154 lines (127 loc) · 3 KB
/
RabbitHouse.cpp
File metadata and controls
154 lines (127 loc) · 3 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
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
#include <bits/stdc++.h>
using namespace std;
void solve(long long int r, long long int c,
vector<vector<long long int> >& grid)
{
priority_queue<pair<long long int,
pair<long long int, long long int> > >
pq;
for (long long int i = 0; i < r; i++) {
for (long long int j = 0; j < c; j++) {
pq.push(make_pair(grid[i][j],
make_pair(i, j)));
}
}
long long int res = 0;
while (!pq.empty()) {
long long int height = pq.top().first,
i = pq.top().second.first,
j = pq.top().second.second;
pq.pop();
if (height != grid[i][j])
continue;
if (i == 0) {
// Down
if (i != r - 1) {
if (grid[i + 1][j] < height - 1) {
res += height - 1 - grid[i + 1][j];
grid[i + 1][j] = height - 1;
pq.push(make_pair(height - 1,
make_pair(i + 1, j)));
}
}
// Left
if (j != 0) {
if (grid[i][j - 1] < height - 1) {
res += height - 1 - grid[i][j - 1];
grid[i][j - 1] = height - 1;
pq.push(make_pair(height - 1,
make_pair(i, j - 1)));
}
}
// Right
if (j != c - 1) {
if (grid[i][j + 1] < height - 1) {
res += height - 1 - grid[i][j + 1];
grid[i][j + 1] = height - 1;
pq.push(make_pair(height - 1,
make_pair(i, j + 1)));
}
}
}
else if (i == r - 1) {
// Up
if (i != 0) {
if (grid[i - 1][j] < height - 1) {
res += height - 1 - grid[i - 1][j];
grid[i - 1][j] = height - 1;
pq.push(make_pair(height - 1,
make_pair(i - 1, j)));
}
}
// Left
if (j != 0) {
if (grid[i][j - 1] < height - 1) {
res += height - 1 - grid[i][j - 1];
grid[i][j - 1] = height - 1;
pq.push(make_pair(height - 1,
make_pair(i, j - 1)));
}
}
// Right
if (j != c - 1) {
if (grid[i][j + 1] < height - 1) {
res += height - 1 - grid[i][j + 1];
grid[i][j + 1] = height - 1;
pq.push(make_pair(height - 1,
make_pair(i, j + 1)));
}
}
}
else {
// Down
if (grid[i + 1][j] < height - 1) {
res += height - 1 - grid[i + 1][j];
grid[i + 1][j] = height - 1;
pq.push(make_pair(height - 1,
make_pair(i + 1, j)));
}
// Up
if (grid[i - 1][j] < height - 1) {
res += height - 1 - grid[i - 1][j];
grid[i - 1][j] = height - 1;
pq.push(make_pair(height - 1,
make_pair(i - 1, j)));
}
// Left
if (j != 0) {
if (grid[i][j - 1] < height - 1) {
res += height - 1 - grid[i][j - 1];
grid[i][j - 1] = height - 1;
pq.push(make_pair(height - 1,
make_pair(i, j - 1)));
}
}
// Right
if (j != c - 1) {
if (grid[i][j + 1] < height - 1) {
res += height - 1 - grid[i][j + 1];
grid[i][j + 1] = height - 1;
pq.push(make_pair(height - 1,
make_pair(i, j + 1)));
}
}
}
}
cout << res;
}
// Driver code
int main()
{
long long int r = 4, c = 5;
vector<vector<long long int> > grid{ { 1, 0, 5, 4, 2 },
{ 1, 5, 6, 4, 8 },
{ 2, 3, 4, 2, 1 },
{ 2, 3, 4, 9, 8 } };
solve(r, c, grid);
}