-
Notifications
You must be signed in to change notification settings - Fork 23
Reforestation (additional task) #26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
f0bd3c8
1d846ff
d3602f6
22ffad7
3709d12
e3e497c
75d57fa
fa8ba68
f13ca9f
2964511
5ae7967
bbf95ff
9c125dd
f336d1a
c8bafd0
ec36842
6a07b97
40382cc
1e04db1
cf9331e
f10cbd4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,15 @@ | ||
# Шаблонная задача | ||
# Reforestation simulation | ||
|
||
Описание задачи | ||
There was a large rectangular forest consisting of M * N chunks. But unfortunately there was a forest fire and some of the chunks were burned to the ground, some were partially burned, and the rest were completely salvaged. | ||
|
||
The forest after the fire is represented as a matrix M * N where each cell contains a value 0, 1 or 2 which has the following value: | ||
|
||
0: Burned to the ground | ||
|
||
1: Partially burned | ||
|
||
2: Completely preserved | ||
|
||
The goal is find the time nedeed to recover all partially burned chunks. A salvaged chunk with index (i, j) can recover partially burned neighboring chunks (up, down, left and right). If it is not possible to recover all partially burned chunks, just return -1 and the list of indecies of the chunks that can't be recovered. | ||
|
||
P.S. This problem does not use the graph classes from lib, but a kind of BFS graph traversal. |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1 +1,31 @@ | ||||||
int main() { return 0; } | ||||||
#include "utils.hpp" | ||||||
|
||||||
void PrintResult(int result, | ||||||
const std::vector<std::pair<int, int>>& unrecovered) { | ||||||
std::cout << "Time: " << result << std::endl; | ||||||
if (result == -1) { | ||||||
std::cout << "Unrecovered chunks at indices:" << std::endl; | ||||||
for (const auto& p : unrecovered) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: do not use 'std::endl' with streams; use '\n' instead [performance-avoid-endl]
Suggested change
|
||||||
std::cout << "(" << p.first << ", " << p.second << ") "; | ||||||
std::cout << std::endl; | ||||||
} | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: do not use 'std::endl' with streams; use '\n' instead [performance-avoid-endl]
Suggested change
|
||||||
} | ||||||
|
||||||
int main() { | ||||||
std::vector<std::vector<int>> forest_1 = {{2, 1, 0}, {1, 1, 1}, {0, 1, 2}}; | ||||||
std::vector<std::pair<int, int>> unrecovered_1; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: variable 'forest_1' of type 'std::vector<std::vector>' can be declared 'const' [misc-const-correctness]
Suggested change
|
||||||
int result_1 = RecoverForest(forest_1, unrecovered_1); | ||||||
PrintResult(result_1, unrecovered_1); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: variable 'result_1' of type 'int' can be declared 'const' [misc-const-correctness]
Suggested change
|
||||||
|
||||||
std::vector<std::vector<int>> forest_2 = {{2, 0, 0}, {0, 1, 0}, {0, 0, 0}}; | ||||||
std::vector<std::pair<int, int>> unrecovered_2; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: variable 'forest_2' of type 'std::vector<std::vector>' can be declared 'const' [misc-const-correctness]
Suggested change
|
||||||
int result_2 = RecoverForest(forest_2, unrecovered_2); | ||||||
PrintResult(result_2, unrecovered_2); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: variable 'result_2' of type 'int' can be declared 'const' [misc-const-correctness]
Suggested change
|
||||||
|
||||||
std::vector<std::vector<int>> forest_3 = {{2, 1, 2}, {1, 0, 1}}; | ||||||
std::vector<std::pair<int, int>> unrecovered_3; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: variable 'forest_3' of type 'std::vector<std::vector>' can be declared 'const' [misc-const-correctness]
Suggested change
|
||||||
int result_3 = RecoverForest(forest_3, unrecovered_3); | ||||||
PrintResult(result_3, unrecovered_3); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: variable 'result_3' of type 'int' can be declared 'const' [misc-const-correctness]
Suggested change
|
||||||
|
||||||
return 0; | ||||||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,8 +1,87 @@ | ||||||
|
||||||
#include <gtest/gtest.h> | ||||||
|
||||||
#include <stack> | ||||||
|
||||||
#include "utils.hpp" | ||||||
|
||||||
TEST(Template, Simple) { ASSERT_EQ(true, true); } | ||||||
// Test Case 1: Single Row with Mixed States | ||||||
TEST(RecoverForestTests, SingleRowMixedStates) { | ||||||
std::vector<std::vector<int>> forest = {{2, 1, 0, 1, 2}}; | ||||||
std::vector<std::pair<int, int>> unrecovered; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: variable 'forest' of type 'std::vector<std::vector>' can be declared 'const' [misc-const-correctness]
Suggested change
|
||||||
int result = RecoverForest(forest, unrecovered); | ||||||
EXPECT_EQ(result, 1); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: variable 'result' of type 'int' can be declared 'const' [misc-const-correctness]
Suggested change
|
||||||
} | ||||||
|
||||||
// Test Case 2: Single Column with All Preserved | ||||||
TEST(RecoverForestTests, SingleColumnAllPreserved) { | ||||||
std::vector<std::vector<int>> forest = {{2}, {2}, {2}, {2}}; | ||||||
std::vector<std::pair<int, int>> unrecovered; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: variable 'forest' of type 'std::vector<std::vector>' can be declared 'const' [misc-const-correctness]
Suggested change
|
||||||
int result = RecoverForest(forest, unrecovered); | ||||||
EXPECT_EQ(result, 0); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: variable 'result' of type 'int' can be declared 'const' [misc-const-correctness]
Suggested change
|
||||||
} | ||||||
|
||||||
// Test Case 3: Large Grid with Multiple Isolated Parts | ||||||
TEST(RecoverForestTests, LargeGridMultipleIsolatedParts) { | ||||||
std::vector<std::vector<int>> forest = {{0, 0, 2, 0, 0}, | ||||||
{0, 1, 0, 1, 0}, | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: variable 'forest' of type 'std::vector<std::vector>' can be declared 'const' [misc-const-correctness]
Suggested change
|
||||||
{0, 0, 0, 0, 0}, | ||||||
{2, 0, 0, 1, 0}, | ||||||
{0, 0, 0, 0, 2}}; | ||||||
std::vector<std::pair<int, int>> unrecovered; | ||||||
int result = RecoverForest(forest, unrecovered); | ||||||
EXPECT_EQ(result, -1); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: variable 'result' of type 'int' can be declared 'const' [misc-const-correctness]
Suggested change
|
||||||
EXPECT_EQ(unrecovered.size(), 3); | ||||||
EXPECT_EQ(unrecovered[0], std::make_pair(1, 1)); | ||||||
EXPECT_EQ(unrecovered[1], std::make_pair(1, 3)); | ||||||
EXPECT_EQ(unrecovered[2], std::make_pair(3, 3)); | ||||||
} | ||||||
|
||||||
// Test Case 4: All Burned Except One | ||||||
TEST(RecoverForestTests, AllBurnedExceptOne) { | ||||||
std::vector<std::vector<int>> forest = {{0, 0, 0}, {0, 2, 0}, {0, 0, 0}}; | ||||||
std::vector<std::pair<int, int>> unrecovered; | ||||||
int result = RecoverForest(forest, unrecovered); | ||||||
EXPECT_EQ(result, 0); | ||||||
EXPECT_EQ(unrecovered.size(), 0); | ||||||
} | ||||||
|
||||||
// Test Case 5: Large Grid with All Preserved and One Isolated Partially Burned | ||||||
TEST(RecoverForestTests, LargeGridAllPreservedOneIsolated) { | ||||||
std::vector<std::vector<int>> forest = { | ||||||
{2, 2, 2, 2}, {2, 1, 2, 2}, {2, 2, 2, 2}}; | ||||||
std::vector<std::pair<int, int>> unrecovered; | ||||||
int result = RecoverForest(forest, unrecovered); | ||||||
EXPECT_EQ(result, 1); | ||||||
EXPECT_EQ(unrecovered.size(), 0); | ||||||
} | ||||||
|
||||||
// Test Case 6: Circular Recovery | ||||||
TEST(RecoverForestTests, CircularRecovery) { | ||||||
std::vector<std::vector<int>> forest = {{2, 1, 2}, {1, 0, 1}, {2, 1, 2}}; | ||||||
std::vector<std::pair<int, int>> unrecovered; | ||||||
int result = RecoverForest(forest, unrecovered); | ||||||
EXPECT_EQ(result, 1); | ||||||
} | ||||||
|
||||||
// Test Case 7: Large Sparse Grid | ||||||
TEST(RecoverForestTests, LargeSparseGrid) { | ||||||
std::vector<std::vector<int>> forest = {{0, 0, 0, 0, 0, 0, 0}, | ||||||
{0, 2, 0, 0, 1, 0, 0}, | ||||||
{0, 0, 0, 0, 0, 0, 0}, | ||||||
{0, 0, 0, 2, 0, 0, 0}, | ||||||
{0, 0, 1, 0, 0, 0, 0}}; | ||||||
std::vector<std::pair<int, int>> unrecovered; | ||||||
int result = RecoverForest(forest, unrecovered); | ||||||
EXPECT_EQ(result, -1); | ||||||
EXPECT_EQ(unrecovered.size(), 2); | ||||||
EXPECT_EQ(unrecovered[0], std::make_pair(1, 4)); | ||||||
EXPECT_EQ(unrecovered[1], std::make_pair(4, 2)); | ||||||
} | ||||||
|
||||||
// Test Case 8: Edge Case with One Chunk | ||||||
TEST(RecoverForestTests, EdgeCaseOneChunk) { | ||||||
std::vector<std::vector<int>> forest = {{1}}; | ||||||
std::vector<std::pair<int, int>> unrecovered; | ||||||
int result = RecoverForest(forest, unrecovered); | ||||||
EXPECT_EQ(result, -1); | ||||||
EXPECT_EQ(unrecovered.size(), 1); | ||||||
EXPECT_EQ(unrecovered[0], std::make_pair(0, 0)); | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,64 @@ | ||
#include "utils.hpp" | ||
|
||
int RecoverForest(const std::vector<std::vector<int>>& grid, | ||
std::vector<std::pair<int, int>>& unrecovered) { | ||
// Number of rows and columns in the grid | ||
int n = grid.size(); | ||
int m = grid[0].size(); | ||
|
||
// Steps for rows & columns (up, right, down, left) | ||
std::vector<int> row_steps = {-1, 0, 1, 0}; | ||
std::vector<int> col_steps = {0, 1, 0, -1}; | ||
|
||
// Visited matrix | ||
std::vector<std::vector<bool>> visited(n, std::vector<bool>(m, false)); | ||
|
||
// Queue for BFS | ||
std::queue<std::pair<std::pair<int, int>, int>> q; | ||
|
||
// Counter of all partially burnt chunks | ||
int part_burnt_count = 0; | ||
|
||
// Push all salvaged chunks into the queue | ||
// & count all partially burnt chunks | ||
for (int i = 0; i < n; i++) | ||
for (int j = 0; j < m; j++) | ||
if (grid[i][j] == 2) { | ||
q.push({{i, j}, 0}); | ||
visited[i][j] = true; | ||
} else if (grid[i][j] == 1) | ||
part_burnt_count++; | ||
|
||
// BFS | ||
int recovered_count = 0, time = 0; | ||
while (!q.empty()) { | ||
int row = q.front().first.first; | ||
int col = q.front().first.second; | ||
int chunk_time = q.front().second; | ||
q.pop(); | ||
|
||
// Update time | ||
time = time < chunk_time ? chunk_time : time; | ||
|
||
// Try steping in all 4 directions | ||
for (int i = 0; i < 4; i++) { | ||
int nrow = row + row_steps[i]; | ||
int ncol = col + col_steps[i]; | ||
|
||
if (nrow >= 0 && nrow < n && ncol >= 0 && ncol < m && | ||
grid[nrow][ncol] == 1 && !visited[nrow][ncol]) { | ||
visited[nrow][ncol] = true; | ||
q.push({{nrow, ncol}, time + 1}); | ||
recovered_count++; | ||
} | ||
} | ||
} | ||
|
||
// Find and store unrecovered chunks | ||
for (int i = 0; i < n; i++) | ||
for (int j = 0; j < m; j++) | ||
if (grid[i][j] == 1 && !visited[i][j]) unrecovered.push_back({i, j}); | ||
|
||
// Return time | ||
return (recovered_count == part_burnt_count) ? time : -1; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,8 @@ | ||
#pragma once | ||
|
||
#include <iostream> | ||
#include <queue> | ||
#include <vector> | ||
|
||
int RecoverForest(const std::vector<std::vector<int>>& grid, | ||
std::vector<std::pair<int, int>>& unrecovered); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
warning: do not use 'std::endl' with streams; use '\n' instead [performance-avoid-endl]