-
Notifications
You must be signed in to change notification settings - Fork 0
Assignment 4 #31
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
Open
deliaioanac
wants to merge
1
commit into
master
Choose a base branch
from
deliaioanac-4
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Assignment 4 #31
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| #include <vector> | ||
| #include <iostream> | ||
|
|
||
| // Function that return the 'head' of the island patch i belongs to | ||
| int find(int i, std::vector<int> &parent) { | ||
| int head = i, aux; | ||
|
|
||
| while (head != parent[head]) { | ||
| head = parent[head]; | ||
| } | ||
|
|
||
| // Path compression (make the parent of every node along | ||
| // the way to be the head of the whole island) | ||
| while (i != parent[i]) { | ||
| aux = parent[i]; | ||
| parent[i] = head; | ||
| i = aux; | ||
| } | ||
|
|
||
| return head; | ||
| } | ||
|
|
||
| // Unite two islands | ||
| void unite(int i, int j, std::vector<int> &parent, std::vector<int> &rank) { | ||
| if (rank[i] > rank[j]) { | ||
| parent[j] = i; | ||
| } else { | ||
| parent[i] = j; | ||
| } | ||
|
|
||
| if (rank[i] == rank[j]){ | ||
| rank[j] ++; | ||
| } | ||
| } | ||
|
|
||
| int countingIslands(const std::vector<std::vector<bool> > &map) { | ||
| // If there is no map, there can be no islands | ||
| if (map.empty()) { | ||
| std::cout << "The map is empty!" << std::endl; | ||
| return 0; | ||
| } | ||
|
|
||
| // The initial number of distinct possible patches of land | ||
| int numberOfIslands = map.size() * map[0].size(); | ||
| std::vector<int> parent(numberOfIslands), rank(numberOfIslands); | ||
|
|
||
| for (int i = 0; i < map.size(); i ++) { | ||
| for (int j = 0; j < map[i].size(); j ++) { | ||
| if (map[i][j]) { | ||
| parent[i * map.size() + j] = i * map.size() + j; | ||
| rank[i * map.size() + j] = 0; | ||
|
|
||
| // Check if these is a patch of land above this one and, | ||
| // if there is, unite its island to this patch of land | ||
| if (i > 0 && map[i - 1][j]) { | ||
| unite(find(i * map.size() + j, parent), | ||
| find((i - 1) * map.size() + j, parent), parent, rank); | ||
| numberOfIslands --; | ||
| } | ||
|
|
||
| // Check if there is a patch of land to the left of this | ||
| // one and, if there is and it does not belong to the | ||
| // same island as a patch of land above, unite their islands | ||
| if (j > 0 && map[i][j - 1] && | ||
| (i == 0 || !map[i - 1][j] || | ||
| find((i - 1) * map.size() + j, parent) != | ||
| find(i * map.size() + (j - 1), parent))) { | ||
| unite(find(i * map.size() + j, parent), | ||
| find(i * map.size() + (j - 1), parent), parent, rank); | ||
| numberOfIslands --; | ||
| } | ||
| } else { | ||
| // If it is not land, it cannot be an island | ||
| numberOfIslands --; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return numberOfIslands; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| #include "Islands.cpp" | ||
| #include <gtest/gtest.h> | ||
|
|
||
| std::vector<std::vector<bool> > map; | ||
|
|
||
| TEST(CountingIslandsTest, EmptyMap) { | ||
| EXPECT_EQ(0, countingIslands(map)); | ||
| } | ||
|
|
||
| TEST(CountingIslandsTest, ExampleMap1) { | ||
| map = {{false, true, false, true}, | ||
| {true, true, false, false}, | ||
| {false, false, true, false}, | ||
| {false, false, true, false}}; | ||
|
|
||
| EXPECT_EQ(3, countingIslands(map)); | ||
| } | ||
|
|
||
| TEST(CountingIslandsTest, ExampleMap2) { | ||
| map = {{false, true, false, true}, | ||
| {true, true, true, false}, | ||
| {false, false, true, false}, | ||
| {true, false, true, false}}; | ||
|
|
||
| EXPECT_EQ(3, countingIslands(map)); | ||
| } | ||
|
|
||
| TEST(CountingIslandsTest, ExampleMap3) { | ||
| map = {{true, true, false, true}, | ||
| {false, false, false, false}, | ||
| {false, false, false, false}, | ||
| {true, false, false, true}}; | ||
|
|
||
| EXPECT_EQ(4, countingIslands(map)); | ||
| } | ||
|
|
||
| TEST(CountingIslandsTest, NoIslands) { | ||
| map = {{false, false, false, false}, | ||
| {false, false, false, false}, | ||
| {false, false, false, false}, | ||
| {false, false, false, false}}; | ||
|
|
||
| EXPECT_EQ(0, countingIslands(map)); | ||
| } | ||
|
|
||
| TEST(CountingIslandsTest, NoWaterFullIsland) { | ||
| map = {{true, true}, | ||
| {true, true}}; | ||
|
|
||
| EXPECT_EQ(1, countingIslands(map)); | ||
| } | ||
|
|
||
| int main(int argc, char **argv) { | ||
| testing::InitGoogleTest(&argc, argv); | ||
| return RUN_ALL_TESTS(); | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This if statement is very hard to parse mentally. Prefer to hide 'ugly' code like that behind helper functions:
if (j > 0 && map[i][j - 1] && AnotherCheck(...)) { ..}Whoever needs to read this in 5 months time will thank you :)