generated from threeal/project-starter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
solution.c
49 lines (39 loc) · 1.22 KB
/
solution.c
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
#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;
}