# Rabbit House | Google Kickstart 2021 Round A

#include using namespace std; void solve(long long int r, long long int c, vector& grid){ priority_queue 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) { 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))); } } 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))); } } 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) { 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))); } } 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))); } } 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 (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))); } 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))); } 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))); } } 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