-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess.c
148 lines (120 loc) · 3.94 KB
/
process.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
void printArray(int **array, int rows, int cols) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d ", array[i][j]);
}
printf("\n");
}
}
void readArrayFromFile(int **array, int rows, int cols, const char *filename) {
FILE *file = fopen(filename, "r");
if (file == NULL) {
printf("Error opening file.\n");
return;
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
fscanf(file, "%d", &array[i][j]);
}
}
fclose(file);
}
void writeArrayToFile(int **array, int rows, int cols, const char *filename) {
FILE *file = fopen(filename, "w");
if (file == NULL) {
printf("Error opening file.\n");
return;
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
fprintf(file, "%d ", array[i][j]);
}
fprintf(file, "\n");
}
fclose(file);
}
int** createArray(int rows, int cols) {
int **array = (int **)malloc(rows * sizeof(int *));
if (!array) {
printf("Error: Memory allocation failed\n");
return NULL;
}
for (int i = 0; i < rows; i++) {
array[i] = (int *)malloc(cols * sizeof(int));
if (!array[i]) {
printf("Error: Memory allocation failed\n");
for (int j = 0; j < i; j++) {
free(array[j]);
}
free(array);
return NULL;
}
}
return array;
}
bool inBounds(int x, int y, int width, int height) {
return x >= 0 && x < width && y >= 0 && y < height;
}
void countVisibleZeros(int **image_array, int width, int height, int **result) {
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
if (image_array[y][x] == 0) {
int count = 0;
for (int yy = 0; yy < height; yy++) {
for (int xx = 0; xx < width; xx++) {
if (image_array[yy][xx] == 0 && (x != xx || y != yy)) {
bool visible = true;
int dx = xx - x;
int dy = yy - y;
int steps = abs(dx) > abs(dy) ? abs(dx) : abs(dy);
float xStep = (float)dx / steps;
float yStep = (float)dy / steps;
float xCheck = x + 0.5;
float yCheck = y + 0.5;
for (int i = 0; i < steps; i++) {
xCheck += xStep;
yCheck += yStep;
int xi = (int)xCheck;
int yi = (int)yCheck;
if (!inBounds(xi, yi, width, height) || image_array[yi][xi] == -1) {
visible = false;
break;
}
}
if (visible) {
count++;
}
}
}
}
result[y][x] = count;
}
}
}
}
int main(int argc, char *argv[]) {
if (argc < 4) {
printf("Usage: %s <rows> <cols> <range>\n", argv[0]);
return 1;
}
int rows = atoi(argv[1]);
int cols = atoi(argv[2]);
int range = atoi(argv[3]);
int** input = createArray(rows, cols);
int** result = createArray(rows, cols);
readArrayFromFile(input, rows, cols, "input.txt");
countVisibleZeros(input, cols, rows, result);
writeArrayToFile(result, rows, cols, "data.txt");
for (int i = 0; i < rows; i++) {
free(input[i]);
free(result[i]);
}
free(input);
free(result);
return 0;
}
// compile: gcc process.c -o process