generated from threeal/project-starter
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: solve problem 2257. Count Unguarded Cells in the Grid in C (#2210)
* test(problem-2257): add support to test in C Signed-off-by: Alfi Maulana <alfi.maulana.f@gmail.com> * feat(problem-2257/c): solve by using brute force approach Signed-off-by: Alfi Maulana <alfi.maulana.f@gmail.com> --------- Signed-off-by: Alfi Maulana <alfi.maulana.f@gmail.com>
- Loading branch information
Showing
5 changed files
with
83 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
This file contains 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 |
---|---|---|
@@ -1,2 +1,5 @@ | ||
add_c_solution(c_solution c/interface.cpp c/solution.c) | ||
|
||
get_dir_name(id) | ||
add_problem_test(test-${id} test.yaml) | ||
target_link_libraries(test-${id} PRIVATE ${c_solution}) |
This file contains 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,29 @@ | ||
#include <vector> | ||
|
||
extern "C" { | ||
int countUnguarded( | ||
int m, int n, int** guards, int guardsSize, int* guardsColSize, | ||
int** walls, int wallsSize, int* wallsColSize); | ||
} | ||
|
||
int solution_c( | ||
int m, int n, std::vector<std::vector<int>> guards, | ||
std::vector<std::vector<int>> walls) { | ||
std::vector<int*> guardsDatas(guards.size()); | ||
std::vector<int> guardsSizes(guards.size()); | ||
for (std::size_t i{0}; i < guards.size(); ++i) { | ||
guardsDatas[i] = guards[i].data(); | ||
guardsSizes[i] = guards[i].size(); | ||
} | ||
|
||
std::vector<int*> wallsDatas(walls.size()); | ||
std::vector<int> wallsSizes(walls.size()); | ||
for (std::size_t i{0}; i < walls.size(); ++i) { | ||
wallsDatas[i] = walls[i].data(); | ||
wallsSizes[i] = walls[i].size(); | ||
} | ||
|
||
return countUnguarded( | ||
m, n, guardsDatas.data(), guardsDatas.size(), guardsSizes.data(), | ||
wallsDatas.data(), wallsDatas.size(), wallsSizes.data()); | ||
} |
This file contains 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,49 @@ | ||
#include <stdlib.h> | ||
#include <string.h> | ||
|
||
int countUnguarded( | ||
int m, int n, int** guards, int guardsSize, int* guardsColSize, | ||
int** walls, int wallsSize, int* wallsColSize) { | ||
(void)guardsColSize; | ||
(void)wallsColSize; | ||
|
||
char* cells = malloc(m * n * sizeof(char)); | ||
memset(cells, 0, m * n * sizeof(char)); | ||
|
||
for (int i = 0; i < guardsSize; ++i) { | ||
cells[guards[i][0] * n + guards[i][1]] = 1; | ||
} | ||
|
||
for (int i = 0; i < wallsSize; ++i) { | ||
cells[walls[i][0] * n + walls[i][1]] = 2; | ||
} | ||
|
||
for (int i = 0; i < guardsSize; ++i) { | ||
for (int j = guards[i][0] + 1; j < m; ++j) { | ||
if (cells[j * n + guards[i][1]] > 0) break; | ||
cells[j * n + guards[i][1]] = -1; | ||
} | ||
|
||
for (int j = guards[i][0] - 1; j >= 0; --j) { | ||
if (cells[j * n + guards[i][1]] > 0) break; | ||
cells[j * n + guards[i][1]] = -1; | ||
} | ||
|
||
for (int j = guards[i][1] + 1; j < n; ++j) { | ||
if (cells[guards[i][0] * n + j] > 0) break; | ||
cells[guards[i][0] * n + j] = -1; | ||
} | ||
|
||
for (int j = guards[i][1] - 1; j >= 0; --j) { | ||
if (cells[guards[i][0] * n + j] > 0) break; | ||
cells[guards[i][0] * n + j] = -1; | ||
} | ||
} | ||
|
||
int unguarded = 0; | ||
for (int i = 0; i < m * n; ++i) { | ||
if (cells[i] == 0) ++unguarded; | ||
} | ||
|
||
return unguarded; | ||
} |
This file contains 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 |
---|---|---|
|
@@ -9,6 +9,7 @@ types: | |
output: int | ||
|
||
solutions: | ||
c: | ||
cpp: | ||
function: countUnguarded | ||
|
||
|